【问题标题】:Java email - Exception Server is not trustedJava 电子邮件 - 异常服务器不受信任
【发布时间】:2017-08-07 01:52:30
【问题描述】:

我正在使用 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 配合得很好,我什至不需要进行身份验证。

标签: java jakarta-mail glassfish-3 apache-commons


【解决方案1】:

将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
  • 您发送到 xxx.com 服务器的邮件不是发送给 xx.com 域中的用户,因此 xxx.com 服务器需要将邮件转发到另一台服务器,并且它拒绝要做到这一点。可能是因为您尚未对服务器进行身份验证。检查调试输出以查看是否正在进行身份验证。如果不是,请尝试更改您的属性设置以使用字符串“true”而不是布尔常量。
  • 我什么都试过了;使用文字/布尔 true 属性禁用/启用身份验证。我启用了调试模式,但没有获得更多信息。抛出的错误还是和上面一样!
  • 使用“javax.mail”我根本不需要进行身份验证,我在 Glassfish v3.1.2 上设置了一个 Javamail 会话。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    spring.mail.properties.mail.smtp.ssl.trust=smtp.mandrillapp.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      相关资源
      最近更新 更多