前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >单元测试工具(连载5)

单元测试工具(连载5)

作者头像
顾翔
发布2019-12-12 13:23:04
3820
发布2019-12-12 13:23:04
举报

1.8 使用JAVA脚本发送测试报告

测试报告产生了,为了配合CI的实现,可以用JAVA来实现发送测试报告到相关人员的邮件系统中,代码如下。

案例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;

/**

* 实现邮件发送功能

* @authorAdministrator

*

*/

public class EmailSender {

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

privateString host; // 服务器地址

privateString from; // 发件人

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

privateString title; // 主题

privateString content; // 内容

privateList<File> attachmentlist ; //附件集

privateString username; // 用户名

privateString password; // 密码

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

privateString sendEmployeeId;

publicString getSendEmployeeId() {

returnsendEmployeeId;

}

publicvoid setSendEmployeeId(String sendEmployeeId) {

this.sendEmployeeId= sendEmployeeId;

}

publicString getHost() {

returnhost;

}

publicvoid setHost(String host) {

this.host= host;

}

publicString getFrom() {

returnfrom;

}

publicvoid setFrom(String from) {

this.from= from;

}

publicString getTo() {

returnto;

}

publicvoid setTo(String to) {

this.to= to;

}

publicString getTitle() {

returntitle;

}

publicvoid setTitle(String title) {

this.title= title;

}

publicString getContent() {

returncontent;

}

publicvoid setContent(String content) {

this.content= content;

}

publicList<File> getAttachmentlist() {

returnattachmentlist;

}

publicvoid setAttachmentlist(List<File> attachmentlist) {

this.attachmentlist= attachmentlist;

}

publicString getUsername() {

returnusername;

}

publicvoid setUsername(String username) {

this.username= username;

}

publicString getPassword() {

returnpassword;

}

publicvoid setPassword(String password) {

this.password= password;

}

publicString getPort() {

returnport;

}

publicvoid setPort(String port) {

this.port= port;

}

privateString port;

publicEmailSender(String host, String from, String to, String title,

Stringcontent, 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;

}

publicEmailSender(String to, String title,

Stringcontent, List attachmentlist) {

this.to= to;

this.title= title;

this.content= content;

this.attachmentlist= attachmentlist;

}

/**

* 发送邮件

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

*/

publicString[] sendMail(){

String[]result=new String[2];

Sessionsession=null;

Propertiesprops = 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");

EmailAuthenticatorauth = new EmailAuthenticator(username, password);

session= Session.getInstance(props, auth);

}

//设置邮件发送信息

try{

//创建邮件

MimeMessagemessage = new MimeMessage(session);

//设置发件人地址

message.setFrom(newInternetAddress(from));

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

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

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

//设置邮件主题

message.setSubject(title);

//设置发送时间

message.setSentDate(newDate());

//设置发送内容

Multipartmultipart = new MimeMultipart();

MimeBodyPartcontentPart = new MimeBodyPart();

contentPart.setText(content);

multipart.addBodyPart(contentPart);

//设置附件

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

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

MimeBodyPartattachmentPart = new MimeBodyPart();

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

attachmentPart.setDataHandler(newDataHandler(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 {

//需要验证

Transporttransport = session.getTransport("smtp");

transport.connect();

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

transport.close();

}

result[0]="0";

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

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

}catch(MessagingExceptionmex){

result[0]="1";

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

if(mexinstanceof AuthenticationFailedException){

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

}

}catch (Exception e) {

result[0]="1";

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

}

returnresult;

}

publicstatic void main(String[] args){

StringSNMPTServer = "smtp.126.com";

Stringfrom="xianggu625@126.com";

Stringto="hedan@cmss.chinamobile.com";

Stringtitle="发送测试报告";

Stringcontent="附件为测试报告";

Stringusername="xianggu625@126.com";

Stringpassword="123456";

Stringport="25";

Listlist=new ArrayList();

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

EmailSendersender=newEmailSender(SNMPTServer,from,to,title,content,list,username,password,port);

String[] result = sender.sendMail();

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

}

}

/**

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

*/

class EmailAuthenticator extends Authenticator {

privateString username = null;

privateString password = null;

publicEmailAuthenticator(String username, String password) {

this.username= username;

this.password= password;

}

publicPasswordAuthentication getPasswordAuthentication() {

returnnew PasswordAuthentication(username, password);

}

}

读者只要修改main()方法开始部分的设置就可以了。

星云测试

http://www.teststars.cc

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试培训 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.8 使用JAVA脚本发送测试报告
    • 案例3:利用JAVA发送电子邮件。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档