使用selenium进行自动化测试的时候,需要将运行结果以邮件形式周知。
一、邮件工具类
使用spring封装的javaMailSender发送邮件,并利用VelocityEngine渲染vmfile邮件模板。
private static JavaMailSenderImpl mailSender;
private static VelocityEngine velocityEngine;
public static void sendEmail(final String to, final String from, final String subject, final String vmFile, final Map<String, Object> context) {
try {
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setTo(to.split(","));
message.setFrom(from);
message.setSubject(subject);
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, vmFile, context);
message.setText(text, true);
logger.info("发送邮件: from:{} to:{} subject:{} content:{}", from, to, subject, text);
}
});
}catch (Exception e){
logger.error("发送邮件失败", e);
}
}
二、velocityEngine实例化
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath">
<value>classpath:</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="file.resource.loader.cache">false</prop>
<prop key="file.resource.loader.modificationCheckInterval">1</prop>
<prop key="velocimacro.library.autoreload">true</prop>
<prop key="velocity.engine.resource.manager.cache.enabled">false</prop>
<prop key="springMacro.resource.loader.cache">false</prop>
<prop key="eventhandler.referenceinsertion.class">org.apache.velocity.app.event.implement.EscapeXmlReference</prop>
<prop key="eventhandler.referenceinsertion.class">org.apache.velocity.app.event.implement.EscapeHtmlReference</prop>
<prop key="eventhandler.escape.html.match">/msg.*/</prop>
</props>
</property>
</bean>
三、mailSender
<!-- 发送邮件设置 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.smtp.host}" />
<property name="port" value="${mail.smtp.port}" />
<property name="username" value="${mail.smtp.username}" />
<property name="password" value="${mail.smtp.password}" />
<property name="defaultEncoding" value="UTF-8" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
四、VMfile模板
<table border="1" cellspacing="0" cellpadding="5" >
<tr bgcolor="aqua" align="center">
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
<td>xx</td>
</tr>
#foreach ($element in $!purchaseOrderData)
<tr #if ($element.lossPrice > 0)
bgcolor="#ffffe0" align="center"
#else
bgcolor="red" align="center"
#end>
<td>$element.airName</td>
<td>$element.orderNo</td>
<td>$element.pnr</td>
<td>$element.purchasePrice</td>
<td>$element.finalPrice</td>
<td>$element.lossPrice</td>
</tr>
#end
</table>
五、pom.xml
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
<version>2.0.6</version>
</dependency>