我正在使用java mail api发送邮件。我可以使用 Google 帐户发送邮件,但在使用 SSL 从我的域发送邮件时遇到了一些问题。
这是实现,请让我知道我缺少的地方。
// Create all the needed properties
Properties connectionProperties = new Properties();
// SMTP host
connectionProperties.put("mail.smtp.host", "abc.example.com");
// Is authentication enabled
connectionProperties.put("mail.smtp.auth", "true");
// Is StartTLS enabled
connectionProperties.put("mail.smtp.starttls.enable", "true");
// SSL Port
connectionProperties.put("mail.smtp.socketFactory.port", "465");
// SSL Socket Factory class
connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// SMTP port, the same as SSL port :)
connectionProperties.put("mail.smtp.port", "465");
// Create the session
Session session = Session.getDefaultInstance(connectionProperties, new javax.mail.Authenticator()
{ // Define the authenticator
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("abc@example.net", "abcxdsjdf123");
}
});
System.out.println("done!");
// Create and send the message
try
{
// Create the message
Message message = new MimeMessage(session);
// Set sender
message.setFrom(new InternetAddress("abc@example.net"));
// Set the recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@example.net"));
// Set message subject
message.setSubject("Hello from Test");
// Set message text
message.setText("This is test mail;)");
System.out.print("Sending message...");
// Send the message
Transport.send(message);
System.out.println("done!");
}
catch (MessagingException e)
{
System.out.println(e.toString());
}
以下是我得到的例外:
javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
任何形式的帮助都将是可观的。提前致谢。