How to Send Email on Java Application using JavaMail API
This article explain how to use JavaMail API for sending email through our Java application. This will cover simple email, HTML email, and email with attachments.
The required JavaMail API can found here. Put it on a directory accessible by CLASSPATH setting. It’s a good idea to read the JavaMail FAQ.
Simple Email
This test application is used for sending simple email.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
class SimpleMail {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mail.server.com");
props.setProperty("mail.user", "mailuser");
props.setProperty("mail.password", "mailpwd");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing simple email by JavaMail API");
message.setContent("This is just a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("rhoma@irama.org"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
HTML Email
This application is a sample of sending HTML email using JavaMail API.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
class SimpleHTMLMail {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mail.server.com");
props.setProperty("mail.user", "mailuser");
props.setProperty("mail.password", "mailpwd");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing JavaMail HTML email");
message.setContent
("This is just a test <b>HOW TO<b> send HTML email", "text/html; charset=ISO-8859-1");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("rhoma@irama.org"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
Email with Attachments
This is a sample application for sending email with attachment using JavaMail API. Note that you will also need additional Jar for this, the Java Activation Framework, JAF, which can be downloaded here.
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Properties;
class SimpleMailWithAttachment {
public static void main(String[] args) throws Exception{
boolean debug = false;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mymail.server.org");
props.setProperty("mail.user", "emailuser");
props.setProperty("mail.password", "");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(debug);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail with attachment");
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "text/html");
MimeBodyPart attachFilePart = new MimeBodyPart();
FileDataSource fds =
new FileDataSource("SimpleMailWithAttachment.java");
attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
message.setContent(mp);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("elvis@presley.org"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}



I usually don