I have written a simple java program to connect to TLS enabled SMTP server as below
props.put("mail.smtp.host",GMAIL SMTP SERVER );
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.port", 587);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
loSession = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("ak@gmail.com",
GMAIL ACCOUNT PASSWORD)
Later setting the from email address as
InternetAddress loFromIA = new InternetAddress( "ashu@gmail.com");
loMsg.setFrom( loFromIA );
Now once i executed the code then i am receiving email from address ak@gmail.com which is SMTP server account address instead of ashu@gmail.com. I have explicitly set the from address as ashu@gmail.com but Gmail is overriding it. Same is happening with Microsoft server also.
Can someone suggest what needs to be fixed in code ? Also this happens after adding startTLS in code. without starttls it was working fine.