Community项目--注册之使用JavaMailSender发送邮件

注册之发送邮件

1.邮箱设置
启用客户端SMTP服务(以qq邮箱为例,其他类似)
在这里插入图片描述

pop就是接收邮件服务器的意思.
smtp就是发送邮件服务器的意思,即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.
2.Spring Email

2.1. 配置Maven

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.6.3</version>
</dependency>

2.2. 邮箱参数配置
查看qq邮箱官方文档进行配置
在这里插入图片描述

# MailProperties
#配置邮件服务器的域名和端口
spring.mail.host=smtp.qq.com
spring.mail.port=465

#配置发件人的账号密码
spring.mail.username=发件人邮箱账号
spring.mail.password=生成的授权码  !!注意不是你邮箱的密码,不然会报密码错误

#配置发送邮件的协议类型
spring.mail.protocol=smtps

#采用ssl连接发送邮件
spring.mail.properties.mail.smtp.ssl.enable=true

2.3. 使用JavaMailSender发送邮件

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.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author XH
 * @date 2022/1/31 9:27
 */
@Component
public class MailClient {
    // 记录日志
    private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

    // 使用JavaMailSender发送邮件
    @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);
            helper.setText(content, true);//true表示允许支持html文本
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            logger.error("发送邮件失败: " + e.getMessage());
        }
    }
}

JavaMailSender是Spring Email的核心组件,负责发送邮件.
MimeMessage用于封装邮件的相关信息.
MimeMessageHelper用于辅助构建MimeMessage对象.

2.4. 测试

import com.nowcoder.community.util.MailClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

/**
 * @author XH
 * @date 2022/1/31 16:15
 */
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)//使测试环境和正式配置类一样
public class MailTests {

    @Autowired
    private MailClient mailClient;

    @Test
    public void testMail(){
        mailClient.sendMail("接收方的邮箱", "测试", "Welcome");
    }
}

2.5. 测试结果
在这里插入图片描述
3.使用模板引擎

使用Thymeleaf发送HTML邮件

3.1. 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: rebeccapurple;" th:text="${username}"></span>!
    </p>
</body>
</html>

3.2. 测试

/**
 * @author XH
 * @date 2022/1/31 16:15
 */
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)//使测试环境和正式配置类一样
public class MailTests2 {

    @Autowired
    private TemplateEngine templateEngine;

    @Test
    public void testHtmlMail(){
        Context context = new Context();
        context.setVariable("username", "January");
        String content = templateEngine.process("/mail/demo", context);
        System.out.println(content);
        mailClient.sendMail("1737149293@qq.com","HTML", content);
    }
}

3.3. 测试结果
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值