3

我正在使用 apache commons mail 发送电子邮件。不幸的是,我遇到了以下异常:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : mail.xxx.com:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
    at org.apache.commons.mail.Email.send(Email.java:1267)
    at com.xxx.UserBean.sendMail(UserBean.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
.
.
.
Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    java.io.IOException: Server is not trusted: mail.xxx.com
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1880)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:648)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
    ... 85 more
Caused by: java.io.IOException: Server is not trusted: mail.xxx.com
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:443)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1875)
    ... 92 more

这是我的java代码:

    public void sendMail(){
    try {  
        SimpleEmail mail = new SimpleEmail();
        mail.setTLS(true); 
        mail.setAuthenticator(new DefaultAuthenticator("user","xyxyxyxy"));
        mail.setHostName("mail.xxx.com");            
        mail.getMailSession().getProperties().put("mail.smtps.auth", true);
        mail.getMailSession().getProperties().put("mail.debug", true);
        mail.getMailSession().getProperties().put("mail.smtps.port", "587");
        mail.getMailSession().getProperties().put("mail.smtp.starttls.enable", true);
        mail.getMailSession().getProperties().put("mail.smtp.ssl.trust", "smtpserver");

        mail.addTo("user@server.com", "user");
        mail.setFrom("info@xxx.com");
        mail.setSubject("Bla Bla");
        mail.setMsg("Bla Bla again");
        mail.send();
                } catch (Exception ex) {   
        logger.info("Senden der Email ist gescheitert!", ex);            
    }
 }

我已经阅读了很多线程并尝试了很多提示。我被困在这里了!我应该提一下,我可以使用“javax.mail”通过另一种方法发送电子邮件。但我想搬到阿帕奇。

我很感激任何帮助。

4

3 回答 3

4

将TLS(传输层安全)设置为 false。如果你不使用。当它是 true 时,它​​应该是 mail.xxx.com 受信任的服务器。

mail.setTLS(false);

于 2012-12-17T16:55:57.253 回答
3

终于解决了。我尝试了一切以在我的交换服务器上进行身份验证。使用下面的代码:

Properties props = (Properties)System.getProperties().clone();
props.put("mail.smtp.host", host);
props.setProperty("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);

//Bypass the SSL authentication
props.put("mail.smtp.ssl.enable", false);
props.put("mail.smtp.starttls.enable", false);
于 2013-03-26T11:57:19.593 回答
2

如果您正在使用Spring-Boot-Mail,请尝试将此行添加到您的.properties文件中:

spring.mail.properties.mail.smtp.ssl.trust=smtp.mandrillapp.com
于 2017-08-06T17:52:30.677 回答