I am using simple-java-mail API which is a wrapper on top of JavaMail API. I am sending emails with my Gmail account credentials. I am mentioning what I am able to do and what not with this.
I am able to send emails perfectly with the following settings and properties.
boolean enableSSL = true;
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.ssl.enable", enableSSL);
If(enableSSL == true)
transportStrategy = TransportStrategy.SMTP_SSL;
I am not able to send emails with plain SMTP.
boolean enableSSL = false;
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", 25);
if(enableSSL == false)
transportStrategy = TransportStrategy.SMTP_PLAIN;
I am not able to send emails with TLS settings.
boolean enableSSL = true;
boolean enableTLS = true;
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.ssl.enable", enableSSL);
props.put("mail.smtp.starttls.enable", true);
If(enableSSL == true && enableTLS == true)
transportStrategy = TransportStrategy.SMTP_TLS;
What should I do apart from these configuration properties? with settings smtp.gmail.com:25, code is not working at all. With smtp.gmail.com:465 and enableSSL = true, code is working like a charm.
But TLS is not working. I am attaching what error I am getting in all the 3 cases.
Thanks everyone. Issue was with my company's network. Network team updated that outgoing SMTP requests are blocked on port 25 and 587. With my personal wifi network, everything working like charm.
So For GMAIL, my settings are as follows for each of the 3 ports provided by GMAIL. And I found there is no need to set the session properties explicitly with simple-java-mail API.
boolean enableSSL = false;
boolean enableTLS = true;
String host = "smtp.gmail.com";
int port = 25;
String user = "<gmail_account_username>";
String password = "<gmail_account_password>";
ServerConfig serverConfig = new ServerConfig(host, port, user, password);
TransportStrategy transportStrategy = TransportStrategy.SMTP_TLS;
Mailer mailer = new Mailer(serverConfig, transportStrategy);
boolean enableSSL = true;
boolean enableTLS = false;
String host = "smtp.gmail.com";
int port = 465;
String user = "<gmail_account_username>";
String password = "<gmail_account_password>";
ServerConfig serverConfig = new ServerConfig(host, port, user, password);
TransportStrategy transportStrategy = TransportStrategy.SMTP_SSL;
Mailer mailer = new Mailer(serverConfig, transportStrategy);
boolean enableSSL = false;
boolean enableTLS = true;
String host = "smtp.gmail.com";
int port = 587;
String user = "<gmail_account_username>";
String password = "<gmail_account_password>";
ServerConfig serverConfig = new ServerConfig(host, port, user, password);
TransportStrategy transportStrategy = TransportStrategy.SMTP_TLS;
Mailer mailer = new Mailer(serverConfig, transportStrategy);