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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/848794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:08:29  来源:igfitidea点击:

Sending email in Java using Apache Commons email libs

javaemailsmtpgmailapache-commons-email

提问by user93796

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?

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

I am using the following code which does not work:

我正在使用以下不起作用的代码:

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();

采纳答案by Chris Dail

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.

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

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

To the sample code add the following to enabled TLS.

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

For API-Versions < 1.3 use:
email.setTSL(true);
the method is deprecated for versions >= 1.3, and instead you should use: email.setStartTLSEnabled(true);

对于 API 版本 < 1.3 使用:
email.setTSL(true);
对于版本 >= 1.3 不推荐使用该方法,而您应该使用:email.setStartTLSEnabled(true);

回答by Hein B

using commons.email worked for me.

使用 commons.email 对我有用。

HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setSSL(true);

回答by Serge

Please find below a code which works. Obviously, you have to add the apache jar to your project's build path.

请在下面找到一个有效的代码。显然,您必须将 apache jar 添加到项目的构建路径中。

public static void sendSimpleMail() throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator("your gmail username",
            "your gmail password"));
    email.setDebug(false);
    email.setHostName("smtp.gmail.com");
    email.setFrom("[email protected]");
    email.setSubject("Hi");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("[email protected]");
    email.setTLS(true);
    email.send();
    System.out.println("Mail sent!");
}

Regards, Sergiu

问候, 塞尔吉