2016-03-10 48 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

相关问题