9
Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.send();

它给出了以下异常

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
4

5 回答 5

19

Commons 电子邮件用户指南有一个使用 SSL 的 Gmail 示例。

https://commons.apache.org/proper/commons-email/userguide.html

SSL/TLS(端口 465) -> email.setSSLOnConnect(true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

STARTTLS (端口 587) -> email.setStartTLSEnabled(true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setStartTLSEnabled(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();
于 2014-01-20T19:26:26.563 回答
14

这对我有用

Email email = new SimpleEmail();
String authuser = "...@gmail.com";
String authpwd = "xxxx";
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put("mail.smtps.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtps.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("xxxx@gmail.com", "ToName");
email.setTLS(true);
email.send();
于 2009-11-23T15:17:14.810 回答
10

您是否不需要告诉 Commons Email 您正在发送 TLS 电子邮件

email.setTLS(true);

在您致电 email.send() 之前?

我不确定这是否能解决你的问题,因为我不确定你是否在连接到 smtp.gmail.com:465 或成功发送到它时遇到问题(错误消息/异常是模棱两可的,因为你'已经介绍过),但据我所知,这绝对是缺少的东西。

于 2009-11-23T16:10:05.937 回答
0

如果要使用 TLS,请使用端口 587 和 starttls:

email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
// or call this:
email.setStartTLSEnabled().
// and set the port to 587
email.setSmtpPort(smtpPort);

如果您想使用端口 465,则不要将此设置为 true,而是调用以下函数:

email.setSSLOnConnect(true);
email.setSslSmptPort("465");
// or can set below but remember it is not "smtps" it is "smtp" and port nmber should be 465.

email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
于 2020-12-17T05:52:55.057 回答
0

不要忘记启用 google安全性较低的应用访问

于 2021-08-11T13:54:38.397 回答