Thursday, August 2, 2012

Sending Email in Selenium RC

This is function help you to send the Email to multiple stockholders with Test Report as an attachment.:


package zipQuoteNavigation;
//File Name SendFileEmail.java

import java.text.SimpleDateFormat;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import org.fest.swing.testng.listener.*;


public class SendFileEmail
{
public static void main(String [] args){
// Recipient's email ID needs to be mentioned.
String to = "utakalkar@king.com,argupta@king.com,mdeshmukh@king.com,sgainewar@king.com" ;
//String to = "sgainewar@king.com";
  // Sender's email ID needs to be mentioned
  String from = "sgainewar@king.com";

  // Assuming you are sending email from localhost  , enter the host (check in outlook)
  String host = "host";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
  System.out.println("---------------- Email Process Started.... " + System.currentTimeMillis());

// Below code in try catch method executed the batch file created in location D:\temp
  //Runtime rn =Runtime.getRuntime();
try {
//Process p1 = rn.exec("cmd /c start D:\\temp\\a.bat");

}
catch(Exception e){};

     // Create a default MimeMessage object.
     MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
     message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
     message.addRecipients(Message.RecipientType.TO,
                              to);

     // Set Subject: header field
     message.setSubject("Whistleblower Report on Zip code navigation !");

     // Create the message part
     BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
     messageBodyPart.setText("To find the more details on the test result find attachment ");
   
     // Create a multipar message
     Multipart multipart = new MimeMultipart();

     // Set text message part
     //multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();
   
     String filename = "C:\\Documents and Settings\\sgainewar\\workspace\\HQF\\test-output\\emailable-report.html";
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);
     // Send the complete message parts
     message.setContent(multipart );

     // Send message
     Transport.send(message);
     System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
   
  }
}
}  

No comments:

Post a Comment