How to attach and Send email through Selenium WebDriver using SMTP OutLook (Using JAVAX Jar file )

Add Comment
1 Answer(s)

Hello Mr. Arun Kaduru,
Previously I worked on the same requirement,Below is the code for your refernce

public class SendMail {
 public static void execute(String errmsg,String methodname) throws Exception
 {
 Properties maildata = ReadPropertiesFileData.read("MailData.properties"); 
 String[] to= maildata.getProperty("to").split(",");
 String[] cc = {};
 String[] bcc = {};
 if(!maildata.getProperty("cc").equals("") && maildata.getProperty("cc") != null ){
 cc= maildata.getProperty("cc").split(",");
 }
 if(!maildata.getProperty("bcc").equals("") && maildata.getProperty("bcc") != null){
 bcc = maildata.getProperty("bcc").split(",");
 }
 SendMail.sendMail(maildata.getProperty("username"),
 maildata.getProperty("password"),
 maildata.getProperty("host"),
 maildata.getProperty("port"),
 "true",
 "true",
 true,
 "javax.net.ssl.SSLSocketFactory",
 "false",
 to,
 cc,
 bcc,
 maildata.getProperty("subject"),
 maildata.getProperty("msgbody"),
 errmsg,
 methodname);
 }
 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 err,
 String name){
 //Object Instantiation of a properties file.
 Properties props = new Properties();
 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);
 }
 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);
 Multipart multipart = new MimeMultipart();
 /*MimeBodyPart messageBodyPart = new MimeBodyPart();
 FileDataSource source = new FileDataSource(attachmentPath);
 messageBodyPart.setDataHandler(new DataHandler(source));
 messageBodyPart.setFileName(attachmentName);*/
// 
 //newly added code
 BodyPart messageBodyPart1 = new MimeBodyPart(); 
 String body_text = "Please find the below message \n\n\nMethod Name : "+name+"\n\nReason for failing : "+err;
 messageBodyPart1.setText(body_text); 
 multipart.addBodyPart(messageBodyPart1);
 msg.setContent(multipart);
 msg.setFrom(new InternetAddress(userName));
 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;
 }
 }
}

I hope it is useful to you !

sunilgadhi Train Answered on July 28, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.