Monday 12 January 2015

Email Utility file in Selenium Webdriver (Email Generic Function)

Email Utility file in selenium Webdriver

1. Create a utility class for emailing  files. I have created a separate  email utility which can be used to send emails.

2. Create a class “SendMail.java” and Copy paste the below code.
 

package Util;


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

import javax.mail.internet.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.*;
import java.util.zip.ZipEntry;



public class SendMail{
   
    java.util.Date date= new java.util.Date();
   
    public static void main(String[] args) {
       
       
       
       
        SendMail send = new SendMail();
        send.sendReport();
       
       
       
    }
   
   
    public void sendReport()

    {
                try {
            TestUtil.archiveFiles("D:\\TestReport", "D:\\AutoTestReport", "TestAutomationReport");
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

       
   
       
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
                String[] to={"archana @gmail.com"};
                                        

                String[] cc={};
                String[] bcc={};

                //This is for google
                    
               
             
                SendMail.sendMail("archana@gmail.com",
                        "password",
                        "172.16.1.4",
                        "25",
                        "false",
                        "true",
                        true,
                        "javax.net.ssl.SSLSocketFactory",
                        "false",
                        to,
                        cc,
                        bcc,
                        " Automation test Reports performed on "+date,
                        "Please find the reports attached",
                        “D:\\AutoTestReport \\ TestAutomationReport.zip ",
                        " TestAutomationReport.zip ");
          
              
    }



        public  static boolean sendMail(String userName,
                String passWord,
                String host,
                String port,
                String starttls,
                String auth,
                boolean debug,
                String socketFactoryClass,
                String fallback,
                String[] to,
                String[] cc,
                String[] bcc,
                String subject,
                String text,
                String attachmentPath,
                String attachmentName){


                Properties props = new Properties();

                //Properties props=System.getProperties();

        props.put("mail.smtp.user", userName);

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

                if(!"".equals(port))

        props.put("mail.smtp.port", port);

                if(!"".equals(starttls))

        props.put("mail.smtp.starttls.enable",starttls);

        props.put("mail.smtp.auth", auth);
       // props.put("mail.smtps.auth", "true");


                if(debug){

                props.put("mail.smtp.debug", "true");

                }else{

                props.put("mail.smtp.debug", "false");        

                }

//                if(!"".equals(port))
//
//        props.put("mail.smtp.socketFactory.port", port);
//
//                if(!"".equals(socketFactoryClass))
//
//        props.put("mail.smtp.socketFactory.class",socketFactoryClass);

                if(!"".equals(fallback))

        props.put("mail.smtp.socketFactory.fallback", fallback);



        try

        {

                        Session session = Session.getDefaultInstance(props, null);

            session.setDebug(debug);

            MimeMessage msg = new MimeMessage(session);

            msg.setText(text);

            msg.setSubject(subject);
            //attachment start
            // create the message part
          
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source =
              new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(
              new DataHandler(source));
            messageBodyPart.setFileName(attachmentName);
            multipart.addBodyPart(messageBodyPart);
           
            // attachment ends

            // Put parts in message
            msg.setContent(multipart);
            msg.setFrom(new InternetAddress("kausunna.haar@nihilent.com"));

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

            msg.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();

                        Transport transport = session.getTransport("smtp");

                        transport.connect(host, userName, passWord);

                        transport.sendMessage(msg, msg.getAllRecipients());

                        transport.close();

                        return true;

        }

        catch (Exception mex)

        {

            mex.printStackTrace();

                        return false;

        }

        }



}


3. Here the zipped Test reports are mailed. Configure your email id and password in the file.

No comments:

Post a Comment