Email 如何将TestNG可通过电子邮件发送的报告发送到maven的电子邮件?

Email 如何将TestNG可通过电子邮件发送的报告发送到maven的电子邮件?,email,automation,smtp,testng,Email,Automation,Smtp,Testng,我使用下面的代码片段通过邮件发送testng的emailable-report.html 公共类示例Sendmail{ public void sendmailfun() { String username = "mailid"; String password = "password"; Properties props = new Properties(); props.put("mail.smtp.auth", true); props.put(

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

公共类示例Sendmail{

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
有人能帮我纠正一下吗

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....");  
    }
   //}  
}
试着像这样使用它对我有用。但是,当我们运行此旧的电子邮件时,只能在邮件中附加报告。

**您可以通过**Apache commons电子邮件发送邮件**
**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();
}
======================================================= **但是在使用ApacheCommons之前,您需要在Maven POM文件中添加依赖项** 依赖关系如下所述 -------------------------------- org.apache.commons 公用电子邮件 1.5 您可以参考以下链接了解兼容的依赖项。 **链接:**https://mvnrepository.com/artifact/org.apache.commons/commons-email **JAVA代码** ------------ public void sendMail()引发MessaginException,EmailException { 字符串username=“abc@gmail.com”; 字符串密码=“1234567890”; 来自_Email=“xyz@gmail.com”的字符串; 字符串指向\u Email=“pqr@gmail.com”; 电子邮件=新的SimpleMail(); email.setHostName(“smtp.gmail.com”); 电邮:setSmtpPort(465); setAuthenticator(新的DefaultAuthenticator(用户名、密码)); email.setSSLOnConnect(true); email.setFrom(from_email); setSubject(“这是一封测试邮件…:-)”; email.setMsg(“正文消息”); email.addTo(to_email); email.send(); }
请阅读我在1、2、3、4、5、6和7中告诉您的代码详细信息。我在testng.xml文件中使用一个单独的测试调用这个类,如下所示。