Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java email - Exception Server is not trusted

I'm using apache commons mail to send emails. Unfortunately I'm getting following Exception:

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

And here is my java code:

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

I've already read many threads and tried many hints. I'm stuck here! I should mention that I'm able to send emails with another method using "javax.mail". But I'd like to move to apache.

I'm thankful for any help.

like image 921
albatros Avatar asked Sep 03 '26 16:09

albatros


1 Answers

setTLS (Transport layer security) to false. If you are not using. When it is true its expected to be mail.xxx.com trusted server.

mail.setTLS(false);

like image 58
someone Avatar answered Sep 05 '26 06:09

someone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!