使用 Commons-Email 向 Gmail 发送电子邮件

发布于 2024-08-13 00:52:53 字数 1641 浏览 4 评论 0原文

Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.send();

它给出了以下异常

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.send();

and it gives the following exception

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑着哭最痛 2024-08-20 00:52:53

Commons Email 用户指南提供了使用 SSL 的 Gmail 示例。

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

< strong>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("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
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("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();

The Commons Email user guide has an example for Gmail using SSL.

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

SSL/TLS (Port 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("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();

STARTTLS (Port 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("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();
爱要勇敢去追 2024-08-20 00:52:53

这对我有用

Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.setTLS(true);
email.send();

This does work for me

Email email = new SimpleEmail();
String authuser = "[email protected]";
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("[email protected]", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("[email protected]", "ToName");
email.setTLS(true);
email.send();
若能看破又如何 2024-08-20 00:52:53

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

email.setTLS(true);

在调用 email.send() 之前?

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

Don't you need to tell Commons Email that you're sending a TLS email:

email.setTLS(true);

prior to your call to email.send()?

I'm not sure if this will fix what ails you, since I'm not sure whether you're experiencing a problem connecting to smtp.gmail.com:465 or successfully sending to it (the error message/exception is ambiguous as you've presented it), but it's definitely something that's missing so far as I can tell.

可是我不能没有你 2024-08-20 00:52:53

如果您想使用 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");

if you want to use TLS use port 587 and starttls:

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

If you want to use port 465 then do not set this true but call below functions:

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");
愁杀 2024-08-20 00:52:53

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

Don't forget to enable google Less secure app access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文