1

我使用以下 java 程序从 gmail 帐户发送邮件

final String username = "user@gmail.com";
final String password = "password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");

Session session =
    Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("user@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
                          InternetAddress.parse("user@live.in"));
    message.setSubject("Testing Subject");
    message.setText("Dear Bhavik Patel," +
                    "\n\n This is just a mail!");

    Transport.send(message);

    System.out.println("Done");

} catch (Exception e) {
    throw new RuntimeException(e);
}

我也试过端口 587 但它不工作

Transport.send(message); 

在此执行中尝试连接并发送

我不知道它有什么问题。我也试过telnet,从那里我可以连接

例外:

java.lang.RuntimeException:javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:465,响应:-1

4

3 回答 3

0

我的代码和您的代码之间存在细微差别send_mail,但我无法找出您的问题是由什么引起的。这是我的代码:

 public static int sendMail(String SMTPServer,
                    String Sender,
                    String Recipient,
                    String Subject,
                    String Body,
                    String ErrorMessage,
                    String Attachments) {
// Error status;
int ErrorStatus = 0;

// Create some properties and get the default Session;
final String username = Sender;
final String password = "passwd";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
  });

try {
   // Create a message.
   MimeMessage msg = new MimeMessage(session);

   // extracts the senders and adds them to the message.
   // Sender is a comma-separated list of e-mail addresses as per RFC822.
   {
      InternetAddress[] TheAddresses = InternetAddress.parse(Sender);
      msg.addFrom(TheAddresses);
   }

   // Extract the recipients and assign them to the message.
   // Recipient is a comma-separated list of e-mail addresses as per RFC822.
   {
      InternetAddress[] TheAddresses = InternetAddress.parse(Recipient);
      msg.addRecipients(Message.RecipientType.TO,TheAddresses);
   }

   // Subject field
   msg.setSubject(Subject);

   // Create the Multipart to be added the parts to
   Multipart mp = new MimeMultipart();

   // Create and fill the first message part
   {
      MimeBodyPart mbp = new MimeBodyPart();
      mbp.setText(Body);

      // Attach the part to the multipart;
      mp.addBodyPart(mbp);
   }

   // Add the Multipart to the message
   msg.setContent(mp);

   // Set the Date: header
   msg.setSentDate(new Date());

   // Send the message;
   Transport.send(msg);
} catch (MessagingException MsgException) {
    System.out.println("blows here");
    ErrorMessage = MsgException.toString();
    Exception TheException = null;
    if ((TheException = MsgException.getNextException()) != null)
     ErrorMessage += "\n" + TheException.toString();
     ErrorStatus = 1;
}
System.out.println(ErrorMessage);
return ErrorStatus;
} 

我一直在使用这段代码没有任何问题。

希望能帮助到你,

于 2013-09-05T21:17:17.747 回答
0

我正在使用该端口587并且能够发送/接收邮件..或者您可以设置属性

mail.smtp.timeout25000并尝试是否是超时异常..

于 2013-09-05T06:40:58.123 回答
0

试试这些 JavaMail FAQ 条目:

您需要设置“mail.smtp.ssl.enable”,而不是 starttls。

于 2013-09-05T20:36:54.597 回答