Sending email in Java using Apache Commons email libs(使用 Apache Commons 电子邮件库在 Java 中发送电子邮件)
问题描述
我正在使用 Apache Commons 电子邮件库发送电子邮件,但我无法通过 GMail SMTP 服务器发送它们.
任何人都可以提供与 GMail SMTP 服务器和其他服务器一起使用的示例代码吗?
I am using Apache Commons Email library to send emails, but I am not able to send them via GMail SMTP server.
Can anyone provide sample code which works with GMail SMTP server and others?
我正在使用以下不起作用的代码:
I am using the following code which does not work:
String[] recipients = {"receiver@gmail.com"};
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setAuthentication("sender@gmail.com", "mypasswd");
email.setDebug(true);
email.setSmtpPort(465);
for (int i = 0; i < recipients.length; i++)
{
email.addTo(recipients[i]);
}
email.setFrom("sender@gmail.com", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
推荐答案
向 GMail SMTP 服务器发送电子邮件需要身份验证和 SSL.用户名和密码非常简单.确保您已设置以下属性以启用身份验证和 SSL,并且它应该可以工作.
Sending emails to the GMail SMTP server requires authentication and SSL. The username and password is pretty straight forward. Make sure you have the following properties set to enable authentication and SSL and it should work.
mail.smtp.auth=true
mail.smtp.starttls.enable=true
在示例代码中添加以下内容以启用 TLS.
To the sample code add the following to enabled TLS.
对于 API 版本
1.3 用途:email.setTSL(true);
对于 >= 1.3 的版本不推荐使用该方法,您应该使用:email.setStartTLSEnabled(true);
这篇关于使用 Apache Commons 电子邮件库在 Java 中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Apache Commons 电子邮件库在 Java 中发送电子邮件
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
