2009-11-23 78 views
6
Email email = new SimpleEmail(); 
String authuser = "[email protected]"; 
String authpwd = "*******"; 
// Very Important, Don't use email.setAuthentication() 
email.setSmtpPort(465); 
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); 
email.setDebug(true); // true if you want to debug 
email.setHostName("smtp.gmail.com"); 

email.getMailSession().getProperties().put("mail.smtp.auth", "true"); 
email.getMailSession().getProperties().put("mail.debug", "true"); 
email.getMailSession().getProperties().put("mail.smtp.port", "465"); 
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465"); 
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false"); 
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true"); 
email.setFrom("[email protected]", "SenderName"); 
email.setSubject("TestMail"); 
email.setMsg("This is a test mail?"); 
email.addTo("[email protected]", "ToName"); 
email.send(); 

和它提供了以下異常發送電子郵件使用共享的電子郵件到Gmail

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465 
+0

你能發佈整個異常堆棧跟蹤嗎?異常的原因可能在其中列出。例外是通用的。這可能是一個簡單的身份驗證失敗,也可能是其他問題。歡呼 – aldrin

回答

14

但這對我來說

Email email = new SimpleEmail(); 
String authuser = "[email protected]"; 
String authpwd = "xxxx"; 
email.setSmtpPort(587); 
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); 
email.setDebug(true); 
email.setHostName("smtp.gmail.com"); 
email.getMailSession().getProperties().put("mail.smtps.auth", "true"); 
email.getMailSession().getProperties().put("mail.debug", "true"); 
email.getMailSession().getProperties().put("mail.smtps.port", "587"); 
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587"); 
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false"); 
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true"); 
email.setFrom("[email protected]", "SenderName"); 
email.setSubject("TestMail"); 
email.setMsg("This is a test mail?"); 
email.addTo("[email protected]", "ToName"); 
email.setTLS(true); 
email.send(); 
+0

嘗試用smtps以及,不起作用 – user217029

+0

也不能與587一起工作..still不起作用 – user217029

+0

再次查看擴展的答案 – jitter

10

工作不要你需要tell Commons Email that you're sending a TLS email

email.setTLS(true); 

在您致電email.send()之前?

我不確定這是否會解決您的問題,因爲我不確定您是否遇到連接到smtp.gmail.com:465的問題或者成功發送給它(錯誤消息/異常就像你提出的那樣是模棱兩可的),但是據我所知,這絕對是缺少的東西。

13

Commons Email用戶指南提供了一個使用SSL的Gmail示例。

https://commons.apache.org/proper/commons-email/userguide.html

SSL/TLS(端口465) - > email.setSSLOnConnect(真);

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

STARTTLS(端口587) - > email.setStartTLSEnabled(真);

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

此代碼適合我!謝謝! – Ascension

+0

我正在獲取類javax.mail.MessagingException:530 5.7.0必須首先發出STARTTLS命令。 g66sm11757390ywh.8 - gsmtp 異常。但是,我更新了與您的代碼相同的代碼。 – Curious