2011-07-29 234 views
-4

無法通過gmail使用Apache Commons Email發送電子郵件。問題是什麼?無法通過gmail使用Apache Commons Email發送電子郵件。

Email email = new SimpleEmail(); 
     email.setHostName("smtp.gmail.com"); 
     email.setSmtpPort(587); 
     email.setTLS(true); 
     email.setAuthenticator(new DefaultAuthenticator("[email protected]", "mypassword")); 
     email.setFrom("[email protected]"); 
     email.setSubject("TestMail"); 
     email.setMsg("This is a test mail ... :-)"); 
     email.addTo("[email protected]"); 
     email.send(); 

錯誤:

Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:587 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242) 
    at org.apache.commons.mail.Email.send(Email.java:1267) 
    at MailService.sendSimpleMail(MailService.java:51) 
    at MailService.main(MailService.java:17) 
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; 
    nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 
    at javax.mail.Service.connect(Service.java:310) 
    at javax.mail.Service.connect(Service.java:169) 
    at javax.mail.Service.connect(Service.java:118) 
    at javax.mail.Transport.send0(Transport.java:188) 
    at javax.mail.Transport.send(Transport.java:118) 
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232) 
    ... 3 more 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) 
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 
    at java.net.Socket.connect(Socket.java:519) 
    at java.net.Socket.connect(Socket.java:469) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359) 
    ... 10 more 

回答

3

What is the problem?

Could not connect to SMTP host 

(直接從你似乎已經忽略了異常輸出複製)

1

請將email.setDebug(真); 並用這個命令,你可以知道你有什麼確切的問題 也許你的用戶名和密碼錯誤

-1

如果您使用的是常規Gmail帳戶或谷歌Apps的電子郵件帳戶,有可能的Gmail它阻止當前域。請確保您有足夠的權限從其他應用程序發送郵件。

相關問題