We need JAVA Mail so many times. It is powerful feature of java which is everyday used for auto generated Java mails.In automation we use this feature alot.
I wrote one sample program to demonstrated that.

you need mail.jar that you can download from here. click here

i wrote one MailerService class,in which we have sendMail public method that you can use to send email.
This class first create session to send email then it creates message to send.

Find sample code heree.

click here to full page preview.

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Authenticator; 
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage;

public class MailerService {

* @param from
* @param to
* @param cc
* @param bcc
* @param subject
* @param message
public void sendMail(String from, String to, String cc, String bcc,
String subject, String message) {

try {
Session session = getSession(); //create session.
Message mailMessage = new MimeMessage(session); //create message
mailMessage.setFrom(new InternetAddress(from));

// Set the mail recipients
setRecipients(mailMessage, to, cc, bcc);


mailMessage.setSentDate(new Date());
} catch (Exception e) {


* Gets the session.
* @return the session
private Session getSession() {

String host = "";//set host ip
final String userName = "username";//put username here. dont put @domain.com only username.
final String password = "password";//put pwd here

Properties properties = System.getProperties();
properties.put("mail.smtp.host", host);
properties.put("mail.user", userName);
properties.put("mail.debug", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");

Session session = Session.getInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);

return session;

* Sets the recipients.
* @param mailMessage
* the mail message
* @param to
* the to
* @param cc
* the cc
* @param bcc
* the bcc
* @throws AgMailException
* the ag mail exception
private void setRecipients(Message mailMessage, String to, String cc,
String bcc) throws Exception {

if (mailMessage != null) {
try {
if (to != null) {
List toList = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(to, ",");
while (tokenizer.hasMoreTokens()) {
toList.add(new InternetAddress(tokenizer.nextToken()));
(InternetAddress[]) toList
.toArray(new InternetAddress[0]));
// set CC recipients
if (cc != null) {
List ccList = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(cc, ",");
while (tokenizer.hasMoreTokens()) {
ccList.add(new InternetAddress(tokenizer.nextToken()));
(InternetAddress[]) ccList
.toArray(new InternetAddress[0]));

// set BCC recipients
if (bcc != null) {
List bccList = new ArrayList();
StringTokenizer tokenizer = new StringTokenizer(bcc, ",");
while (tokenizer.hasMoreTokens()) {
bccList.add(new InternetAddress(tokenizer.nextToken()));
(InternetAddress[]) bccList
.toArray(new InternetAddress[0]));
} catch (MessagingException e) {
} catch (Exception e) {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
MailerService mail = new MailerService();
//cc & bcc are comma separated
mail.sendMail("sender email id",
"send to", "cc",
"bcc", "first mail",
"hi, this is my first mail ");



If this sample code help you, kindly leave your feedback.help me make it better.

The following two tabs change content below.
He is founder and CTO of Habilelabs.io. He senior software architect. He is guru of various Mobile and web technologies including Node.js, Angular.js, Meteor.js. He is founder member of habilelabs Pvt. Ltd.