selenium---邮件模板

使用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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值