zoukankan      html  css  js  c++  java
  • Java

    1、spring + Java Mail + Velocity

      项目结构:

      

      注意:用户包中引入各包的顺序问题。如velocity-2.1。

      beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
        
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
        
        <bean id="propertyConfigurer" 
             class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="locations" value="mail.properties"/>
        </bean>
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
           <property name="host" value="${mail.host}"/>
           <property name="username" value="${mail.user}"/>
           <property name="password" value="${mail.pwd}"/>
        </bean>
        
        <bean id="javaMailSenderService" class="com.lfy.sendmail.JavaMailSenderService"/>
        <bean id="javaMailSenderImplService" class="com.lfy.sendmail.JavaMailSenderImplService"/>
        
        <!-- VelocityEngineFactory -->
        <bean id="velocityEngineFactory" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
          <property name="velocityProperties">
             <props>
                 <prop key="resource.loader">class</prop>
                 <prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
             </props>
          </property>
        </bean>
        
        <bean id="velocityEngineService" class="com.lfy.velocity.VelocityEngineService"/>
    
    </beans>

      VelocityEngineService.java

    package com.lfy.velocity;
    
    import org.apache.velocity.app.VelocityEngine;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class VelocityEngineService {
    
        @Autowired
        private VelocityEngine velocityEngine;
    
        public VelocityEngine getVelocityEngine() {
            return velocityEngine;
        }
    
        public void setVelocityEngine(VelocityEngine velocityEngine) {
            this.velocityEngine = velocityEngine;
        }
    }

       JavaMailSenderImplService.java

    package com.lfy.sendmail;
    
    import javax.mail.internet.MimeMessage;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    
    /**
     * 2、spring支持的第二种发送器JavaMailSenderImpl
     * @author lfy
     *
     */
    public class JavaMailSenderImplService {
    
        @Autowired
        private JavaMailSenderImpl javaMailSenderImpl;
        
        public JavaMailSenderImpl getJavaMailSenderImpl() {
            return javaMailSenderImpl;
        }
    
        public void setJavaMailSenderImpl(JavaMailSenderImpl javaMailSenderImpl) {
            this.javaMailSenderImpl = javaMailSenderImpl;
        }
    
        /**
         * simple content
         * @param message
         */
        public void send(SimpleMailMessage message){
            javaMailSenderImpl.send(message);
            System.out.println("JavaMailSenderImpl:send silpleMessage successfully.");
        }
        
        /**
         * Velocity content
         * @param message
         */
        public void sendWithVelocity(MimeMessage message) {
            javaMailSenderImpl.send(message);
            System.out.println("JavaMailSenderImpl:send mimeMessage successfully.");
        }
    }

       index.vm

    <html>
    <head>
    <style type="text/css">
    h4{
        color:red;
        background:#efefef;
    }
    </style>
    </head>
    <body>
    <h4>${user} </h4>
    <p><p>
    <i>${content}</i>
    </body>
    </html>

       springJavaMailSender.java

    package com.lfy.main;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    import org.apache.velocity.app.VelocityEngine;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.ui.velocity.VelocityEngineUtils;
    
    import com.lfy.sendmail.JavaMailSenderImplService;
    import com.lfy.velocity.VelocityEngineService;
    
    public class springJavaMailSender {
    
        public static void main(String[] agrs) throws MessagingException {
            //创建spring容器
            ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
            JavaMailSenderImplService javaMailSenderImplService = (JavaMailSenderImplService)ctx.getBean("javaMailSenderImplService");
            
            VelocityEngine velocityEngine=(VelocityEngine)((VelocityEngineService)ctx.getBean("velocityEngineService")).getVelocityEngine();
            Map<String,Object> model=new HashMap<String,Object>();
            model.put("user", "Tomcat");
            model.put("content", "Hello");
            String emailText=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "index.vm", "UTF-8", model);
            MimeMessage msg=javaMailSenderImplService.getJavaMailSenderImpl().createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(msg,true);
            helper.setFrom("xxxxxxxxxx@163.com");
            helper.setTo("xxxxxxxx@qq.com");
            helper.setCc("xxxxxxxx@163.com");
            helper.setSubject("Velocity模板测试");
            helper.setText(emailText, true);
            javaMailSenderImplService.sendWithVelocity(msg);
        }
    }

      运行效果:

    2、使用Maven配置使用Velocity。整理中...

    3、Velocity加载模板的3中方式

     1》从文件加载模板文件(默认方式)

    properties.setProperty("resource.loader", "file");
    //设置velocity资源加载方式为file时的处理类
    properties.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");

     2》从类路径加载模板文件

    properties.setProperty("resource.loader", "class");
    //设置velocity资源加载方式为file时的处理类
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

     3》从jar文件中加载模板文件

    properties.setProperty("resource.loader", "jar");
    //设置velocity资源加载方式为file时的处理类
    properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
    //设置jar包所在的位置
    properties.setProperty("jar.resource.loader.path", "jar:file:/F:/quicksure_Server_Provider.jar");
  • 相关阅读:
    [POJ1195] Mobile phones(二维树状数组)
    [SWUST1740] 圆桌问题(最大流)
    [SWUST1759] 骑士共存问题(最大流,最大独立集)
    欧拉函数O(sqrt(n))与欧拉线性筛素数O(n)总结
    BZOJ 1036: [ZJOI2008]树的统计Count-树链剖分(点权)(单点更新、路径节点最值、路径求和)模板,超级认真写了注释啊啊啊
    POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
    计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)
    洛谷 P3383 【模板】线性筛素数-线性筛素数(欧拉筛素数)O(n)基础题贴个板子备忘
    计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)
    计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)
  • 原文地址:https://www.cnblogs.com/ZeroMZ/p/11478956.html
Copyright © 2011-2022 走看看