Website Design United States, Website Design California, Website Designing United States, Website Designing California

Send Email using JavaMail API - Java

G Rowland

Sends an email message by means of the JavaMail API

// by Rowland
// uses JavaMail to send a message

// NOTE: Needs mail.jar and activation.jar in CLASSPATH to run
// Needs mail.jar to compile

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/** MailNotifier - a utility class to send a SMTP mail notification **/
public class MailNotifier {
final String localhost;
final String mailhost;
final String mailuser;
final String email_notify;
protected Session session= null;

public MailNotifier(String _localhost, String _mailhost, String _mailuser, String _email_notify) {
localhost= _localhost;
mailhost= _mailhost;
mailuser= _mailuser;
email_notify= _email_notify;

public void send(String subject, String text) throws Exception {
send(email_notify, subject, text);
public void send(String _to, String subject, String text) throws Exception {
if (session== null) {
Properties p = new Properties();
p.put("", mailhost);
p.put("mail.user", mailuser);
session = Session.getDefaultInstance(p, null);

// Try to fake out and get working EHLO:
Properties properties = session.getProperties();
String key= "mail.smtp.localhost";
String prop= properties.getProperty(key);
if (prop== null) properties.put(key, localhost);
else System.out.println(key+ ": "+ prop);

MimeMessage msg = new MimeMessage(session);
Address fromAddr = new InternetAddress(mailuser);
Address toAddr = new InternetAddress(_to);
msg.addRecipient(Message.RecipientType.TO, toAddr);
// Note: will use results of getLocalHost() to fill in EHLO domain

* Get the name of the local host, for use in the EHLO and HELO commands.
* The property mail.smtp.localhost overrides what InetAddress would tell
* us.
Adapted from
public String getLocalHost() {
String localHostName= null;
String name = "smtp"; // Name of this protocol
try {
// get our hostname and cache it for future use
if (localHostName == null || localHostName.length() <= 0)
localHostName = session.getProperty("mail." + name + ".localhost");
if (localHostName == null || localHostName.length() <= 0)
localHostName = InetAddress.getLocalHost().getHostName();
} catch (Exception uhex) {
return localHostName;

/** main() for testing pursposes **/
public static void main(String args[]) {
// Adapt to your needs:
String localhost= "";
String mailhost= "";
String mailuser= "";
String email_notify= "";

MailNotifier mn= new MailNotifier(localhost, mailhost, mailuser, email_notify);
try {
mn.send("Test", "Test body from main()");
} catch (Exception E) {


© 2008-2009