Tuesday, March 2, 2010

Send Mail in Java using SMTP

I was trying to send a mail from my JSP page. I had tried every way to make that code work. Later I found that I have to set  Mail Authentication, which lead me to solution.
Here is my SendMail cass, which you can implement in any Java application to send mail.
I have used a new Thread to send the mail, unless you do this the Thread which the mail sender was called will wait until the mail is sent due to this reason the whole program might halt for awhile
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class MailSender {

private String email = "jason@gmail.com";
private String password = "****";
private String host = "smtp.gmail.com";
private String port = "465";
private String from = "jason@gmail.com"; // if needed this can be used to set the from e-mail address
private String toAddresses = "abc@gmail.com,cbn@yahoo.com";
private String ccAddresses = "aft@hotmail.com,abc@gamil.lk";
private String bccAddresses = "";

public void sendEmail(final String subject, final String message) {
new Thread() {
public void run() {
sendMail(email, password, host, port, "true",
"true", true, "javax.net.ssl.SSLSocketFactory", "false", toAddresses, ccAddresses, bccAddresses,
subject, message);

public synchronized boolean sendMail(String userName, String passWord, String host, String port, String starttls,
String auth, boolean debug, String socketFactoryClass, String fallback,
String to,
String cc, String bcc, String subject, String text) {
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable", starttls);
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.debug", Boolean.toString(debug));
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", socketFactoryClass);
props.put("mail.smtp.socketFactory.fallback", fallback);

try {
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
for (String aTo : to.split(",")) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(aTo));
if (!cc.equals("")) {
for (String aCc : cc.split(",")) {
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(aCc));
if (!bcc.equals("")) {
for (String aBcc : bcc.split(",")) {
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(aBcc));
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
return true;
catch (Exception e) {
return false;

class GenerateMailTest {
public static void main(String[] args) {
final MailSender sender = new MailSender();
sender.sendEmail("This is a test", "This is the content of test mail");

No comments:

Post a Comment