Sending automated emails from Java mail API is very easy. But everyone face issue with performance of that code.That generally takes near 10 sec to complete sending task. But we can run email thread in backend and speedup performace of our code.
This will greatly help in improving performance of our code.

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 GmailRunnable implements Runnable {
    private String to = "";
    private String cc = "";
    private String bcc = "";
    private String subject = "";
    private String message = "";
    public void run() {
    public void setParameters(String to, String cc, String bcc, String subject,
            String message) { = to; = cc;
        this.bcc = bcc;
        this.subject = subject;
        this.message = message;
    private void sendMail() {
        try {
            Session session = getSession();
            Message mailMessage = new MimeMessage(session);
    //      mailMessage.setFrom(new InternetAddress(from));
            // Set the mail recipients
            setRecipients(mailMessage, to, cc, bcc);
            mailMessage.setSentDate(new Date());
        } catch (Exception e) {
    private Session getSession() {
        String host = "";// set ip address (server ip for
        final String userName = "username";// put username here. dont put
                                            // only username.
        final String password = "password";// put pwd here
        final String smtpPort = "465"; // put port number.
        final String sslFactory = "";
        Properties props = System.getProperties();
        props.put("", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", smtpPort);
        props.put("mail.smtp.socketFactory.port", smtpPort);
        props.put("mail.smtp.socketFactory.class", sslFactory);
        props.put("mail.smtp.socketFactory.fallback", "false");
        Session session = Session.getInstance(props, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
        return session;
    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) {
    public static void main(String[] args) {
        GmailRunnable gmailRunnable = new GmailRunnable();
        gmailRunnable.setParameters("", "","","first call", "hi this is first call");
        Thread t = new Thread(gmailRunnable);

The following two tabs change content below.
He is founder and CTO of 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.