SpringBoot发送模板邮件

作者: 0_cc | 来源:发表于2017-08-03 13:24 被阅读153次

    1、引入freemarker模块的依赖:

    org.springframework.bootspring-boot-starter-freemarker

    2、在配置文件application.properties中加入配置信息:

    spring.mail.host=邮箱主机

    spring.mail.username=用户名

    spring.mail.password=密码,一般这里是填写授权码

    spring.mail.properties.mail.smtp.auth=true

    spring.mail.properties.mail.smtp.starttls.enable=true

    spring.mail.properties.mail.smtp.starttls.required=true

    spring.freemarker.allow-request-override=false

    spring.freemarker.cache=true

    spring.freemarker.check-template-location=true

    spring.freemarker.charset=UTF-8

    spring.freemarker.content-type=text/html

    spring.freemarker.expose-request-attributes=false

    spring.freemarker.expose-session-attributes=false

    spring.freemarker.expose-spring-macro-helpers=false

    3、在resources中,新建一个名为templates的文件夹,并在其中创建一个模板页面email.ftl:

    你好, ${username}, 这是一封模板邮件!

    4、发送邮件代码如下:


    ❌出现错误:

    异常如下:

    Exception in thread "main" org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException

    Caused by: javax.mail.AuthenticationFailedException

    遇到这个异常首先检查用户名密码配置是否正确,如果正确那就可能是邮箱的POP3/SMTP/IMAP服务没有开启,网易邮箱是默认关闭的,QQ邮箱也是默认关闭的。可以在邮箱的设置中开启POP3/SMTP/IMAP服务,但是开启该服务以后网易邮箱客户端和配置文件中的登录密码就变成重新设置的授权码了(记得修改配置文件中的密码为授权码,否则还是会报上述错误)。

    相关文章

      网友评论

        本文标题:SpringBoot发送模板邮件

        本文链接:https://www.haomeiwen.com/subject/xwfplxtx.html