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/
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
@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
.– Fernando Leal
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
@felipearon, what becomes helo?
– Fernando Leal
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).
– felipearon