Sunday, February 3, 2008

Sending mail using Gmail SMTP server with Java

This is a companion project of my previous experiment with S/Mime encryption. This script is also based on Beanshell and it's purpose is basically to connect to Gmail SMTP servers and deliver the encrypted payload to the message recipients.

There are several examples off the Internet but, somehow, I had to combine the techniques before I finally pulled this off. I've enabled debug, otherwise this post would not have any image at all!

Here's the debug output..


Here's the script..


addClassPath( "./mail.jar" );
addClassPath( "./activation.jar" );

import java.security.Security;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Authenticator;
import javax.mail.internet.MimeMessage;

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
}

public static final String smtpHost = "smtp.gmail.com";
public static final String smtpUsername = "rexjun@gmail.com";
public static final String smtpPassword = "-change-me-";
public static final String smtpPort = "465";

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.put("mail.smtp.user", smtpUsername);
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl","true");

SecurityManager security = System.getSecurityManager();
Authenticator auth = new SMTPAuthenticator();
Session smtpSession = Session.getInstance(props, auth);
smtpSession.setDebug(true);

MimeMessage smtpMessage = new MimeMessage(smtpSession,
new FileInputStream("Encrypted.eml"));
smtpMessage.saveChanges();
smtpMessage.setSentDate(new Date());

Transport tr = smtpSession.getTransport("smtp");
tr.connect(smtpHost, smtpUsername, smtpPassword);
tr.sendMessage(smtpMessage, smtpMessage.getAllRecipients());
tr.close();

6 comments:

$NPR$ said...

Hi,
I want send mail using our own SMTP server (SSL enabled). The code given here is working for Gmail.But its not working for My SMTP server.
I am getting this error:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

Please help me to resolve this issue.

Coboshhh... said...

I have exactly the same problem javax.net.ssl.SSLException: Unrecognized SSL
what should I do?

Legiongrid said...

I made it I just had to comment this line
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

j said...

There is a typo in the key for the auth flag. It should be "mail.smtp.auth."

I needed only the following three properties to make it work -
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");

LittleHelper said...

I've removed to following code and it still works:

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

LittleHelper said...

I've removed to following code and it still works:

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());