• SpringBoot发送邮件


    邮箱设置

    首先要开启设置POP3/SMTP/IMAP服务,因为邮件发送都是需要调用这些协议来发送,不论是QQ邮箱或者是网易新浪的邮箱的设置都差不多。

    找到设置,在找到POP3/SMTP/IMAP,选择开启:

    image.png

    使用Spring Mail发送邮件

    加入依赖:

    <dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-mail</artifactId>
    			<version>2.1.5.RELEASE</version>
    		</dependency>
    

    添加邮箱参数配置:(根据自己的邮箱自行配置host,用户名,密码)

    注意:该处的密码是开启SMTP的授权码而非登录密码

    # MailProperties
    spring.mail.host=smtp.163.com
    spring.mail.port=465
    spring.mail.username=xxx@qq.com
    spring.mail.password=xxxxxxxx
    spring.mail.protocol=smtps
    spring.mail.properties.mail.smtp.ssl.enable=true
    

    邮件发送工具类:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Component;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    @Component
    public class MailClient {
    
        private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
    
        @Autowired
        private JavaMailSender mailSender;
    
        @Value("${spring.mail.username}")
        private String from;
    
        public void sendMail(String to, String subject, String content) {
            try {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(content, true);
                mailSender.send(helper.getMimeMessage());
            } catch (MessagingException e) {
                logger.error("发送邮件失败:" + e.getMessage());
            }
        }
    
    }
    

    测试邮件发送:

    import com.nowcoder.community.util.MailClient;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = CommunityApplication.class)
    public class MailTests {
    
        @Autowired
        private MailClient mailClient;
    
        @Test
        public void testTextMail() {
            mailClient.sendMail("2609076192@qq.com", "TEST", "Welcome.");
        }
    }
    
    

    使用 Thymeleaf 发送 HTML 邮件

    这里介绍一下使用模板引擎Thymeleaf发送邮件,使用它的好处就是发送的内容能够动态的生成。

    创建一个邮件发送模板放在template目录下:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>邮件示例</title>
    </head>
    <body>
        <p>欢迎你, <span style="color:red;" th:text="${username}"></span>!</p>
    </body>
    </html>
    

    测试发送:

    注:templateEngine.process()内对应的参数为模板文件的路径,根据此模板文件拿到解析后的字符串

    import com.nowcoder.community.util.MailClient;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = CommunityApplication.class)
    public class MailTests {
    
        @Autowired
        private MailClient mailClient;
    
        @Autowired
        private TemplateEngine templateEngine;
    
        @Test
        public void testHtmlMail() {
            Context context = new Context();
            context.setVariable("username", "Chen");
    
            String content = templateEngine.process("/mail/demo", context);
            System.out.println(content);
    
            mailClient.sendMail("2609076192@qq.com", "HTML", content);
        }
    
    }
    
  • 相关阅读:
    Bailian 2808 校门外的树(入门线段树)
    C++对运算符进行重载学习笔记
    C++构造函数和析构函数什么情况下会用
    C++ 2048游戏
    输入输出速度统计
    左偏树(可并堆) [模板]
    Luogu [P3622] [APIO2007]动物园
    Luogu [P1248] 加工生产调度
    Luogu [P2814] 家谱
    练习盲打的网站 / 练习打字的网站
  • 原文地址:https://www.cnblogs.com/chen88/p/11898877.html
Copyright © 2020-2023  润新知