川石教育
全国咨询热线:136-9172-9932
  1. 首页 > 资讯与干货 > IT资讯

使用JAVA脚本发送测试报告

作者:川石学院 日期:2021-10-26 16:27:22 点击数:

一天一个关于测试知识点,5分钟内讲解你最关心的软件测试问题,今天就来谈谈关于软件测试中的“使用JAVA脚本发送测试报告”。

使用JAVA脚本发送测试报告(图1)

  测试报告产生后,可以用JAVA发送测试报告到相关人员的邮件系统,代码如下。

  案例2-3:利用JAVA发送电子邮件。

  package Util.com.jerry;

  import java.io.File;

  import java.util.ArrayList;

  import java.util.Date;

  import java.util.List;

  import java.util.Properties;

  import javax.activation.DataHandler;

  import javax.activation.FileDataSource;

  import javax.mail.AuthenticationFailedException;

  import javax.mail.Authenticator;

  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.InternetAddress;

  import javax.mail.internet.MimeBodyPart;

  import javax.mail.internet.MimeMessage;

  import javax.mail.internet.MimeMultipart;

  import javax.mail.internet.MimeUtility;

  import org.apache.commons.lang.StringUtils;

  import org.apache.log4j.LogManager;

  import org.apache.log4j.Logger;

  /**

  * 实现邮件发送功能

  * @author Administrator

  *

  */

  public class EmailSender {

  private static final Logger logger = LogManager.getLogger(EmailSender.class);

  private String host; // 服务器地址

  private String from; // 发件人

  private String to; // 收件人 多个收件人以,分隔

  private String title; // 主题

  private String content; // 内容

  private Listattachmentlist ; //附件集

  private String username; // 用户名

  private String password; // 密码

  /**发件人员工编号*/

  private String sendEmployeeId;

  public String getSendEmployeeId() {

  return sendEmployeeId;

  }

  public void setSendEmployeeId(String sendEmployeeId) {

  this.sendEmployeeId = sendEmployeeId;

  }

  public String getHost() {

  return host;

  }

  public void setHost(String host) {

  this.host = host;

  }

  public String getFrom() {

  return from;

  }

  public void setFrom(String from) {

  this.from = from;

  }

  public String getTo() {

  return to;

  }

  public void setTo(String to) {

  this.to = to;

  }

  public String getTitle() {

  return title;

  }

  public void setTitle(String title) {

  this.title = title;

  }

  public String getContent() {

  return content;

  }

  public void setContent(String content) {

  this.content = content;

  }

  public ListgetAttachmentlist() {

  return attachmentlist;

  }

  public void setAttachmentlist(Listattachmentlist) {

  this.attachmentlist = attachmentlist;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  this.username = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  this.password = password;

  }

  public String getPort() {

  return port;

  }

  public void setPort(String port) {

  this.port = port;

  }

  private String port;

  public EmailSender(String host,String from,String to,String title,

  String content,List attachmentlist,String username,String password,String port) {

  this.host = host;

  this.from = from;

  this.to = to;

  this.title = title;

  this.content = content;

  this.attachmentlist = attachmentlist;

  this.username = username;

  this.password = password;

  this.port=port;

  }

  public EmailSender(String to,String title,

  String content,List attachmentlist) {

  this.to = to;

  this.title = title;

  this.content = content;

  this.attachmentlist = attachmentlist;

  }

  /**

  * 发送邮件

  * @return 发送状态信息 index0:状态 0成功 1失败;index1:描述错误信息

  */

  public String[] sendMail(){

  String[] result=new String[2];

  Session session=null;

  Properties props = System.getProperties();

  props.put("mail.smtp.host",host);

  props.put("mail.smtp.sendpartial","true");

  props.put("mail.smtp.port",port);

  if(StringUtils.isBlank(username)){//不需要验证用户名密码

  session = Session.getDefaultInstance(props,null);

  }else{

  props.put("mail.smtp.auth","true");

  EmailAuthenticator auth = new EmailAuthenticator(username,password);

  session = Session.getInstance(props,auth);

  }

  //设置邮件发送信息

  try{

  // 创建邮件

  MimeMessage message = new MimeMessage(session);

  // 设置发件人地址

  message.setFrom(new InternetAddress(from));

  // 设置收件人地址(多个邮件地址)

  InternetAddress[] toAddr = InternetAddress.parse(to);

  message.addRecipients(Message.RecipientType.TO,toAddr);

  // 设置邮件主题

  message.setSubject(title);

  // 设置发送时间

  message.setSentDate(new Date());

  // 设置发送内容

  Multipart multipart = new MimeMultipart();

  MimeBodyPart contentPart = new MimeBodyPart();

  contentPart.setText(content);

  multipart.addBodyPart(contentPart);

  //设置附件

  if(attachmentlist!=null && attachmentlist.size()>0){

  for(int i = 0 ; i < attachmentlist.size();i++){

  MimeBodyPart attachmentPart = new MimeBodyPart();

  FileDataSource source = new FileDataSource(attachmentlist.get(i));

  attachmentPart.setDataHandler(new DataHandler(source));

  attachmentPart.setFileName(MimeUtility.encodeWord(attachmentlist.get(i).getName(),"gb2312",null));

  multipart.addBodyPart(attachmentPart);

  }

  }

  message.setContent(multipart);

  //登录SMTP服务器

  if (StringUtils.isBlank(username)) {

  // 不需验证

  Transport.send(message);

  } else {

  // 需要验证

  Transport transport = session.getTransport("smtp");

  transport.connect();

  transport.sendMessage(message,message.getAllRecipients());

  transport.close();

  }

  result[0]="0";

  result[1]="发送成功";

  logger.info("邮件发送成功!发送人:"+from);

  }catch(MessagingException mex){

  result[0]="1";

  result[1]="邮件服务器发生错误";

  if(mex instanceof AuthenticationFailedException){

  result[1]="用户名或密码错误";

  }

  } catch (Exception e) {

  result[0]="1";

  result[1]="系统异常";

  }

  return result;

  }

  public static void main(String[] args){

  String SNMPTServer = "smtp.126.com";

  String from="xianggu625@126.com";

  String to="hedan@cmss.chinamobile.com";

  String title="发送测试报告";

  String content="附件为测试报告";

  String username="xianggu625@126.com";

  String password="123456";

  String port="25";

  List list=new ArrayList();

  list.add(new File("C:\\myjava\\web\\junit.rar"));#junit.rar为发送测试报告的目录压缩jar包

  EmailSender sender=new EmailSender(SNMPTServer,from,to,title,content,list,username,password,port);

  String [] result = sender.sendMail();

  System.out.println(result[1]+"ffffffffffffffff");

  }

  }

  /**

  * class MyAuthenticator用于邮件服务器认证 构造器需要用户名、密码作参数

  */

  class EmailAuthenticator extends Authenticator {

  private String username = null;

  private String password = null;

  public EmailAuthenticator(String username,String password) {

  this.username = username;

  this.password = password;

  }

  public PasswordAuthentication getPasswordAuthentication() {

  return new PasswordAuthentication(username,password);

  }

  }

读者只需要按照需求修改main()方法中的黑体部分。

以上就是关于“使用JAVA脚本发送测试报告”的内容就学习到这里,大家觉得文章有用的话记得每天来这里和小编一起学习涨薪技能哦。


相关文章
  • 亚马逊运营成功转行软件测试,薪资13K表示很满意!2021-10-26 16:27:22
  • 西安川石的兰朋友喊你来当他的学弟学妹啦!2021-10-26 16:27:22
  • 国外的月亮也不一定比国内测试猿的年薪美~2021-10-26 16:27:22
  • 建筑工程专业朱同学成功转行为软件测试人!2021-10-26 16:27:22
  • 财务管理专业转行软件测试月薪甩会计几条街!2021-10-26 16:27:22
  • 只有技术沉淀才能成功上岸,深圳就业薪资13K!2021-10-26 16:27:22
  • 薪资11K!实现自我价值,从掌握一门IT技术开始...2021-10-26 16:27:22
  • 文科生转行软件测试照样拿下高薪15K!2021-10-26 16:27:22
  • 恭喜罗同学喜提19.5K,成功入行软件测试!2021-10-26 16:27:22
  • 毕业1年,迷茫的他最终选择转行软件测试2021-10-26 16:27:22