使用SpringBoot调用QQ的SMTP服务实现邮件发送功能

登录QQ邮箱 开启smtp服务

首先登录QQ邮箱 点击设置

在这里插入图片描述

在邮箱设置里选择账户

在这里插入图片描述

下拉找到如下位置. 开启POP3/SMTP服务

这里可能需要验证手机号 按照提示进行操作即可.

在这里插入图片描述
开启成功后 复制好授权码待用. (是连续的没有空格)
在这里插入图片描述

创建SpringBoot项目

引入依赖

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

修改SpringBoot配置文件

spring:
  mail:
    host: smtp.qq.com
    port: 465 #端口
    username: 123456@qq.com #自己的QQ账号
    password: zvylfdpzqhfjecah #密码使用授权码
    protocol: smtps #设置邮件协议 smtps

写一个发邮件的客户端

@Component
public class MailClient {

    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 
     * @param to 收件人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    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);
            //邮件内容,运行发送html页面
            helper.setText(content, true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败:" + e.getMessage());
        }
    }

}

/mail/demo.html模板

<!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>

测试

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests {

    @Autowired
    private MailClient mailClient;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 简单邮件
     */
    @Test
    public void testTextMail() {
        mailClient. sendMail("520@qq.com", "TEST", "Welcome.");
    }

    /**
     * 发送以HTML为模板的邮件
     */
    @Test
    public void testHtmlMail() {
        Context context = new Context();

        //设置传入模板的页面的参数 参数名为:id 参数随便写一个就行
        context.setVariable("username", "sunday");

        ///mail/demo是你要发送的模板我这里用的是Thymeleaf
        String content = templateEngine.process("/mail/demo", context);

        System.out.println(content);

        mailClient.sendMail("520@qq.com", "HTML", content);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Spring Boot发送邮件的步骤: 1. 导入Spring Boot提供的依赖[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在配置文件中设置邮箱信息: ```properties spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=your-email@qq.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建一个邮件服务类,用于发送邮件[^2]: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用邮件服务类的方法发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email."; emailService.sendEmail(to, subject, text); return "Email sent successfully."; } } ``` 以上是使用Spring Boot发送邮件的简单示例。你可以根据自己的需求进行相应的配置和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值