zoukankan      html  css  js  c++  java
  • spring 模板发送邮件

    一、jar包

    velocity-1.7.jar
    velocity-tools-2.0.jar
    mail.jar
    spring-context-support-4.0.6.RELEASE.jar

    二、配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
        
        <bean id="velocityEngine"
            class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
            <property name="velocityProperties">
                <props>
                    <prop key="resource.loader">file</prop>
                    <prop key="file.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">3</prop>
                    <prop key="input.encoding">UTF-8</prop>
                    <prop key="output.encoding">UTF-8</prop>
                </props>
            </property>
        </bean>
    
        <!--邮件发送实现类 -->
        <bean id="javaMailSender" 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="javaMailProperties">
                <props>
                    <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
                    <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                    <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
                    <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
                    <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                    <prop key="mail.smtp.socketFactory.fallback">false</prop>
                </props>
            </property>
        </bean>
    
    
    </beans>

    代码

    package com.ljx.model;
    
    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.beans.factory.annotation.Autowired;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Component;
    import org.springframework.ui.velocity.VelocityEngineUtils;
    
    @Component
    public class SendTemplateMail {
    
        @Autowired
        private JavaMailSenderImpl mailSender;
        @Autowired
        private VelocityEngine velocityEngine;
    
        public void sendEmail() throws MessagingException {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "GBK");
            helper.setFrom("111111111@qq.com");
            helper.setTo("222222222@qq.com");
            helper.setSubject("测试HTM主题");
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("userName", "xxx");
            String mailText = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "mail.vm", "GBK", model);
            System.out.println(mailText);
            helper.setText(mailText, true);
            mailSender.send(message);
        }
    
    }

    模板mail.vm

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <h1>欢迎</h1>
    
    <div>
        您好${userName}
    </div>
    </body>
    </html>

    controllor

    @RequestMapping(value = "/mail.action")
        public @ResponseBody String mail() {
    
            try {
                System.out.println(1);
                mail.sendEmail();
            } catch (MessagingException e) {
    
                e.printStackTrace();
                return "false";
            }
    
            return "ok";
        }
  • 相关阅读:
    科学计算和可视化,做数据分析与雷达图。
    Leetcode 429 N叉树的层序遍历
    Leetcode 867转置矩阵
    Leetcode 832 翻转图像
    Leetcode 1052 爱生气的书店老板
    Leetcode 337打家劫舍 III
    Leetcode 766 托普利茨矩阵
    Leetcode 1438绝对差不超过限制的最长连续子数组
    Leetcode 697 数组的度
    Leetcode 567 字符串的排列
  • 原文地址:https://www.cnblogs.com/lijinxin/p/6036692.html
Copyright © 2011-2022 走看看