Email code modifications needed

  • 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("", "");
                // 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(""));
                    // 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 = "";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("", 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("", "123ABC", "", "PDF Report", "");

    Hope you have all JAR files.

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2