albatros
albatros

Reputation: 31

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.

Upvotes: 3

Views: 12588

Answers (3)

Sebastian Ullrich
Sebastian Ullrich

Reputation: 1037

If you're using Spring-Boot-Mail, try adding this line to you .properties file:

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

Upvotes: 2

Chandan Singh
Chandan Singh

Reputation: 27

Finally Solved. I tried everything to get authenticated at my exchange server. use the code below:

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

Upvotes: 3

someone
someone

Reputation: 6572

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

Upvotes: 4

Related Questions