测试报告邮件发送
一配置pom.xml:
邮件
<!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
testng:
so.dian.DianInterface.common.SendMail 为邮件类
<!--添加插件 关联testNg.xml--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <testFailureIgnore>true</testFailureIgnore> <suiteXmlFiles> <file>res/testNG.xml</file> </suiteXmlFiles> <workingDirectory>target/</workingDirectory> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <includePluginDependencies>true</includePluginDependencies> <mainClass>so.dian.DianInterface.common.SendMail</mainClass> </configuration> </execution> </executions> </plugin>
二 压缩生成的结果
创建压缩工具:
package so.dian.DianInterface.common.httpRequest; /** * @Author:xiwu * @Description * @Date;Created in ${time} 2018/12/14. */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.log4j.Logger; /** * @ClassName: ZipCompressor * @Description: 压缩文件的通用工具类-采用org.apache.tools.zip.ZipOutputStream实现,较复杂。 */ public class ZipCompressor { private Logger logger = Logger.getLogger(ZipCompressor.class); static final int BUFFER = 8192; private File zipFile; /** * 压缩文件构造函数 * * @param pathName 压缩的文件存放目录 */ public ZipCompressor(String pathName) { zipFile = new File(pathName); } /** * 执行压缩操作 * * @param srcPathName 被压缩的文件/文件夹 */ public void compressExe(String srcPathName) { File file = new File(srcPathName); if (!file.exists()) { throw new RuntimeException(srcPathName + "不存在!"); } try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; compressByType(file, out, basedir); out.close(); } catch (Exception e) { e.printStackTrace(); logger.error("执行压缩操作时发生异常:" + e); throw new RuntimeException(e); } } /** * 判断是目录还是文件,根据类型(文件/文件夹)执行不同的压缩方法 * * @param file * @param out * @param basedir */ private void compressByType(File file, ZipOutputStream out, String basedir) { /* 判断是目录还是文件 */ if (file.isDirectory()) { logger.info("压缩:" + basedir + file.getName()); this.compressDirectory(file, out, basedir); } else { logger.info("压缩:" + basedir + file.getName()); this.compressFile(file, out, basedir); } } /** * 压缩一个目录 * * @param dir * @param out * @param basedir */ private void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) { return; } File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { /* 递归 */ compressByType(files[i], out, basedir + dir.getName() + "/"); } } /** * 压缩一个文件 * * @param file * @param out * @param basedir */ private void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
对文件进行压缩
public static void testzip(String basepath,String path){ //压缩后的存放路径 try { File file = new File(path); if (file.exists()){ file.delete(); } ZipCompressor zc = new ZipCompressor(path); //压缩谁 String Zpath = basepath+"/target/surefire-reports/html"; zc.compressExe(Zpath); } catch (Exception e) { e.printStackTrace(); System.out.println("+++++++++压缩失败+++++++++++++++"); } }
三:邮件发送结果报告
邮件配置:
阿里云
public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.transport.protocol", "smtp"); //协议 prop.setProperty("mail.smtp.host", "smtp.mxhichina.com"); //主机名 prop.setProperty("mail.smtp.auth", "true"); //是否开启权限控制 prop.setProperty("mail.debug", "true"); //返回发送的cmd源码 prop.setProperty("mail.smtp.port", "465");//465/994 // prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // prop.setProperty("mail.smtp.socketFactory.fallback", "false"); // prop.setProperty("mail.smtp.socketFactory.port", "465"); //开启SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); // 创建Authenticator内部类,重写getPasswordAuthentication()方法 Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("email", "password"); } }; Session session = Session.getInstance(prop); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("email")); //自己的email msg.setRecipient(Message.RecipientType.TO, new InternetAddress("email")); // 要发送的email,可以设置数组 msg.setSubject("UI接口自动化测试报告");//邮件标题 // msg.setText("HI,欢迎测试!");//邮件正文 String basepath = System.getProperty("user.dir"); String path = basepath+"/target/surefire-reports/html/html.zip"; // testzip(basepath,path); // File file = new File(path); // System.out.println("filename =========="+fileName); Multipart mp = new MimeMultipart(); MimeBodyPart mbpfile = new MimeBodyPart(); FileDataSource fds = new FileDataSource(path); try { mbpfile.setDataHandler(new DataHandler(fds)); mbpfile.setFileName(fds.getName()); mp.addBodyPart(mbpfile); } catch (MessagingException e) { e.printStackTrace(); } msg.setContent(mp); // msg.setText("结果查看附件"); //不被当作垃圾邮件的关键代码--Begin ,如果不加这些代码,发送的邮件会自动进入对方的垃圾邮件列表 msg.addHeader("X-Priority", "3"); msg.addHeader("X-MSMail-Priority", "Normal"); msg.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); //本文以outlook名义发送邮件,不会被当作垃圾邮件 msg.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); msg.addHeader("ReturnReceipt", "1"); msg.saveChanges(); // 不被当作垃圾邮件的关键代码--end Transport trans = session.getTransport(); trans.connect("smtp.mxhichina.com", "username", "password");//邮箱账号和密码 trans.sendMessage(msg, msg.getAllRecipients()); trans.close(); }
网易:
/** * @Author:xiwu * @Description 发送邮件 * @Date;Created in ${time} 2018/12/13. */ public class SendMail { /** * 网易邮箱 * * @throws Exception */ public void test() throws Exception { Properties prop = new Properties(); prop.setProperty("mail.transport.protocol", "smtp"); //协议 prop.setProperty("mail.smtp.host", "smtp.163.com"); //主机名 prop.setProperty("mail.smtp.auth", "true"); //是否开启权限控制 prop.setProperty("mail.debug", "true"); //返回发送的cmd源码 prop.setProperty("mail.smtp.port", "465");//465/994 // prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // prop.setProperty("mail.smtp.socketFactory.fallback", "false"); // prop.setProperty("mail.smtp.socketFactory.port", "465"); //开启SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); //创建Authenticator内部类,重写getPasswordAuthentication()方法 Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("XX@163.com", "keyPassword"); } }; Session session = Session.getInstance(prop, auth); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("XX@163.com")); //自己的email msg.setRecipient(Message.RecipientType.TO, new InternetAddress("XXX")); // 要发送的email,可以设置数组 msg.setSubject("测试邮件");//邮件标题 // msg.setText("HI,欢迎测试!");//邮件正文 msg.setContent("你好,感谢你阅读", "text/html;charset=utf-8"); //不被当作垃圾邮件的关键代码--Begin ,如果不加这些代码,发送的邮件会自动进入对方的垃圾邮件列表 msg.addHeader("X-Priority", "3"); msg.addHeader("X-MSMail-Priority", "Normal"); msg.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); //本文以outlook名义发送邮件,不会被当作垃圾邮件 msg.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); msg.addHeader("ReturnReceipt", "1"); msg.saveChanges(); // 不被当作垃圾邮件的关键代码--end Transport trans = session.getTransport(); trans.connect("smtp.163.com", "XX@163.com", "keyPasswor"); trans.sendMessage(msg, msg.getAllRecipients()); trans.close(); }
执行命令:mvn clean test -DxmlFileName=testNG.xml -Pcode-sendMail