Sending an email is an important activity as we always try to run the automation test suite during non-business hours. We will get the test updates on email like Test has been completed or not, how many test cases have been passed, failed or skipped. So, all such information we can get very easily if you integrate the below code in your framework. The below code will give you the guidance that how you can send an email after completion of your test execution using Selenium API.
The very first thing that we should know about the Server & Port details. In case of Gmail the following details will be used.
I have to pass the server details in Key, Value pairs using Properties object like below
public static Properties property;
property=new Properties();
property.put("mail.smtp.host","smtp.gmail.com");
property.put("mail.smtp.socketFactory.port", "465");
property.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
property.put("mail.smtp.auth","true");
property.put("mail.smtp.port", "465");
To support this we should have "javax.mail-api-1.6.2" jar file which can be downloaded from https://mvnrepository.com/artifact/javax.mail/javax.mail-api
Now, import this Jar file in your code and write the below code:
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;
public class SendingReport {
public static Properties property;
public static Session session;
public static void setConnection()
{
property=new Properties();
property.put("mail.smtp.host","smtp.gmail.com");
property.put("mail.smtp.socketFactory.port", "465");
property.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
property.put("mail.smtp.auth","true");
property.put("mail.smtp.port", "465");
}
public static void CreateAuthenticSessions(String UserName, String Password)
{
session=Session.getDefaultInstance(property, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(UserName,Password);
}});
}
public static void ComposeMessage(String strMsgTxt, String strRecepient_Email_Id) throws MessagingException
{
InternetAddress receientAddress=new InternetAddress(strRecepient_Email_Id);
Message msg=new MimeMessage(session);
msg.setFrom(receientAddress);
msg.setRecipients(Message.RecipientType.TO, receientAddress.parse(strRecepient_Email_Id));
msg.setSubject("This is my first email");
BodyPart msgBody=new MimeBodyPart();
msgBody.setText(strMsgTxt);
Multipart multi=new MimeMultipart();
multi.addBodyPart(msgBody);
msg.setContent(multi);
Transport.send(msg);
System.out.println("Mail sent succussfully");
}
public static void main(String[] args) throws MessagingException
{
setConnection();
CreateAuthenticSessions("Enter User Name", "Enter Password");
ComposeMessage("My First Email message","vikas_9264@yahoo.co.in");
}
}
The first time when you execute the above code then you will get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:283)
at javax.mail.Session.<init>(Session.java:268)
at javax.mail.Session.getDefaultInstance(Session.java:378)
at SendingReport.CreateAuthenticSessions(SendingReport.java:34)
at SendingReport.main(SendingReport.java:60)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
The very first thing that we should know about the Server & Port details. In case of Gmail the following details will be used.
Key
|
Value
|
mail.smtp.host
|
smtp.gmail.com
|
mail.smtp.socketFactory.port
|
465
|
mail.smtp.socketFactory.class
|
javax.net.ssl.SSLSocketFactory
|
mail.smtp.auth
|
true
|
mail.smtp.port
|
465
|
I have to pass the server details in Key, Value pairs using Properties object like below
public static Properties property;
property=new Properties();
property.put("mail.smtp.host","smtp.gmail.com");
property.put("mail.smtp.socketFactory.port", "465");
property.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
property.put("mail.smtp.auth","true");
property.put("mail.smtp.port", "465");
To support this we should have "javax.mail-api-1.6.2" jar file which can be downloaded from https://mvnrepository.com/artifact/javax.mail/javax.mail-api
Now, import this Jar file in your code and write the below code:
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;
public class SendingReport {
public static Properties property;
public static Session session;
public static void setConnection()
{
property=new Properties();
property.put("mail.smtp.host","smtp.gmail.com");
property.put("mail.smtp.socketFactory.port", "465");
property.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
property.put("mail.smtp.auth","true");
property.put("mail.smtp.port", "465");
}
public static void CreateAuthenticSessions(String UserName, String Password)
{
session=Session.getDefaultInstance(property, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(UserName,Password);
}});
}
public static void ComposeMessage(String strMsgTxt, String strRecepient_Email_Id) throws MessagingException
{
InternetAddress receientAddress=new InternetAddress(strRecepient_Email_Id);
Message msg=new MimeMessage(session);
msg.setFrom(receientAddress);
msg.setRecipients(Message.RecipientType.TO, receientAddress.parse(strRecepient_Email_Id));
msg.setSubject("This is my first email");
BodyPart msgBody=new MimeBodyPart();
msgBody.setText(strMsgTxt);
Multipart multi=new MimeMultipart();
multi.addBodyPart(msgBody);
msg.setContent(multi);
Transport.send(msg);
System.out.println("Mail sent succussfully");
}
public static void main(String[] args) throws MessagingException
{
setConnection();
CreateAuthenticSessions("Enter User Name", "Enter Password");
ComposeMessage("My First Email message","vikas_9264@yahoo.co.in");
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:283)
at javax.mail.Session.<init>(Session.java:268)
at javax.mail.Session.getDefaultInstance(Session.java:378)
at SendingReport.CreateAuthenticSessions(SendingReport.java:34)
at SendingReport.main(SendingReport.java:60)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
To avoid the above exception then import this jar file "javax.mail-1.6.2". Download this jar from here https://mvnrepository.com/artifact/javax.mail/javax.mail-api/1.6.2
Now, when you run the code you will get rid from the above exception. Google has implemented security that no third party application can access your account for sending an email. You will get the below email in your gmail inbox:
Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure that no one else has access.
So, you have to change some security settings for your gmail account in order to work this code. I did change the status from Off to On for "Less Secure app access" settings in Gmail (https://myaccount.google.com/lesssecureapps).
Now it's working for me. See the below output.