我正在使用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("[email protected]", "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("[email protected]"));
// Set the recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
// 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)
任何形式的帮助都是有意义的。
提前致谢。
最佳答案
尝试这个..
public class SentMail {
public boolean deliverMail(String username, String email, String subject, String content) {
boolean sent = false;
try {
String delim = "\n\n";
String text = "Hi ," + delim + content + delim + "Thank you ," + delim + username + "\n" + email;
String from = "from_email";
String pass = "from_pass";
String to = "to_email";
<!--Your host-->
String host = "smtp.zoho.com";
<!--Host port-->
String port = "465";
Properties properties = System.getProperties();
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", port);
properties.put("mail.smtp.startssl.enable", "true");
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
sent = true;
} catch (Exception e) {
}
return sent;
}
}