Send e-mail with STARTTLS port 587

3

I need to send an email using the client settings. Host access uses STARTTLS security using port 587.

For testing I put the following code (I changed the client data for security):

try {           
        String host = "200.201.202.203";
        String port = "587";
        String address = "[email protected]";
        String pass = "xxxx";

        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.quitwait", "false");
        props.put("mail.smtp.host", host); 
        props.put("mail.smtp.user", address); 
        props.put("mail.smtp.password", pass); 
        props.put("mail.smtp.port", port); 

        Session session = Session.getDefaultInstance(props, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(address));

        Multipart multiPart=new MimeMultipart();

        InternetAddress toAddress = new InternetAddress("[email protected]"); 
        message.addRecipient(Message.RecipientType.TO, toAddress);

        message.setSubject("Send Auto-Mail"); 
        message.setContent(multiPart); 
        message.setText("Demo For Sending Mail in Android Automatically");

        Transport transport = session.getTransport("smtp");
        transport.connect(host, address, pass);

        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        return true;            
    } catch (Exception e) {
        Log.e(this.getClass().toString(), e.getMessage());
        return false;
    }

However when trying to connect on the host (transport.connect ()) the following error occurs:

  

javax.mail.MessagingException: Could not convert socket to TLS;     nested exception is:       javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

I think it's some missing property (or not), but I'm not sure how to handle that mistake. What to do in this case?

I did the same test using lib javamail but I got the same error: link

    
asked by anonymous 22.07.2014 / 22:32

3 answers

2

Try changing this

 props.put("mail.smtp.ssl.trust", "smtpserver");

by your client's smtp, as your client's SMTP should not be smtpServer right?

Ex:

     props.put("mail.smtp.ssl.trust", "smtp.meucliente.com.br");
    
22.07.2014 / 22:47
2

Comparing here with a mine, configured with JavaMail that has been working perfectly for some time, for TLS , the properties it has are different:

In my case:

props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.quitwait", "false");

and does not have:

props.put("mail.smtp.ssl.trust", "smtpserver");

I believe this mail.smtp.ssl.trust is not a property used for TLS , since it seems to be explicitly a configuration for SSL servers.

At JavaMail Documentation reads as follows on the use of mail.smtp.ssl.trust property:

  

If set, and a socket factory has not been specified, enables use of a MailSSLSocketFactory. If set to "*", all hosts are trusted. If set to a whitespace separated list of hosts, those hosts are trusted. Otherwise, trust depends on the server.

It briefly informs that this property sets the smtp specified to "trusted", dispensing with the use of the security certificate with specified.

  

I'm not sure, but it seems to bring vulnerability to the implementation, since you're using smtp with security certificates and ignoring this (you also need to note if this property will not cause the likelihood of your email being marked with span for some email servers).

Try the changes I've suggested and see if it works.

I recommend observing this post about the difference between < strong> SSL and TLS .

    
22.07.2014 / 22:47
1

The first response from @ user2227682 works perfectly, however as you are using Start TLS there is a more specific property for this, @Fernando Leal said, it is not the ideal rule, though it works.

If you want to use the property below too, it will work perfectly:

session.getProperties().put("mail.smtp.starttls.enable", "true");

Therefore, the two isolated rules or together solves this type of problem.

Reference (in English):

06.07.2018 / 12:57