SpringBoot 中实现邮件发送功能

背景

在项目中有相关需求,需要发送相关数据到指定邮箱,发现在 SpringBoot 上实现起来非常简单,这里记录一下。这里以 maven 项目为例实现,如果是 gradle 项目只需改一下,依赖配置即可。

具体实现
添加依赖

首先,在 pom.xml 文件中添加相关依赖,

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置帐号信息

其次,在SpringBoot 的 application.properties 配置文件中,添加邮箱相关配置,

spring.mail.host=smtp.163.com
spring.mail.username=你的163邮箱    
spring.mail.password=注意,这里不是邮箱密码,而是 SMTP 授权密码
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
获取 SMTP 授权密码

以 163 邮箱为例,首先要登录你的 163 邮箱帐号;登录成功后,依次进入,设置 -> POP3/SMTP/IMAP -> 客户端授权密码 -> 选择”开启”,参考下面相关截图。

POP3/SMTP/IMAP截图
在这里插入图片描述

客户端授权密码开启截图
在这里插入图片描述
开启时,会提示你设置授权密码,记好这个密码,配置 application.properties 时要用到。

实现邮件发送类

定义实现邮件发送类 MyMailSender,代码如下,

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class MyMailSender {
     
     static final String SUBJECT_PREFIX = " Test";
     static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
     @Autowired
     private JavaMailSender mMailSender;
     
     synchronized public void sendEmail(String content) {
         SimpleMailMessage message = new SimpleMailMessage();
         message.setTo(“xxx@163.com”);  //设置发送到哪个邮箱,可设置多个
         message.setFrom(“xxxxxx@163.com”);  //设置从哪个邮箱发送
         Date date = Calendar.getInstance().getTime();
         String subject = DATE_FORMAT.format(date) + SUBJECT_PREFIX;
         message.setSubject(subject);
         
         message.setText(content);
         
         mMailSender.send(message);
     }
}

在 sendEmail() 方法中,首先创建了一个 SimpleMailMessage 对象,然后设置相关信息,包括:fromEmail, toEmail, 主题,内容。

SimpleMailMessage 对象构建完成后,调用 JavaMailSender 对象 mMailSender 的 send() 方法即完成发送。

注意:mMailSender 没有主动创建对象,而是通过 @Autowired 注解配置一下,就可以直接使用。这是因为 SpringFramework 中已经定义好这个 Bean,使用 @Autowired 注解后,SpringBoot 运行时会自动装载这个 Bean 对象。

另外,在定义 MyMailSender 类时,使用了注解 @Component,这相当于告知了 SpringFramework 这个类是一个 Bean。

发送邮件

在 MyApplication 类中,触发邮件发送操作,MyApplication 的实现如下,

@EnableScheduling
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
     @Autowired
     private MyMailSender myMailSender;
     @Override
     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
         return builder.sources(MyApplication.class);
     }
     public static void main(String[] args) {
         SpringApplication.run(MyApplication.class, args);
     }
     
     //Seconds Minutes Hours DayofMonth Month DayofWeek Year
     //Seconds Minutes Hours DayofMonth Month DayofWeek
     @Scheduled(cron = "58 29,59 * ? * *")    //每小时的 29分58秒,59分58秒触发
     public void dumpUsageStats() {
         String content = “这是内容";
         myMailSender.sendEmail(content);
     }
}

在 MyApplication 类中,dumpUsageStats 方法中会调用 MyMailSender 的 sendEmail() 方法发送邮件。

MyMailSender 对象有 @Autowired 注解声明,对象实例由 SpringBoot 自动装载,这里能装载成功,是由于在 MyMailSender 类中使用了 @Component 注解,相当于告知了 SpringFramework 这个类是一个 Bean,SpringFramework 在需要这个 Bean 的地方进行自动装载。

这里的邮件发送触发时机通过 @Scheduled 配置,定义为每小时的 29分58秒和59分58秒触发。你也可以直接在需要的地方进行配置和发送,不影响。

总结

从上面的实现可以看出,发送邮件实现非常简单,SpringBoot 已经帮你做好了很多事情,只需要正确配置,并且调用 SpringBoot 封装好的代码即可。

对于程序员来说,开发越来越简单,是好事也是坏事。
欢迎关注我的公众号,一起进步!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToSimpleL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值