zoukankan      html  css  js  c++  java
  • java

      使用 thymeleaf 制作邮件模版还是很简单的,先推荐官方文档:https://www.thymeleaf.org/doc/articles/springmail.html

      不过,该文档没给出项目结构以及文件路径,所以可能有点不知道该怎么放置比较好,但文档里给出了github上的示例:https://github.com/thymeleaf/thymeleafexamples-springmail

      我是复制粘贴的,有小部分用不上,但其实基本上可以git下来用,因为用上的还是大部分。

      需要注意的是,文档示例不是 spring boot 的,而是 MVC 的。spring boot + thymeleaf 查找模板默认从 ../resources/templates/ 下找,而且.txt 文件模板必须要加上后缀,不然默认找 .html,所以如过用 txt 模板不加后缀会找不到,这些是文档中没说的,还有一点就是代码不需要任何 config 部分,首先是spring boot 帮你做了大部分事情,然后还有一小部分只需要加到 application.properties 文件中就可以了。

      关于 pom.xml 其实只用加上下面两个就可以了:

    <dependency>
    	<groupId>commons-io</groupId>
    	<artifactId>commons-io</artifactId>
    	<version>2.6</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

      因为,git上的一些不是 spring boot 需要的。

      然后配置部分的:

    spring.mail.host=smtp.qq.com
    spring.mail.port=465 # qq 的似乎必须要写这个,我用过 587 但会得到 SSL 错误
    spring.mail.protocol=smtp
    spring.mail.username=xxx # 邮箱
    spring.mail.password=xxx # 授权码
    spring.mail.defaultEncoding=UTF-8
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    spring.mail.properties.mail.smtp.socketFactory.fallback=false

      我们要用的是下图几个部分。

      处理模板以及发送的服务部分:

      发送邮件部分代码:

       模板部分(这部分看你用 html 模板还是 txt,html 的支持嵌入图像、添加附件等):

       

  • 相关阅读:
    HDU
    洛谷- P1306 斐波那契公约数
    HDU
    HDU
    POJ3321
    SPOJ
    HDU
    POJ 1236 Network of Schools
    POJ-3169 Layout (差分约束+SPFA)
    POJ-1062 昂贵的聘礼 (最短路)
  • 原文地址:https://www.cnblogs.com/darkchii/p/12612869.html
Copyright © 2011-2022 走看看