1
votes

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 charm.

But TLS is not working. I am attaching what error I am getting in all the 3 cases.

Thanks,

1
Please make a minimal but working example to demonstrate your problem - in your case the hostname is already wrong. Also "does not work" is no useful error description: please add any errors you get back from the server, like an error message from the server which tells you why it does not accept your connection.Steffen Ullrich
Basic Gmail instructions are in the JavaMail FAQ. If you still can't get it to work, post the exact code you're using along with the JavaMail debug output.Bill Shannon
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(@home), everything working like charm.ramanKC

1 Answers

1
votes

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.

With Port 25

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);

With Port 465

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);

With Port 587

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);