如何从maven发送TestNG emailable报告到电子邮件?

时间:2016-10-07 07:35:20

标签: email automation smtp testng

我使用以下代码段通过邮件发送testng的emailable-report.html。

公共类SampleSendMail {

public void sendmailfun() {

    String username = "mailid";
    String password = "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("sendingmailid"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("receivingmailid"));
        message.setSubject("Testing Subject");
        message.setText("PFA");

        MimeBodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();
        String file = "/Users/Documents/workspace/sampleproject/test-output/emailable-report.html";
        String fileName = "emailable-report.html";
        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();
    }
}

}

在@AfterSuite中,我调用了这个函数。

public void appstop() throws IOException {
        sendingemail.sendmailfun();
    }

我收到以下错误。

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

任何人都可以帮我纠正这个问题吗?

2 个答案:

答案 0 :(得分:0)

import java.io.File;
import java.io.IOException;
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.PasswordAuthentication;
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;

import org.testng.annotations.Test;

public  class SendAttachment{  
@Test
public static void sendmail() throws AddressException, MessagingException, InterruptedException{
 Thread.sleep(50000);
  System.out.println("Test mail");
  String[] to={"mail address","mail address"};
  String to2="mail address";//change accordingly  
  final String user="mail address";//change accordingly  
  final String password="password";//change accordingly  

  //1) get the session object     
  Properties properties = System.getProperties();  
  properties.setProperty("mail.smtp.host", "smtp.gmail.com");
  properties.put("mail.smtp.port", "587"); //TLS Port
  properties.put("mail.smtp.auth", "true"); //enable authentication
  properties.put("mail.smtp.starttls.enable", "true"); 

  Session session = Session.getDefaultInstance(properties,  
   new javax.mail.Authenticator() {  
   protected PasswordAuthentication getPasswordAuthentication() {  
   return new PasswordAuthentication(user,password);  
   }  
  });  

  //2) compose message     

    MimeMessage message = new MimeMessage(session);  
    message.setFrom(new InternetAddress(user));  
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mailid1,mailid2"));
    message.setSubject("ECM Regression Test suite Results");  

    //3) create MimeBodyPart object and set your message text     
    BodyPart messageBodyPart1 = new MimeBodyPart();  
    messageBodyPart1.setText("Please find the Regression Result in the attachment");  

    //4) create new MimeBodyPart object and set DataHandler object to this object      
    MimeBodyPart messageBodyPart2 = new MimeBodyPart();  MimeBodyPart messageBodyPart3 = new MimeBodyPart(); MimeBodyPart messageBodyPart4 = new MimeBodyPart(); MimeBodyPart messageBodyPart5 = new MimeBodyPart(); 

    File f3=new File("D:\\svn\\CI_1.0\\seleniumScriptsRegression\\seleniumScriptsRegression\\test-output\\emailable-report.html");
    DataSource source4 = new FileDataSource(f3);
    messageBodyPart5.setDataHandler(new DataHandler(source4));    
    messageBodyPart5.setFileName(f3.getName()); 


    //5) create Multipart object and add MimeBodyPart objects to this object      
    Multipart multipart = new MimeMultipart();  
    multipart.addBodyPart(messageBodyPart1);   
    multipart.addBodyPart(messageBodyPart5);  

    //6) set the multiplart object to the message object  
    message.setContent(multipart);  

    //7) send message  
    Transport.send(message);  

   System.out.println("message sent....");  
    }
   //}  
}

尝试像这样使用它为我工作。但是,当我们运行这个旧的电子邮件报告时,只会附在邮件中。

答案 1 :(得分:0)

**You can send the mail through **Apache commons Email**
=======================================================

**but before using Apache commons you need to add the dependency in Maven POM File.**

Dependency is as mentioned below
--------------------------------

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.5</version>
</dependency>

You can refer to the below link for compatible dependency.

**Link:** https://mvnrepository.com/artifact/org.apache.commons/commons-email 
**JAVA CODE**
------------

public void sendMail() throws MessagingException, EmailException
{        
    String username="abc.@gmail.com";
    String password="1234567890";
    String from_Email="xyz.@gmail.com";
    String to_Email="pqr.@gmail.com";
    Email email = new SimpleEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator(username, password));
    email.setSSLOnConnect(true);
    email.setFrom(from_Email);
    email.setSubject("This is a test mail ... :-)");
    email.setMsg("Body Message");
    email.addTo(to_Email);
    email.send();
}