2016-03-10 44 views
1

我有一個Maven項目,使用JSF 2.2,Tomcat 7和使用Apache Commons發送電子郵件。org.apache.commons.mail.EmailException:將電子郵件發送到以下服務器失敗:smtp.gmail.com:465

這裏是我的代碼

try { 
    // Create the email message 
    HtmlEmail email = new HtmlEmail(); 
    email.setSmtpPort(465); //email.setSslSmtpPort("465"); 
    email.setSSLOnConnect(true); 
    email.setHostName("smtp.gmail.com"); 
    email.addTo("[email protected]", "test"); 
    email.setFrom(getEmail(), getName()); 
    email.setSubject(getSubject()); 
    email.setHtmlMsg("<html>Test</html>"); // set the html message 
    email.setTextMsg(getText());// set the alternative message 
    email.send();// send the email 
} catch (EmailException e) { 
    logger.error("Exception sending email: ", e); 
} catch (Exception ex) { 
    logger.error("Exception sending email: ", ex); 
} 

當我試圖給Tomcat 7運行的代碼,我得到了以下異常:

org.apache.commons.mail.EmailException:發送電子郵件向 以下服務器失敗:smtp.gmail.com:465

+0

標準javax.mail API與HtmlEmail沒有任何關係。它屬於Apache Commons。 – Tiny

回答

0

這將是因爲SMTP中繼需要身份驗證,則必須升ogin與Gmail用戶/通行證,然後才能使用中繼。

我以前從未使用過普通郵件,但經過一些Google搜索後,i found this通過Gmail發送郵件。

HtmlEmail email = new HtmlEmail(); 

String authuser = "user"; 
String authpwd = "pass"; 

email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd)); 

email.setHostName("smtp.gmail.com"); 

// properties to configure encryption 
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"); 
+0

謝謝,我改變了代碼,但現在我得到的excpetion _org.apache.commons.mail.EmailException:發送電子郵件到以下服務器失敗:smtp.gmail.com:25_ – snears

相關問題