Send email with STARTTLS porta 587

Asked

Viewed 1,649 times

3

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

For testing I set up the following code (I changed the customer 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;
    }

But when trying to connect to 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 believe it is some property that lacks setar (or not), but I do not know how to treat this error. What to do in this case?

I did the same test using lib javamail but got the same error: https://code.google.com/p/javamail-android/

3 answers

2

Comparing here to one of mine, configured with Javamail which has been working perfectly for some time, to TLS, the properties it has of different are:

In my has:

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

and has not:

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

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

In the Javamail documentation says the following about the use of property mail.smtp.ssl.trust:

If set, and a socket Factory hasn’t 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 Certificate the server presents.

That briefly informs that this property defines the smtp specified with "reliable", dispensing with the use of safety certificate with specified.

I’m not sure, but it seems to bring vulnerability to the implementation, since you are using a smtp with security certificates and ignoring this (it is also necessary to note that this property will not cause the increase in the likelihood of your email being tagged with span by some email servers).

Try the changes I suggested and see if it works.

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

  • Great Fernando, great answer. I made the change in the properties, and doing a test with my email from kinghost, which uses TLS also, worked. The ONLY DIFFERENCE is that my host is "smtp.dominio" and the client is "mail.dominio". Because it is "mail" do I need to declare this in property? How?

  • @felipearon, which property do you want to declare this "mail.dominio" or "smtp.dominio"? I didn’t understand your doubt? In "mail.transport.Protocol"? If it is I believe that it makes no difference, it can remain smtp.

  • was a question whether or not to declare "mail" or "smtp". From what I saw just remain "smtp". HELO boot mode makes some difference in connection?

  • @felipearon, what becomes helo?

  • things are clearing up more. From what I understand, the client server is local in the company. It uses TLS encryption with a certified that is not standard, because of that I am not getting the connection, different from the certificate of kinghost (which I did the sending test and is working).

2


Try to change that

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

by your client’s smtp, as your client’s smtp should not be smtpserver sure?

Ex:

     props.put("mail.smtp.ssl.trust", "smtp.meucliente.com.br");
  • I refined the email sending class, and setando the property above worked perfectly!

1

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

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

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

So the two rules alone or together solve this kind of problem.


Reference (in English):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.