Skip to main content

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

Today we will share code how to send any type of selenium report in mail using java. For sending the email using JavaMail API, you need to load the two jar files:
  • mail.jar
  • activation.jar

package SeleniumNew;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*; 
import javax.mail.internet.*; 

public class SendEmail
{

    public static void main(String[] args)
    {
      
         String host="smtp.gmail.com";
         final String user="ashishxx@gmail.com";//change accordingly
         final String password="XXX";//change accordingly
        
          /*String[] to={"AshishXX@gmail.com","Ashishxxx@gmail.com"};
          String[] cc={};
          String[] bcc={};
        */
          String to="ashishxx@gmail.com";//change accordingly
        
           //Get the session object
           Properties props = new Properties();
           props.put("mail.smtp.host",host);
           props.put("mail.smtp.auth", "true");
           props.put("mail.smtp.starttls.enable","true");
           
           Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,password);
              }
            });
        
           //Compose the message
            try {
             MimeMessage message = new MimeMessage(session);
             message.setFrom(new InternetAddress(user));
             
             /*for(int i=0;i<to.length;i++){
                 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
   
                     }

                         for(int i=0;i<cc.length;i++){

                        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
                
                                 }
                                    for(int i=0;i<bcc.length;i++){
        
                               msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
                               msg.saveChanges();*/
           
           
           
           
           
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
             message.setSubject("ClubMahindra Automation Report");
            // message.setText("Please find the attached Clubmahindra Automation Report");
             
             BodyPart messageBodyPart1 = new MimeBodyPart();
             messageBodyPart1.setText("Please find the attached Clubmahindra Automation Report");
           
             MimeBodyPart messageBodyPart2 = new MimeBodyPart();
            //File Path
            String Report="Report10.html";
             String filename =System.getProperty("user.dir")+"//Report//"+Report;
                     //"E:\\SeleniumHeadless\\Report\\Report10.html";//change accordingly
             DataSource source = new FileDataSource(filename);
             messageBodyPart2.setDataHandler(new DataHandler(source));
             messageBodyPart2.setFileName(filename);
           
             //5) create Multipart object and add MimeBodyPart objects to this object    
             Multipart multipart = new MimeMultipart();
             multipart.addBodyPart(messageBodyPart1);
             multipart.addBodyPart(messageBodyPart2);
         
             //6) set the multiplart object to the message object
             message.setContent(multipart );
           
            //send the message
             Transport.send(message);
        
             System.out.println("message sent successfully...");
         
             } catch (MessagingException e) {e.printStackTrace();}
         }
}
 
 

Comments

Post a Comment

Popular posts from this blog

Handle radio button & Check Boxes in Selenium

The main difference between Radio button and Checkbox is that, using radio button we will be able to select only one option from the options available. whereas using checkbox, we can select multiple options. Selenium WebDriver supports Radio Button and Radio Group controls using the WebElement class. We can select and deselect the radio buttons using the click() method of the WebElement class and check whether a radio button is selected or deselected using the isSelected() method. Before performing the click on the Radio buttons or check boxes we will have to verify follwing scenarios :- If Radio button or Checkbox is displayed on the webpage If Radio button or Checkbox is enabled on the webpage Check the default selection of the Radio button or Checkbox We use predefined methods present in selenium to handle check Box and Radio  button :- isDisplayed() isEnabled() isSelected() 1.  isDisplayed () this method returns a Boolean value, if it returns t

Appium architecture (How Appium works internally)

Appium Architecture  A ppium is a cross-platform automation tool, API of it supports both OS (Android and iOS) test scripts.It is tested on simulators (iOS,), emulators (Android), and real devices (iOS, Android)   Appium is an HTTP server written in Node.js that creates and handles WebDriver sessions.The Appium web server follows the same approach as the Selenium WebDriver, which receives HTTP requests from client libraries through JSON and then handles those requests in different ways   JSON wire protocol The JSON wire protocol  ( JSONWP ) is a transport mechanism created by WebDriver developers. This wire protocol is a specific set of predefined, standardized endpoints exposed via a RESTful API.   Appium implements the Mobile JSONWP, the extension to the Selenium JSONWP, and it controls the different mobile device behaviors, such as installing/uninstalling apps over the session Let’s have a look at some of the endpoints from the API which are used to interact with