Play Framework使用Apache Commons发送电子邮件

时间:2016-05-25 18:53:01

标签: java playframework-2.2

我正在尝试创建一个在Play Framework上发送带附件的电子邮件的页面。我首先需要知道如何发送简单的电子邮件。我跟着 Apache Commons Userguide for Email

   Email email = new SimpleEmail();
        email.setHostName("smtp-mail.outlook.com");
        email.setSmtpPort(587);
        email.setAuthenticator(new DefaultAuthenticator("email@outlook.com", "password"));
        email.setSSLOnConnect(true);
        email.setFrom("user@outlook.com");
        email.setSubject("Subject");
        email.setMsg("This is a test mail ... :-)");
        email.addTo("add@outlook.com");
        email.send();

执行例外:[EmailException: Sending the email to the following server failed : smtp-mail.outlook.com:465]

即使我将端口设置为465

,我也不知道为什么会说587

终端上的例外:Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 465; timeout 60000;

程序接受端口号为465的原因是什么。

使用gmail

时也会出现同样的问题

1 个答案:

答案 0 :(得分:0)

It's answered in http://stackoverflow.com/questions/26393906/error-sending-email-with-gmail.

You have to login to "https://www.google.com/settings/security/lesssecureapps" for google and enable then run the program. It works.

If it's not enabled we get the following exception 
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
    at org.apache.commons.mail.Email.send(Email.java:1448)
    at EmailTest.main(EmailTest.java:19)
Caused by: javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:306)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
    ... 2 more


(This is for gmail)