通过spring发送邮件,定制模板

记录一次在工作使用Spring框架,发送邮件的demo。

依赖jar

注意一下,我使用的是gradle,使用maven的需要通过maven的方式引入jar包依赖。

compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'
compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
compile 'org.springframework:spring-context-support'

具体实现

@Service
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;

    public VelocityEngine getVelocityEngine() {
        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty("resource.loader", "class");
        velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.setProperty("input.encoding", "UTF-8");
        velocityEngine.setProperty("output.encoding", "UTF-8");
        velocityEngine.init();
        return velocityEngine;
    }

    private static MimeMessageHelper helper(MimeMessage mimeMessage)throws Exception{
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        List<String> tos = new ArrayList<>();
        // 接收邮件的地址 集合 可以为多个
//        tos.add("service@*.com");
        tos.add("service2@*.com");
        helper.setFrom("service@*.com");
        String[] strings = (String[])tos.toArray(new String[tos.size()]);
        helper.setTo(strings);
        return helper;
    }
    @Override
    public void sendMail(Map<String, Object> map) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = helper(mimeMessage);
        // 设置邮件主题
        helper.setSubject("overdue");
        // 需要填充到模板中的内容
        Map<String, Object> model = new HashedMap();
        model.put("hospital", map.get("hospital"));
        
        String template = (String) map.get("template");
        // 得到模板 并填充内容,模板地址根据自己的实际情况所定
        String text = VelocityEngineUtils.mergeTemplateIntoString(
                getVelocityEngine(), "template/" + template, "UTF-8", model);
        helper.setText(text, true);
        //  发送邮件
        mailSender.send(mimeMessage);
    }
}

模板实例

在resources目录下,新建一个template文件夹,新建一个 *.vm 文件,注意文件后缀为 .vm
如图所示:

image

模板中内容为:

<html>
<body>
<div>
    test
    <p>hospital: ${hospital}</p>
</div>
</body>
</html>

** 注意 ${hospital} 和service中model字段相对应。

配置发送邮件的密码

基本代码写好之后,我们需要在 application.yml 中配置一下邮件的用户名密码等;

spring:
  mail:
    default-encoding: utf-8
    host: smtp.exmail.qq.com # 邮箱host,不同的邮箱host是不一样的,这个列子是腾讯企业邮的,具体可以google。
    username: service@*.com # 发送邮箱的用户名, 替换为自己的
    password: 123456 # 填写自己邮箱的密码,如果是QQ邮箱,应该是需要填写授权码

测试用例

现在基本代码洗好了,我们接着写一个测试用例
我的项目所用的是springboot,具体情况视自己项目的情况而定。

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = OverdueApplication.class)
@ActiveProfiles("ubuntu")
@Ignore
public class MailTest {

    @Autowired
    private MailService mailService;

    @Test
    public void testSendMail() {
        Map<String, Object> map = new HashMap<>();
        // 模板中的变量
        map.put("hospital", "test hospital");
        // 对应的邮件模板 文件名
        map.put("template", "overdueMail.vm");
        try {
            mailService.sendMail(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

结果

image

这样,一件简单的邮件模板发送就成功了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,399评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,054评论 6 342
  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981阅读 16,138评论 2 11
  • 这两天起来感觉非常地累,打心底希望不要有那么多的生意,就让我这么恢复一下。这是我是这么想,可是现实生活的压力还是...
    曾曾的麻麻阅读 773评论 0 0
  • 我有梦想 但我的未来不是梦 只有保持全力以赴的奔跑 才有可能成功 每个人都渴望美好 就像每棵树都想 变成雕梁画栋 ...
    卧龙饮水阅读 1,319评论 0 0