Tuesday, June 19, 2007

Java mail SMTP sample code

This sample code demonstrates the minimum Java code for using regular SMTP service and GMail SMTP service.

First of all, we download the Java Activation Framework and JavaMail packages.

Sample code is followed:

===============================================================

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class AlarmMail {
public void sendGMail() {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

// Get a Properties object, and set the parameters of mail service
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "shuzhanqiang@gmail.com";
final String password = "******";
Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

session.setDebug(true);

// -- Create a new message --
Message msg = new MimeMessage(session);

// -- Set the FROM, TO, and Mail contain fields --
try {
msg.setFrom(new InternetAddress(username));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
"shu_zhq@yahoo.com", false));
msg.setSubject("Hello");
msg.setText("Hello");
msg.setSentDate(new Date());
System.out.println("Start sending");
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();

} catch (MessagingException e) {
e.printStackTrace();

}

System.out.println("Message sent.");
}

private String mailhost = "mail.abc.com";
private String user = "qiang@abc.com";
private String password = "123456";
private String from, to, cc, bcc;
private String subject;
private String text;
private String mailer = "smtpsend";
private boolean ssl = false;
private boolean verbose;

public void sendMsg() {
try {
InternetAddress[] address = null;

// Get a Properties object, and set the parameters of mail service
Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.setProperty("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
javax.mail.Session sessmail = javax.mail.Session.getInstance(props);
sessmail.setDebug(true);
MimeMessage msg = new MimeMessage(sessmail);
msg.setFrom(new InternetAddress(user));

address = InternetAddress.parse(to, false);
msg.setRecipients(Message.RecipientType.TO, address);

msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(text, "UTF-8");
Transport transport = sessmail.getTransport("smtp");
transport.connect(mailhost, user, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (AddressException e) {
e.printStackTrace();

} catch (NoSuchProviderException e) {
e.printStackTrace();

} catch (MessagingException e) {
e.printStackTrace();

}

}


public static void main(String[] args) {
AlarmMail am = new AlarmMail();
am.setFrom("shu_zhq@gmail.com");
am.setTo("shu_zhq@yahoo.com");
am.setSubject("Help");
am.setText("Help.");
am.sendMsg();

am.sendGMail();
}

public String getBcc() {
return bcc;

}

public void setBcc(String bcc) {
this.bcc = bcc;

}

public String getCc() {
return cc;

}

public void setCc(String cc) {
this.cc = cc;

}

public String getFrom() {
return from;

}

public void setFrom(String from) {
this.from = from;

}

public String getMailer() {
return mailer;

}

public void setMailer(String mailer) {
this.mailer = mailer;

}

public String getMailhost() {
return mailhost;

}

public void setMailhost(String mailhost) {
this.mailhost = mailhost;

}

public String getPassword() {
return password;

}

public void setPassword(String password) {
this.password = password;

}

public boolean isSsl() {
return ssl;

}

public void setSsl(boolean ssl) {
this.ssl = ssl;

}

public String getSubject() {
return subject;

}

public void setSubject(String subject) {
this.subject = subject;

}

public String getText() {
return text;

}

public void setText(String text) {
this.text = text;

}

public String getTo() {
return to;

}

public void setTo(String to) {
this.to = to;

}

public String getUser() {
return user;

}

public void setUser(String user) {
this.user = user;

}

public boolean isVerbose() {
return verbose;

}

public void setVerbose(boolean verbose) {
this.verbose = verbose;

}

}

1 comment:

Anonymous said...

Well written article.