我正在使用 Apache Commons Email 库发送电子邮件,但我无法通过 GMail SMTP 服务器发送它们。
谁能提供适用于 GMail SMTP 服务器和其他服务器的示例代码?
我正在使用以下代码,但它不起作用:
String[] recipients = {"[email protected] /cdn-cgi/l/email-protection"};
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setAuthentication("[email protected] /cdn-cgi/l/email-protection", "mypasswd");
email.setDebug(true);
email.setSmtpPort(465);
for (int i = 0; i < recipients.length; i++)
{
email.addTo(recipients[i]);
}
email.setFrom("[email protected] /cdn-cgi/l/email-protection", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
向 GMail SMTP 服务器发送电子邮件需要身份验证和 SSL。用户名和密码非常简单。确保您已设置以下属性来启用身份验证和 SSL,并且它应该可以工作。
mail.smtp.auth=true
mail.smtp.starttls.enable=true
在示例代码中添加以下内容以启用 TLS。
对于 API 版本
email.setTSL(true);
对于 >= 1.3 的版本,该方法已被弃用,您应该使用:email.setStartTLSEnabled(true);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)