Passionate to Quality

How To Send TestNG Report In Mail Using Java In Selenium Web driver

Send-Report-TestNG

Selenium is becoming very popular automation tool now. It is capable to automate any web based application. It makes functional testing easy , fast and accurate.

Today we will share code with you using which you send TestNG generated report to any email id. We assume that readers are familiar with TestNG tool and they have integrated it with their Eclipse & selenium.

Following code will help you to send automatic mail to any email id. Mail will have TestNG generated report. You should call this code at end of your tests so it can send latest report of your automated test cases.

First of all you will need to integrate following JARs to your java project to enable mail function :

1. Mail.Jar

2. Activation.Jar

3. Additional.Jar

4. javamail-connector-4.0

5. pop3.jar

6. smtp-1.4.2.jar

7. java-mail-1.4.4.Jar

Necessary code is given below :

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail{

  public static void main(String[] args) {

final String username = "username@gmail.com";
final String password = "password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
  });

try {

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from-email@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("to-email@gmail.com"));
    message.setSubject("Testing Subject");
    message.setText("Dear Mail Crawler,"
        + "\n\n No spam to my email, please!");

    MimeBodyPart messageBodyPart2 = new MimeBodyPart();  

    String filename = "Your attachment file path"
    DataSource source = new FileDataSource(filename);  
    messageBodyPart2.setDataHandler(new DataHandler(source));  
    messageBodyPart2.setFileName(filename);  



    Multipart multipart = new MimeMultipart();  
    multipart.addBodyPart(messageBodyPart1);  
    multipart.addBodyPart(messageBodyPart2);  

     message.setContent(multipart );  


    Transport.send(message);

    System.out.println("Done");

} catch (MessagingException e) {
    throw new RuntimeException(e);
}
}

 

Make sure you replace your actual Email ids in above code. Also add your file location which you want to attach.

That’s it , you are ready now to send TestNG report to any email id.

Email Us for any query




Email: inquiry@thesoftwaretesting.com

DISCLAIMER

Thesoftwaretesting.com provides QA services world wide. We Specialized in Manual Testing, Automation Testing, Web Testing and Mobile App. Testing. We focus on providing top notch Quality assurance services.