首页 > 其他分享 >项目整合spring邮箱starter

项目整合spring邮箱starter

时间:2022-10-12 15:59:13浏览次数:72  
标签:String spring 发送 starter 邮箱 mail message com 邮件

邮件发送的基本过程与概念 (摘自小滴课堂大课)

邮件服务器

类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中

电子邮箱 :用户在邮件服务器上申请的一个账户

* from:[[email protected]](mailto:[email protected])  ----发件人
* to:[[email protected]](mailto:[email protected])   ----收件人
*  subject:hello     ----主题
* body: 欢迎来到小滴课堂 -----内容体

邮件传输协议

SMTP协议

全称为 Simple Mail Transfer Protocol,简单邮件传输协议。它定义了邮件客户端软件和SMTP邮件服务器之间,以及两台SMTP邮件服务器之间的通信规则

POP3协议

全称为 Post Office Protocol,邮局协议。它定义了邮件客户端软件和POP3邮件服务器的通信 规则

IMAP协议

全称为 Internet Message Access Protocol,Internet消息访问协议,它是对POP3协议一种扩展,也是定义了邮件客户端软件和IMAP邮件服务器的通信规则

整合流程

配置邮箱服务器(发送端使用QQ邮箱)

QQ邮箱地址 https://mail.qq.com

开启POP3/SMTP服务

img

开启后,保存客户端授权码,后面配置文件需要用到

配置项目依赖

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

新增项目配置

  # 邮箱配置
  mail:
    host: smtp.qq.com #发送邮件服务器
    username: [email protected] #QQ邮箱
    password: xxxxxxxxxxxxxxxx #客户端授权码
    protocol: smtp #发送邮件协议
    properties.mail.smtp.auth: true
    properties.mail.smtp.port: 465 #端口号465或587
    properties.mail.display.sendmail: Javen #可以任意
    properties.mail.display.sendname: Spring Boot Guide Email #可以任意
    properties.mail.smtp.starttls.enable: true
    properties.mail.smtp.starttls.required: true
    properties.mail.smtp.ssl.enable: true
    default-encoding: utf-8
    from: [email protected] #与上面的username保持一致

用户微服务 service封装

service

public interface MailService {

    /**
     * 发送邮件
     * @param to
     * @param subject
     * @param content
     */
    void sendMail(String to, String subject, String content);
}

serviceImpl

@Service
@Slf4j
public class MailServiceImpl implements MailService {

    /*
     * springboot 提供的一个发送邮件的简单抽象, 直接注入即可
     */
    @Autowired
    private JavaMailSender mailSender;

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

    /**
     * 发送邮件
     * @param to        收件人
     * @param subject   主题
     * @param content   内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {

        // 创建一个邮箱消息对象
        SimpleMailMessage message = new SimpleMailMessage();

        // 配置邮件发送人
        message.setFrom(from);

        // 邮件的收件人
        message.setTo(to);

        // 邮件的主题
        message.setSubject(subject);

        // 邮件的内容
        message.setText(content);

        // 发送邮件
        mailSender.send(message);

        log.info("邮件发送成功:{}", message.toString());

    }
}

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserApplication.class)
@Slf4j
public class MailTest {

    @Autowired
    private MailService mailService;


    @Test
    public void testSendMail(){
        mailService.sendMail("[email protected]", "邮件发送测试", "testtesttest123");

    }


}

标签:String,spring,发送,starter,邮箱,mail,message,com,邮件
From: https://www.cnblogs.com/isChenJY/p/16776506.html

相关文章

  • 关于 springcloud + nacos 启动报错:nacos save snapshot error
    关于nacos报错:nacossavesnapshoterror1:首先这个nacos报错并不影响你的正常使用,但是每次启动错误都会报错nacossavesnapshoterror,找不到config的配置;2:确......
  • 通过themyleaf模板显示spring-boot数据
    遍历后台返回数据,并动态生成表格 控制器返回ModelAndView前要指定视图名 在前端读取数据要用到的数据模型名要与后台返回时指定的名称一致 在用themyleaf模板前要在prope......
  • spring-boot 同时使用themyleaf和freemarker模板
    在pom中添加依赖 在templates目录中建立ftl文件与html文件 不同模板下调用方式如下:  ......
  • SpringCloud 微服务框架搭建
    框架及技术环境:IDEA、JDK1.8、MYSQL5.0、NacosServer2.1.1技术:SpringBoot+Nacos+SpringCloudGatway+SpringCloudOpenFegin+SpringSleuth+Maven目标用Maven搭建一个......
  • spring-boot配置属性注入到Bean
    1.在属性文件中配置book实体 2.创建配置属性对应实体类与控制器 3.输出实体时乱码在属性配置文件中加入spring.http.encoding.force=truespring.http.encoding.charset=UT......
  • 【云原生】Spring Cloud Alibaba 之 Feign 远程调用 实战
    文章目录​​一、什么是远程调用?​​​​⛅远程调用的原理​​​​二、RestTemplate与Feign的区别​​​​三、Feign远程调用实战开发​​​​⏳Feign替代RestTempla......
  • Nebula Graph介绍和SpringBoot环境连接和查询
    NebulaGraph介绍和SpringBoot环境连接和查询转载请注明来源https://www.cnblogs.com/milton/p/16784098.html说明当前NebulaGraph的最新版本是3.2.1,根据官方的文档......
  • SpringBoot的yml多环境配置3种方法
    方式一:多个yml文件 步骤一、创建多个配置文件application.yml#主配置文件application-dev.yml#开发环境的配置application-prod.yml#生产环境的配置applic......
  • Spring Boot 2.3.x 升级至 2.4.x 遇到的那些事
    随着SpringBoot3.0需要Java17和SpringFramework6作为最低版本。计划逐步升级系统的SrpingBoot版本,以应对未来的趋势,当前系统SpringBoot版本是2.3.12,继续先升即到......
  • springbean的八种加载方式
    总结了一下八种bean的加载方式bold;">接口 xml+<bean/> xml:context+注解(@Component+4个@Bean) 配置类+扫描+注解(@Component+4个@Bean)@Bean定义FactoryBean接口......