SpringBoot-邮件任务
2024-09-08 01:36:19
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
邮件发送需要引入spring-boot-start-mail
SpringBoot 自动配置MailSenderAutoConfiguration
定义MailProperties内容,配置在application.yml中
自动装配JavaMailSender
测试邮件发送
1、引入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、查看自动配置类:MailSenderAutoConfiguration
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({MimeMessage.class, MimeType.class, MailSender.class})
@ConditionalOnMissingBean({MailSender.class})
@Conditional({MailSenderAutoConfiguration.MailSenderCondition.class})
@EnableConfigurationProperties({MailProperties.class})
@Import({MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class})
public class MailSenderAutoConfiguration {
public MailSenderAutoConfiguration() {
} static class MailSenderCondition extends AnyNestedCondition {
MailSenderCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
} @ConditionalOnProperty(
prefix = "spring.mail",
name = {"jndi-name"}
)
static class JndiNameProperty {
JndiNameProperty() {
}
} @ConditionalOnProperty(
prefix = "spring.mail",
name = {"host"}
)
static class HostProperty {
HostProperty() {
}
}
}
}
然后我们去看下配置文件MailProperties
public class MailProperties {
private static final Charset DEFAULT_CHARSET;
private String host;
private Integer port;
private String username;
private String password;
private String protocol = "smtp";
private Charset defaultEncoding;
private Map<String, String> properties;
private String jndiName;
.....
}
MailProperties 中就是我们能自己去定义的属性。
3、配置文件:
spring.mail.username=1763204261@qq.com
spring.mail.password=qq邮箱授权码
spring.mail.host=smtp.qq.com
# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true
获取授权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务
4、Spring单元测试
@Autowired
JavaMailSenderImpl mailSender; @Test
public void emailTest(){
//邮件设置1:一个简单的邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("通知-你收到邮件了");
message.setText("神圣的时刻来了!"); message.setTo("1763204261@qq.com");
message.setFrom("1763204261@qq.com");
mailSender.send(message);
}
查看邮箱,邮件接收成功!
上面这个是比较简单的邮件发送,有时候会需要更复杂的,比如带入文件之类的:
此时需要用 MimeMessage
@Autowired
JavaMailSenderImpl mailSender; @Test
public void contextLoads2() throws MessagingException {
//邮件设置2:一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("通知-收到文件邮件");
helper.setText("<b style='color:red'>又是神圣的时刻!</b>",true); //发送附件
helper.addAttachment("1.jpg",new File("G:\\1.jpg")); helper.setTo("1763204261@qq.com");
helper.setFrom("1763204261@qq.com"); mailSender.send(mimeMessage);
}
测试
和预想的一样!
最新文章
- jquery——左右按钮点击切换一组图片功能
- render :template 和 render :parital
- Android 学习第15课,Android 开发的单元测试、及输出错误信息
- asp.net mvc4 设置build项目时,编译view页面
- Linux下Hadoop集群环境的安装配置
- C#实现汉字转换为拼音缩写的代码
- Delphi 对象的创建(create)与释放(free/destory)
- php的一些基本概念梳理
- HTML学习(四)样式
- Python2和Python3的差异
- 机器学习面试--一句话概括传统ML算法
- Redis整理
- Android高级_视频播放控件
- UEFI与 Legacy BIOS两种启动模式详解
- Dll封装dll,并且调用该封装的dll
- FDConnection
- iOS开发系列课程预告
- jQuery左右选择框
- 使用过多的递归出现错误,&ldquo;System.StackOverflowException&rdquo;类型的未经处理的异常在 mscorlib.dll 中发生
- row format delimited fields terminated by &#39;,&#39;
热门文章
- Learning ROS: Recording and playing back data
- MySQL 事务和锁
- Linux中MySQL的安装以及卸载
- golang error错误处理
- PyPDF2.py 合并pdf时报错问题
- JDK1.8源码(八)——java.util.HashMap类
- 20210811 Dove 打扑克,Cicada 与排序,Cicada 拿衣服
- node.js一头雾水
- NIO.2中Path,Paths,Files类的使用
- PTA——c++类与对象