Java邮件 - 异常服务器不受信任

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”使用另一种方法发送电子邮件。但我想转向使用apache。

非常感谢任何帮助。


我想重新表达我的问题,如何设置“apache.commons.mail”来使用我的Glassfish邮件会话?是否可以像mail.session.jndi.name=mail/MailSession这样进行引用,但在哪里进行设置?javax.mail在glassfish上运行良好,我甚至不需要进行身份验证。 - albatros
3个回答

4

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

mail.setTLS(false);


Caused by: javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <user@server.com>: Relay access denied - albatros
您发送给 xxx.com 服务器的消息未寻址到 xx.com 域中的用户,因此 xxx.com 服务器需要将该消息转发到另一个服务器,但它拒绝这样做。可能是因为您尚未对服务器进行身份验证。请检查调试输出以查看是否正在进行身份验证。如果没有,请尝试更改属性设置,使用字符串“true”而不是布尔常量。 - Bill Shannon
我尝试了所有的方法:使用字面/布尔真属性禁用/启用身份验证。我启用了调试模式,但是我没有得到更多的信息。抛出的错误仍然与上面相同! - albatros
使用“javax.mail”,我根本不需要进行身份验证,我在Glassfish v3.1.2上设置了一个Javamail会话。 - albatros

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);

2
如果您正在使用Spring-Boot-Mail,请尝试将以下行添加到您的.properties文件中:
spring.mail.properties.mail.smtp.ssl.trust=smtp.mandrillapp.com

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接