栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

注册之发送邮件

1.邮箱设置
启用客户端SMTP服务(以qq邮箱为例,其他类似)

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

2.1. 配置Maven


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

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;


@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;

    
    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;


@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




    
    邮件示例


    

欢迎你, !

3.2. 测试

@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. 测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/723357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号