使用Apache Commons电子邮件库以Java发送电子邮件

user93796:

我正在使用Apache Commons电子邮件库发送电子邮件,但是无法通过GMail SMTP服务器发送电子邮件。
任何人都可以提供适用于GMail SMTP服务器和其他服务器的示例代码吗?

我正在使用以下无效的代码:

String[] recipients = {"[email protected]"};

SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setAuthentication("[email protected]", "mypasswd");
email.setDebug(true);
email.setSmtpPort(465);

for (int i = 0; i < recipients.length; i++)
{
    email.addTo(recipients[i]);
}

email.setFrom("[email protected]", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();
克里斯·戴尔(Chris Dail):

将电子邮件发送到GMail SMTP服务器需要身份验证和SSL。用户名和密码非常简单。确保设置了以下属性以启用身份验证和SSL,并且该属性应该可以正常工作。

mail.smtp.auth=true
mail.smtp.starttls.enable=true

在示例代码中,将以下内容添加到已启用的TLS。

对于API版本<1.3,请使用:
email.setTSL(true);
对于版本=> 1.3,该方法已弃用,而应使用:email.setStartTLSEnabled(true);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章