Email Functionality to Test Automation Framework using JavaMail

Automation, Frameworks, Java

Email Functionality to Test Automation Framework using JavaMail

Sending an email after test execution completed is a very nice idea. it helps all members to look at the report and act accordingly.

Today, we will see how to send an email using JavaMail API. we will also make sure that it will be easy to configure in test automation framework.

first of all, you will require a JavaMail library. Download the latest version from JavaMail site.

once you include this lib/jar in your project, you are set to write the code using JavaMail.

Option 1: without TLC Authentication

 

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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;
import java.util.Properties;
import javax.mail.Session;

public class SendEmail {

/**
* Utility method to send simple HTML email
* @param session
* @param toEmail
* @param subject
* @param body
*/
public static void sendEmail(Session session, String toEmail, String subject, String body){
try
{
System.out.println("Session creation Start");
 
String smtpHostServer = "smtp.example.com";
String emailID = "email_me@example.com";
 
Properties props = System.getProperties();

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

Session session = Session.getInstance(props, null);
System.out.println("Session creation Ends");

System.out.println("Email content preparation starts");
 
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");

msg.setFrom(new InternetAddress("no_reply@example.com", "NoReply-JD"));

msg.setReplyTo(InternetAddress.parse("no_reply@example.com", false));

msg.setSubject(subject, "UTF-8");

msg.setText(body, "UTF-8"); 
// this will send text and not html. if you want html then use msg.setContent(someHtmlMessage, "text/html; charset=utf-8");

msg.setSentDate(new Date());

InternetAddress[] myToList = InternetAddress.parse("abcd@xyz.com,ccs@xyz.com");
InternetAddress[] myBccList = InternetAddress.parse("wwss@xyz.com");
InternetAddress[] myCcList = InternetAddress.parse("tutu@xyz.com");
msg.setRecipients(RecipientType.TO,myToList);
msg.addRecipient(RecipientType.BCC,myBccList);
msg.addRecipient(RecipientType.CC,myCcList);
 
System.out.println("Message is ready to send");
Transport.send(msg);

System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}

 

Option 2: With TLC Authentication ( Gmail, Microsoft exchange)

 

in above java code, replace the authentication block with below block

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host 
props.put("mail.smtp.port", "587"); //TLS Port microsoft exchange server port usaully is 25
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
 
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);

 

Option 3: SMTP Example with SSL Authentication

you can so SMTP authentication with SSL certification

		System.out.println("SSLEmail Start");
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
		props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
		props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
		props.put("mail.smtp.port", "465"); //SMTP Port
		
		Authenticator auth = new Authenticator() {
			//override the getPasswordAuthentication method
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(fromEmail, password);
			}
		};
		
		Session session = Session.getDefaultInstance(props, auth);
Leave A Comment
*
*