2016-05-25 88 views
1

我試圖創建一個頁面,它在Play Framework上發送帶附件的電子郵件。我首先需要知道如何發送簡單的電子郵件。我正在關注 Apache Commons Userguide for Email播放框架使用Apache Commons發送電子郵件

Email email = new SimpleEmail(); 
     email.setHostName("smtp-mail.outlook.com"); 
     email.setSmtpPort(587); 
     email.setAuthenticator(new DefaultAuthenticator("[email protected]", "password")); 
     email.setSSLOnConnect(true); 
     email.setFrom("[email protected]"); 
     email.setSubject("Subject"); 
     email.setMsg("This is a test mail ... :-)"); 
     email.addTo("[email protected]"); 
     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

回答

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) 
也發生

同樣的問題

相關問題