Pages

Pages

Wednesday, 12 August 2015

Sending Email using gmail with Attachment and Text (Core JAVA)

If you want to use this function with testNG then use as below under AfterSuite annotation of TestNG:


//After complete execution send pdf report by email
    
    @AfterSuite
 
    public void tearDown(){
 
        sendPDFReportByGMail("FROM@gmail.com", "PASSWORD", "TO@zellax.com", "PDF Report", "");

 
        }

package reporting;

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.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class PDEmail{
 
 public static void main(String[] args){
  sendPDFReportByGMail("FROM@gmail.com", "PASSWORD", "TO@zellax.com", "PDF Report", "");
 }
 
 public static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {
   
  Properties props = System.getProperties();
   
  String host = "smtp.gmail.com";
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.user", from);
  props.put("mail.smtp.password", pass);
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.auth", "true");
  
  Session session = Session.getDefaultInstance(props);
  
  MimeMessage message = new MimeMessage(session);
  try {
      //Set from address
   
   message.setFrom(new InternetAddress(from));
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
   //Set subject
   message.setSubject(subject);
   message.setText(body);
   
   BodyPart objMessageBodyPart = new MimeBodyPart();
   objMessageBodyPart.setText("Dear Customer,\n\nPlease Find the attached Automation Report File!\n\nThanks and Regards\nZellax Solutions");
   
   Multipart multipart = new MimeMultipart();
   multipart.addBodyPart(objMessageBodyPart);
   objMessageBodyPart = new MimeBodyPart();
   
   //Set path to the pdf report file
   
   String filename = System.getProperty("user.dir")+"\\Default test.pdf";
   
   //Create data source to attach the file in mail
   
   DataSource source = new FileDataSource(filename);
   objMessageBodyPart.setDataHandler(new DataHandler(source));
   objMessageBodyPart.setFileName("Automation Testing Report");
   multipart.addBodyPart(objMessageBodyPart);
   message.setContent(multipart);
   Transport transport = session.getTransport("smtp");
   transport.connect(host, from, pass);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
   }
   
  catch (AddressException ae) {
   ae.printStackTrace();   
  }
   
  catch (MessagingException me) {
   me.printStackTrace();   
  } 
}
}
 

No comments:

Post a Comment