I am trying to send an email from my program through a TLS connection. Here is my code
final String username = "XXXXXX";
final String password = "XXXXX";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "mail.xxxx.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to_address));
message.setSubject("Test Mail");
message.setText("TestMail ");
Transport.send(message)
My email gateway has incoming Mail settings with SSL enabled and outgoing with TLS enabled on port 587. I am able to configure this settings in outlook and it's working fine. But through my java program it's saying "Connection Refused". Help Appreciated!
I used the InstallCert program to import the certicate to generate jssecacerts file and I added the file to my /jre/lib/security/ path. here is my working code
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "XXXXXX");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", true);
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.put("mail.smtp.quitwait", "false");
properties.put("mail.smtp.auth", "true");
MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message. The mail. mime. address. strict session property controls the parsing of address headers.
If the connect() method does not explicitly specify a port, specify the SMTP server port to connect to. The default is 25. 465. mail.smtp.socketFactory.port. Specifies the port to connect to when you use the specified socket factory.
JavaMail now supports the use of SSL/TLS to establish secure connections to access mail servers. In order to simplify secure access, two methods of enabling SSL secure connection are provided: Configure connection properties.
SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks.
You need to use the smtps
protocol instead of smtp
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.starttls.enable","true");
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.host", "mail.xxxx.com");
props.put("mail.smtps.port", "587");
You can also try to set the protocol specificly for rfc822, this helps some times
props.put("mail.transport.protocol.rfc822", "smtps");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With