HOW TO SEND EMAIL USING JAVA MAIL FROM GMAIL?

We can send email using Java Mail API library from Gmail.
Here is the very basic program to explain How to send email using Java Mail API library from Gmail.



Java Source code:

package com.helloselenium.selenium.test;

import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

public class SendEMailUsingJavaMailFromGmail{

public static void main(String[] args) {

    final String username = "testusername@gmail.com"; //change to your Gmail username
    final String password = "testpassword"; //change to your Gmail password
    final String from = "test.from.email@helloselenium.com"; //change to from email address
    final String to = "test.to.email@helloselenium.com"; //change to to email address
    final String cc = "test.cc.email@helloselenium.com"; //change to cc email address
    final String bcc = "test.bcc.email@helloselenium.com"; //change to bcc email address
    final String subject = "Test Email from Hello Selenium"; //change to your subject
    final String msg = "Test Email from Hello Selenium to learn the automation of email message sending using Java Mail API from Gmail."; //change to your message

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        //below code only requires if your want cc email address
        message.setRecipients(Message.RecipientType.CC,
                InternetAddress.parse(cc));
        //below code only requires if your want bcc email address
        message.setRecipients(Message.RecipientType.BCC,
                InternetAddress.parse(bcc));
        message.setSubject(subject);
        message.setText(msg);

        System.out.println("Sending");

        Transport.send(message);

        System.out.println("Done");

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

Detailed explanation for the above program is as follows:




Following code is the required packages for Java and Java Mail API library.
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;
Following code is to declare required variable for username, password, to, cc, bcc, etc.
final String username = "testusername@gmail.com"; //change to your Gmail username
    final String password = "testpassword"; //change to your Gmail password
    final String from = "test.from.email@helloselenium.com"; //change to from email address
    final String to = "test.to.email@helloselenium.com"; //change to to email address
    final String cc = "test.cc.email@helloselenium.com"; //change to cc email address
    final String bcc = "test.bcc.email@helloselenium.com"; //change to bcc email address
    final String subject = "Test Email from Hello Selenium"; //change to your subject
    final String msg = "Test Email from Hello Selenium to learn the automation of email message sending using Java Mail API from Gmail."; //change to your message
Following code is to setup the Gmail server with SMTP details.
Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");
Following code is to get the session of Gmail account using provided username and password.
Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
Following code is to set from email address of email message.
Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
Following code is to set to email address of email message.
message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));



Following code is to set cc email address of email message.
message.setRecipients(Message.RecipientType.CC,
                InternetAddress.parse(cc));
Following code is to set bcc email address of email message.
message.setRecipients(Message.RecipientType.BCC,
                InternetAddress.parse(bcc));
Following code is to set subject of email message.
message.setSubject(subject);
Following code is to set message text of email message.
message.setText(msg);
Following code is to send email message.
Transport.send(message);


Post a Comment

1 Comments


What would you like to add in my list? I look forward to reading your comments below.