千家信息网

怎么用SpringBoot实现QQ邮箱发送邮件

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,本篇内容主要讲解"怎么用SpringBoot实现QQ邮箱发送邮件",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用SpringBoot实现QQ邮箱发送邮
千家信息网最后更新 2025年02月01日怎么用SpringBoot实现QQ邮箱发送邮件

本篇内容主要讲解"怎么用SpringBoot实现QQ邮箱发送邮件",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用SpringBoot实现QQ邮箱发送邮件"吧!

1.获取QQ邮箱授权码

2.导入邮箱发送依赖启动器

使用定制邮件模板的方法实现通用邮件发送,Thymeleaf构建邮件模板需要一起导入依赖。

                           org.springframework.boot            spring-boot-starter-mail                                    org.springframework.boot            spring-boot-starter-thymeleaf        

3.配置文件yml添加邮件服务配置

# Spring配置spring:  mail:    host: smtp.qq.com    username: ********@qq.com    # password是第一步QQ邮箱开通的smtp服务后得到的客户端授权码    password: ******************    default-encoding: UTF-8    properties:      mail:        smtp:          auth: true          starttls:            enable: true            required: true#thymeleaf模板引擎配置太简单,就不贴出来了

4.编写接口IMailService

public interface IMailService {    void sendHtmlMailThymeLeaf(String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content);}

5.编写实现MailServiceImpl

@Servicepublic class MailServiceImpl implements IMailService {    /**     * JavaMailSender是Spring Boot在MailSenderPropertiesConfiguration 类中配直好的,该类在 Mail     * 自动配置类 MailSenderAutoConfiguration 中导入 因此这里注入 JavaMailSender 就可以使用了     */    @Autowired    private JavaMailSender mailSender;    @Override    public void sendHtmlMailThymeLeaf(String mailFrom, String mailFromNick, String mailTo, String cc, String subject, String content) {        MimeMessage mimeMessage = mailSender.createMimeMessage();        try {            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);            mimeMessageHelper.setFrom(new InternetAddress(mailFromNick + " <" + mailFrom + ">"));            // 设置多个收件人            String[] toAddress = mailTo.split(",");            mimeMessageHelper.setTo(toAddress);            if (!StringUtils.isEmpty(cc)) {                mimeMessageHelper.setCc(cc);            }            mimeMessageHelper.setSubject(subject);            // 第二个参数为true表示邮件正文是html格式的,默认是false            mimeMessageHelper.setText(content, true);            mailSender.send(mimeMessage);        } catch (MessagingException e) {            System.out.println(e);        }    }}

6.Controller调用

    // 发件人要跟yml配置文件里填写的邮箱一致    String mailFrom = "******@qq.com";    // 收件人    String mailTo = "******@qq.com,******@qq.com";    // 抄送(可为空)    String cc = "******@qq.com";    // 注入mailService    @Autowired    private IMailService mailService;      // 注入TemplateEngine    @Autowired    TemplateEngine templateEngine;    @RequestMapping("/other/test")//请求路径    @ResponseBody    public void testMail() {        //注意1:这里我是查询对应的内容,使用富文本编辑器存储html标签的内容        Strategy strategy = strategyService.selectStrategyByStrategyId(Long.valueOf(1));               Context context = new Context(); // 导包是org.thymeleaf.context        //注意2:获取发送的内容传入thymeleaf模板中        context.setVariable("content", strategy.getStrategyContent());        String content = templateEngine.process("mailTemplate.html", context);        //System.out.println(content);        mailService.sendHtmlMailThymeLeaf(mailFrom, "定义发件人名字", mailTo, cc, "定义邮件标题", content);        System.out.println("邮件发送成功");    }

7.thymeleaf模板 mailTemplate.html

        邮件发送    
Some escaped text

到此,相信大家对"怎么用SpringBoot实现QQ邮箱发送邮件"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0