题
我在SO上发现了几个关于JavaMail API和通过SMTP服务器发送邮件的其他问题,但没有人讨论过使用TLS安全性。我正在尝试使用JavaMail通过我的工作SMTP邮件服务器向我自己发送状态更新,但它需要TLS,我无法在线找到有关如何使用JavaMail访问需要TLS加密的SMTP服务器的任何示例。任何人都可以帮忙吗?
解决方案
我们的产品实际上有一些通知代码,如果可用,我们会使用TLS发送邮件。
您需要设置Java Mail属性。您只需要TLS,但如果您的SMTP服务器使用SSL,则可能需要SSL。
Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true"); // If you need to authenticate
// Use the following if you need SSL
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
然后,您可以将其传递给JavaMail会话或任何其他会话实例化器,如 Session.getDefaultInstance(props)
。
其他提示
好帖子,行
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
如果SMTP服务器使用 SSL身份验证,则必须,就像GMail SMTP服务器一样。但是,如果服务器使用通过TLS进行明文身份验证,则不应该出现,因为Java Mail会抱怨初始连接是纯文本。
还要确保使用的是最新版本的Java Mail。最近我使用了之前项目中的一些旧Java Mail jar并且无法使代码工作,因为登录过程失败了。在我升级到最新版本的Java Mail之后,错误的原因变得清晰了:它是一个javax.net.ssl.SSLHandshakeException,它没有在旧版本的lib中抛出。
上述示例中的设置对我使用的服务器不起作用( authsmtp.com ) 。我一直收到这个错误:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
我删除了mail.smtp.socketFactory设置,一切正常。最终设置是这样的(没有使用SMTP身份验证,我在其他地方设置了端口):
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.starttls.enable", "true");
只需使用以下代码即可。通过Java发送电子邮件非常有用,它可以工作:
import java.util.*;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.*;
import javax.mail.Provider;
import javax.mail.internet.*;
public class Main {
public static void main(String[] args) {
final String username="your@gmail.com";
final String password="password";
Properties prop=new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
String body="Dear Renish Khunt Welcome";
String htmlBody = "<strong>This is an HTML Message</strong>";
String textBody = "This is a Text Message.";
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("receiver@gmail.com"));
message.setSubject("Testing Subject");
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
message.setText(htmlBody);
message.setContent(textBody, "text/html");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
使用 Simple Java Mail 5.0.0 (simplejavamail.org),它非常简单,图书馆将负责所有会话属性。
以下是使用Google的SMTP服务器的示例:
Email email = EmailBuilder.startingBlank()
.from("lollypop", "lol.pop@somemail.com")
.to("C.Cane", "candycane@candyshop.org")
.withSubject("hey")
.withPlainText("We should meet up!")
.withHTMLText("<b>We should meet up!</b>")
.buildEmail();
MailerBuilder.withSMTPServer("smtp.gmail.com", 25, "user", "pass", SMTP_TLS)
.buildMailer()
.sendMail(email);
MailerBuilder.withSMTPServer("smtp.gmail.com", 587, "user", "pass", SMTP_TLS)
.buildMailer()
.sendMail(email);
MailerBuilder.withSMTPServer("smtp.gmail.com", 465, "user", "pass", SMTP_SSL)
.buildMailer()
.sendMail(email);
如果您启用了双因素登录,则需要生成特定于应用程序的密码来自您的Google帐户。