How to Send Email notification of Test report in Selenium?

If we use continuous integration through Jenkins, then we can easily configure email notification through the pipeline. In that case, we do not require the coding practices. Here we are going to discuss the implementation of send email notification feature in our Selenium project. Therefore, the agenda of this tutorial is about sending test report through email notification. We will implement it through Java programming.

Why do we need to implement send email notification in Selenium?

There are some stakeholders who are not directly accessing our test repository, hence, they can’t access the test execution report directly. Therefore, it is always a good practice to implement this feature in our test framework so that all the stakeholders get notified with the test execution status which contains the test reports.

Pre-requisites for implementation of ‘send email notification’ feature in Selenium

There is some pre-requisite which a developer needs to follow before implementing this feature in the Selenium project.

  • Add JavaMail API JAR file in your Selenium project. Click here to download the JAR file.
  • Alternatively, you can create maven dependency of JavaMail API. Click here to get the Maven dependency of JavaMail API.
  • Get ready with SMTP (Simple Mail Transfer Protocol) hostname and port number

In addition to above Jar files, we need to import some of the Packages, which are listed as below:

import java.util.Properties;
import javax.activation.*;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

Well, these packages are just the part of coding.

How to implement ‘Send email notification’ feature in the Selenium project?

Here are some of the steps which implement this email notification service in your automation project.

At first, we need to identify the list of recipients who are going to receive the email notification, then identify the sender’s email address with their SMTP and host’s port number. We will implement this service after the entire test execution ends so that it could trigger the mail with the segregated test reports. In the previous article, we discussed the technique to generate the PDF report of the test execution and in this article, we will learn the technique to send that PDF report through email. If you haven’t gone through the previous article, then I would like to suggest you one time reading of the tutorial. Click on the link below to read the tutorial of PDF report creation.

Next, we set the SMTP hostname as the property, further, we create a new session to implement the email service. After session set up, we create the instance of MimeMessage() class and we pass the instance of the session in the constructor. Further, we create Internet address of Sender and recipients.

After all these setups, we create the first message body in which we define the subject of the email and email’s body. In the second message body, we insert the test report as an attachment and finally, we combine these message body with MultiPart class.

In the end, we use Transport to authenticate the session and login validation. Once it is authenticated then the user successfully sends the email to the stakeholders.

Here is the coding implementation of the above explanation.

package Test;

import java.util.Properties;
import javax.activation.*;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

public class SendEmailReportSeleniumDemo {

	public static void main(String[] args) throws MessagingException {
		
		//Recipient's Mail id
		String receipientTo = "go4avinash@gmail.com";
		
		//Sender's Mail id
		String senderFrom = "inviultest@gmail.com";
		
		//Path of PDF test report
		String path = "C:\\Users\\Avinash\\workspace1\\com.inviul.datadrivenframework1\\TestReport\\pdf-1544861365116.pdf";
		
		//Getting System properties
		Properties prop = System.getProperties();
		
		//Setting up smtp host
		prop.setProperty("mail.smtp.host", "smtp.gmail.com");
		
		//Creating a new session for smtp
		Session session = Session.getDefaultInstance(prop);
		
		MimeMessage msg = new MimeMessage(session);
		
		//Instance of From Internet address
		InternetAddress frmAddress = new InternetAddress(senderFrom);
		
		//Instance of To Internet address
		InternetAddress toAddress = new InternetAddress(receipientTo);
		
		//Setting up sender's address
		msg.setFrom(frmAddress);
		
		//Setting up recipient's address
		msg.addRecipient(Message.RecipientType.TO, toAddress);
		
		//Setting email's subject
		msg.setSubject("Test Status Report");
		
		BodyPart msgBody = new MimeBodyPart();
		
		//Setting email's message body
		msgBody.setText("This is test report through mail");
		
		//Instance of second part
		Multipart multiPart = new MimeMultipart();
		
		multiPart.addBodyPart(msgBody);
		
		//Another mail body
		msgBody = new MimeBodyPart();
		
		//Path to pdf file for attachment
		DataSource source = new FileDataSource(path);
		
		DataHandler dataHandler = new DataHandler(source);
		
		msgBody.setDataHandler(dataHandler);
		
		msgBody.setFileName(path);
		
		multiPart.addBodyPart(msgBody);
		
		msg.setContent(multiPart);
		
		//Authentication and connection establishment to the sender's mail
		Transport transport = session.getTransport("smtps");
		transport.connect("smtp.gmail.com",465,"inviultest@gmail.com","your password");
		transport.sendMessage(msg, msg.getAllRecipients());
		transport.close();
		
		System.out.println("Mail Sent");
		
		
	}

}

Email Output

Following screenshot shows the email sent from the sender’s mail address.

Send email notification of Test report in Selenium

That’s all about ‘send email notification of Test report in Selenium’. You can raise your queries through the comment form below and don’t miss to join our Facebook group for quick updates on Selenium.

Join Inviul fb group

Leave a Reply