Below is the email code to send Reports after execution and one method put is used, that's generating error. How to defined that put method to successful Test execution and test report sends automatically, and also it throws error for getDefaultInstance for Session objects Refer below code:

package com.deere.gmsr;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.sql.DataSource;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class Utility {

            // Create object of Property file
            PropertiesFileReader properties = new PropertiesFileReader();

            // this will set host of server- you can change based on your requirement 
            properties.put("mail.smtp.host", "smtp.gmail.com");

            // set the port of socket factory 
            properties.put("mail.smtp.socketFactory.port", "465");

            // set socket factory

            // set the authentication to true
            properties.put("mail.smtp.auth", "true");

            // set the port of SMTP server
            properties.put("mail.smtp.port", "465");

            // This will handle the complete authentication
            Session session = Session.getDefaultInstance(properties,

                    new javax.mail.Authenticator() {

                        protected PasswordAuthentication getPasswordAuthentication() {

                        return new PasswordAuthentication("add your email", "add your password");



            try {

                // Create object of MimeMessage class
                Message message = new MimeMessage(session);

                // Set the from address
                message.setFrom(new InternetAddress("abc@gmail.com"));

                // Set the recipient address

                            // Add the subject link
                message.setSubject("GMSR Automation Framework");

                // Create object to add multimedia type content
                BodyPart messageBodyPart1 = new MimeBodyPart();

                // Set the body of email
                messageBodyPart1.setText("This is message body");

                // Create another object to add another content
                MimeBodyPart messageBodyPart2 = new MimeBodyPart();

                // Mention the file which you want to send
                String filename = "D:\\GMSR-Automation";

                // Create data source and pass the filename
                FileDataSource source = new FileDataSource(filename);

                // set the handler
                messageBodyPart2.setDataHandler(new DataHandler(source));

                // set the file

                // Create object of MimeMultipart class
                Multipart multipart = new MimeMultipart();

                // add body part 1

                // add body part 2

                // set the content

                // finally send the email

                System.out.println("=====Email Sent=====");

            } catch (MessagingException e) {

                throw new RuntimeException(e);




I have tried same thing below is my code which is working fine for me, hope it help you-

 private static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {

Properties props = System.getProperties();

String host = "smtp.gmail.com";

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", host);

props.put("mail.smtp.user", from);

props.put("mail.smtp.password", pass);

props.put("mail.smtp.port", "587");

props.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);

MimeMessage message = new MimeMessage(session);

try {

  //Set from address

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

//Set subject



BodyPart objMessageBodyPart = new MimeBodyPart();

objMessageBodyPart.setText("Please Find The Attached Report File!");

Multipart multipart = new MimeMultipart();


objMessageBodyPart = new MimeBodyPart();

//Set path to the pdf report file

String filename = System.getProperty("user.dir")+"\\AddCustomer.pdf";

//Create data source to attach the file in mail

DataSource source = new FileDataSource(filename);

objMessageBodyPart.setDataHandler(new DataHandler(source));




Transport transport = session.getTransport("smtp");

transport.connect(host, from, pass);

transport.sendMessage(message, message.getAllRecipients());



catch (AddressException ae) {



catch (MessagingException me) {




Now use this method in your BaseTest class-

  public void tearDown(){

      sendPDFReportByGMail("FromEmail@gmail.com", "123ABC", "toEmail@gmail.com", "PDF Report", "");


Hope you have all JAR files.

| improve this answer | |
  • DataSource source = new FileDataSource(filename); I am getting error for DataSource once its gets changed to FileDataSource its vanishes all errors, jars used <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.5</version> </dependency> – jay May 6 '19 at 10:39
  • ok, so after correction do you get anything in logs? or everything is working? – Upkar Singh May 6 '19 at 10:44
  • no logs are generating – jay May 6 '19 at 11:16
  • are you getting mails? – Upkar Singh May 6 '19 at 11:50
  • No no emails are coming – jay May 6 '19 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.