当前位置: 首页 > 知识库问答 >
问题:

如何从Maven向电子邮件发送TestNG可电子邮件报告?

法烨烨
2023-03-14

我使用下面的代码片段通过邮件发送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();
    }
}
public void appstop() throws IOException {
        sendingemail.sendmailfun();
    }
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

共有1个答案

何修能
2023-03-14
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....");  
    }
   //}  
}

试着像这样使用它对我有效。但是当我们运行这个旧的电子邮件时,只能在邮件中附上报告。

 类似资料:
  • 请让我知道通过邮件自动发送TestNG可电子邮件报告而不添加外部jar的最佳方式。

  • 问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送

  • 我有一个场景,我需要从不同的TestNG-Emailable-HTML报告中收集数据,并使其与testng-emailable报告一样。 我对此没有任何想法,比如我如何实现它。我只想创建一个分离的java项目,它将读取不同的html报告,并将使其最终合并报告看起来像testng-emailable报告。 我看起来没有工作代码。只是寻找一些输入来启动这个需求。

  • 问题内容: 我希望我的网站能够发送电子邮件而不刷新页面。所以我想使用Javascript。 这是我要调用的函数的方式,但是我不确定要在javascript函数中放入什么。通过研究,我发现了一个使用mailto方法的示例,但是我的理解是实际上并没有直接从站点发送邮件。 所以我的问题是,我在哪里可以找到要放在JavaScript函数中的内容,以便直接从网站发送电子邮件。 问题答案: 您不能直接使用ja

  • 问题内容: 在Swift 的正常情况下,我使用此代码发送邮件。 如何在SwiftUI中实现相同目标? 我需要使用吗? 问题答案: 如前所述,您需要将组件移植到via 。 这是一个简单的实现: 用法 : (在运行iOS 13的iPhone 7 Plus上进行了测试-就像一个护身符) 为Xcode 11.4更新

  • 主要内容:SmtpClient类,示例VB.Net应用程序可发送电子邮件。名称空间包含用于向简单邮件传输协议(SMTP)服务器发送电子邮件以供传送的类。 下表列出了一些常用的类: 编号 类 描述 1 代表电子邮件的附件。 2 将附件存储为电子邮件的一部分。 3 代表电子邮件发件人或收件人的地址。 4 存储与电子邮件关联的电子邮件地址。 5 表示可以使用类发送的电子邮件。 6 允许应用程序使用简单邮件传输协议(SMTP)发送电子邮件。