How to send a report through email using Selenium Webdriver

0 votes
How to send a report through email using Selenium Webdriver?
Jul 16, 2019 in Selenium by Yashmika
10,146 views

1 answer to this question.

0 votes

Hey Yashmika, to send a report through email, you can use Java Mail Jar files with Selenium. It comes with a library which allows you to create session and send mails though SMTP:

package sendReportMailSelenium;

import java.util.Properties;

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.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 SendMailSSLWithAttachment {

 public static void main(String[] args) {

  // Create object of Property file
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");

  // This will handle the complete authentication
  Session session = Session.getDefaultInstance(props,

    new javax.mail.Authenticator() {

     protected PasswordAuthentication getPasswordAuthentication() {

     return new PasswordAuthentication("your_email", "your_password");

     }

    });

  try {

   // Create object of MimeMessage class
   Message message = new MimeMessage(session);
   message.setFrom(new InternetAddress("anvi.rajawat@gmail.com"));
   message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("anvi.rajawat@gmail.com"));
   message.setSubject("Testing Subject");

   // Create object to add multimedia type content
   BodyPart messageBodyPart1 = new MimeBodyPart();
   messageBodyPart1.setText("This is message body");

   // Create another object to add another content
   MimeBodyPart messageBodyPart2 = new MimeBodyPart();
   String filename = "D:\\report.xlsx";

   // Create data source and pass the filename
   DataSource source = new FileDataSource(filename);

   // set the handler
   messageBodyPart2.setDataHandler(new DataHandler(source));

   // set the file
   messageBodyPart2.setFileName(filename);

   // Create object of MimeMultipart class
   Multipart multipart = new MimeMultipart();

   // add body part 1
   multipart.addBodyPart(messageBodyPart2);

   // add body part 2
   multipart.addBodyPart(messageBodyPart1);

   // set the content
   message.setContent(multipart);

   // finally send the email
   Transport.send(message);

   System.out.println("=====Email Sent=====");

  } catch (MessagingException e) {

   throw new RuntimeException(e);

  }

 }

}
answered Jul 17, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

0 votes
1 answer

How can I send some data to a Prompt Alert box using Selenium Webdriver?

Hey Abhishek, you can use sendKeys() method ...READ MORE

answered Jul 3, 2019 in Selenium by Abha
• 28,140 points
9,451 views
0 votes
1 answer
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
15,988 views
0 votes
1 answer

How to take screenshot of a frame using Selenium WebDriver?

you can use the below code: import java.awt.image.BufferedImage; import ...READ MORE

answered Jun 20, 2018 in Selenium by Meci Matt
• 9,460 points
3,440 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,567 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,084 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,178 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,840 views
0 votes
1 answer

How to mouse hover on a web element using Selenium WebDriver?

Hello Nitin, to automate mouse hovering over ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
6,490 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP