简体   繁体   中英

Play Framework Sending Email by using Apache Commons

I am trying to create a page that sends an email with attachment on Play Framework. I first need to know how to send a simple email. I am following 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();

Execution Exception: [EmailException: Sending the email to the following server failed : smtp-mail.outlook.com:465]

I don't know why it says 465 even I set port as 587

Exception on terminal: Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 465; timeout 60000; Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp-mail.outlook.com, 465; timeout 60000;

What is the reason that program accepts port number as 465 .

Same issue also occurs when I use gmail

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM