2012-12-17 28 views
1

我使用Apache公共邮件发送电子邮件。不幸的是我得到以下异常:Java电子邮件 - 异常服务器不可信

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("[email protected]", "user"); 
     mail.setFrom("[email protected]"); 
     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。

我很感激任何帮助。

+0

我想重新制定我的问题,我怎么能设置“阿帕奇.commons.mail“使用我的Glassfish邮件会话?是否有可能做一个像mail.session.jndi.name = mail/MailSession的引用,但在哪里? Javax.mail与glassfish协同工作,我甚至不需要进行身份验证。 – albatros

回答

2

setTLS(传输层安全性)为false。如果你不使用。如果它是真的,它预计是mail.xxx.com可信服务器。

mail.setTLS(false);

+0

..当我禁用TLS我得到以下错误: – albatros

+0

'由...引起:javax.mail.SendFailedException:无效的地址; 嵌套的异常是: com.sun.mail.smtp.SMTPAddressFailedException:554 5.7.1 <[email protected]>:中继访问被拒绝 – albatros

+0

您发送到xxx.com服务器的消息不会发送给用户在xx.com域中,以便xxx.com服务器需要将消息转发给另一台服务器,并拒绝这样做。可能是因为你没有通过服务器认证。检查调试输出以查看身份验证是否正在发生。如果不是,请尝试更改属性设置以使用字符串“true”而不是布尔常量。 –

3

终于解决了。我试图在我的交换服务器上进行身份验证。 使用下面的代码:如果您使用Spring-Boot-Mail

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

,尝试添加此行到您.properties文件:

spring.mail.properties.mail.smtp.ssl.trust=smtp.mandrillapp.com 
相关问题