前言:由于ant-jmeter目前的版本不支持javamail,也就是说发送邮件时只能借助jenkins自带的发送邮件插件来发送报告。
但是jenkins发送邮件支持发送邮件内容(且有价值、有营养的内容也只能是借用jenkins的宏),不允许上传附件。
总的来说若借助jenkins自带的邮件插件来发送报告的话,内容空洞、没价值
jenkins自带的邮件系统配置内容如下:
邮件内容为:
从图种可以看出,发送一个报告的连接,相对来说不直观,且如果要保持这个连接一直有效,那必须要做备份处理!!
缺点很多,就不一一列举~
因此这么多缺点,是必须要做出优化了!!!!!!
再此,对ant-jmeter进行二次开发,让他支持javamail。
二次开发支持的功能:①支持上传附件②支持邮件内容是以文件的方式输出
首先反编译jmeter自带的ant-jmeter,之后增加2个类,
一个JavaMail类(方法实现),一个MailTask类(方法执行)。ps:关于javamail网上很多材料,现成的的代码,稍微坐下修改就可以~
JavaMail类具体实现:
package org.programmerplanet.ant.taskdefs.jmeter; import java.io.File; import java.io.UnsupportedEncodingException; import java.text.DecimalFormat; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; 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; public class JavaMail { static File mailAttachmentFile; static public void sendMail(int all, int successnum, int failnum, String htmlstring, String mailSubject, String mailAttachment, String mailAddress) throws MessagingException, UnsupportedEncodingException { String s; DecimalFormat df = new DecimalFormat("0.00"); if(all == 0){ s = "0"; } else { s = df.format((float)successnum/(float)all*100); } Properties props = new Properties(); // 开启debug调试 //props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "false"); // 设置邮件服务器主机名(ip或者域名) props.setProperty("mail.smtp.host", "10.64.1.85"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // 设置环境信息 Session session = Session.getInstance(props); // 创建邮件对象 Message msg = new MimeMessage(session); // 设置发件人 msg.setFrom(new InternetAddress("XXX@XXX.com.cn")); // 设置收件人 @SuppressWarnings("static-access") Address[] addresses = new InternetAddress().parse(mailAddress); msg.setRecipients(Message.RecipientType.TO,addresses); // 设置主题 if(mailSubject != null){ msg.setSubject(mailSubject); }else{ msg.setSubject("接口测试报告"); } // 设置邮件内容 BodyPart bp = new MimeBodyPart(); Multipart mp = new MimeMultipart(); bp.setContent("<!DOCTYPE html>" + "<html lang=\"en\">" + "<head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "<meta content=\"shanhe.me\" name=\"Author\">" + "<title>JMeter Test Results</title>" + "<style type=\"text/css\">" + "* { margin: 0; padding: 0 }" + "html{ font-size: 12px; margin: auto; }" + "body { width: 80%; margin: 0 auto; text-align:center; font-size:62.5%;}" + "table { font-size:12px;border-collapse: collapse; table-layout: fixed;word-wrap:break-word;word-break:break-all; }" + "th{border:1px solid black;color: #ffffff;font-weight: normal;text-align:center;background:#2674a6;}" + "td {border:1px solid black;font-weight:normal;}" + "</style></head></head><body>" + "<h2>接口测试报告概要(详细内容见邮件附件)</h2>" + "<table width=\"100%\"class=\"details\" align=\"center\">" + "<tr style=\"line-height:2em;\" valign=\"middle\">" + "<th width=\"50%\">执行总数</th>" + "<th>失败数</th>" + "<th>成功率</th>" + "</tr>" + "<tr style=\"line-height:2em;\" valign=\"middle\">" + "<td align=\"center\">"+all+"</td>" + "<td align=\"center\">"+failnum+"</td>" + "<td align=\"center\">"+s+"%</td>" + "</tr>" +htmlstring + "</table></body></html>", "text/html;charset=utf-8"); mp.addBodyPart(bp); //附件为空时不发附件 mailAttachmentFile = new File(System.getProperty("user.dir")+mailAttachment); if(mailAttachmentFile.exists()){ System.out.println("把mailAttachment报告文件作为附件发送"); bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(System.getProperty("user.dir")+mailAttachment); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B")); mp.addBodyPart(bp); }else{System.out.println("mailAttachment文件不存在,邮件添加附件失败,请检查!");} msg.setContent(mp); msg.saveChanges(); Transport transport = session.getTransport(); // 连接邮件服务器 transport.connect(); // 发送邮件 Transport.send(msg); // 关闭连接 transport.close(); } //测试 public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { String ContentString = "<tr valign=\"middle\" style=\"line-height:2em;\">" + "<th>接口</th>" + "<th>执行结果</th>" + "<th>执行时间</th>" + "</tr>"; String ContentString2 = "<tr valign=\"middle\" style=\"color:black;background:#D1F3FE;line-height:2em;\">" + "<td align=\"left\">"+"/portal/home/pc/search/popup报告测试"+"</td>" + "<td align=\"center\">"+"成功"+"</td>" + "<td align=\"center\">"+"100ms"+"</td>" + "</tr>"; sendMail(5,5,0,ContentString+ContentString2,"/portal/home/pc/search/popup接口报告", "111","XXX@XXX.com.cn"); } }
MailTask类具体实现:
package org.programmerplanet.ant.taskdefs.jmeter; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.mail.MessagingException; import java.io.UnsupportedEncodingException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; public class MailTask extends Task { private String mailAddress=null; private String mailSubject=null; private String mailAttachment=null; private String resultLog=null; File resultLogFile; File htmlFile; //jtl文件 public void setResultLog(String resultLog) { this.resultLog = resultLog; } public String getResultLog() { return resultLog; } //邮件主题 public void setMailSubject(String mailSubject) { this.mailSubject = mailSubject; } public String getMailSubject() { return mailSubject; } //附件 html报告文件 public void setMailAttachment(String mailAttachment) { this.mailAttachment = mailAttachment; } public String getMailAttachment() { return mailAttachment; } //收件地址,多个逗号(英文)隔开 public void setMailAddress(String mailAddress) { this.mailAddress = mailAddress; } public String getMailAddress() { return mailAddress; } /** * @see org.apache.tools.ant.Task#execute() * task执行的入口 */ public void execute() throws BuildException { System.out.println("开始执行发送邮件task"); resultLogFile = new File(System.getProperty("user.dir")+resultLog); if (mailAddress != null && resultLogFile.exists()){ System.out.println("开始解析resultLog"); try { analyseResultLog(); } catch (MessagingException e) { e.printStackTrace(); } }else{System.out.println("resultLog不存在,请检查!");} } /** * 计算统计数据 * @throws MessagingException */ http://www.cnblogs.com/nzg-noway/p/6909821.html