Send testng report through email after test execution



  • I have to send an email of report of the test as soon as my all tests get executed. I will have to mention this in AfterSuite but don't know what exactly will have to use to achieve this. Any suggestion.



  • I did this by following code.

    import java.util.Properties;
    
    import javax.activation.*;
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    
    public class emailReport {
    
    public static void main(String[] args) {
    
        final String username = "give your email id";
        final String password = "enter your password";
    
        Properties props = new Properties();
        props.put("mail.smtp.auth", true);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
    
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
    
        try {
    
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from.mail.id91@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("to.mail.id@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("PFA");
    
            MimeBodyPart messageBodyPart = new MimeBodyPart();
    
            Multipart multipart = new MimeMultipart();
    
            messageBodyPart = new MimeBodyPart();
            String file = "path of file to be attached";
            String fileName = "attachment name";
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);
    
            message.setContent(multipart);
    
            System.out.println("Sending");
    
            Transport.send(message);
    
            System.out.println("Done");
    
        } catch (MessagingException e) {
            e.printStackTrace();
        }
      }
    }
    

    It worked fine for me.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2