SpringBoot整合JavaMail
简单使用-发送简单邮件
- 介绍协议
文章来源地址https://www.toymoban.com/news/detail-608252.html
- 导入坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 添加配置
spring:
mail:
host: smtp.qq.com
username:
password: mrqzoenvhtwledfb
- 开启定时任务功能
@Override
public void sendMail() {
SimpleMailMessage message = new SimpleMailMessage();
// 设置发送右键的内容
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(context);
javaMailSender.send(message);
}
package com.ustc;
import com.ustc.service.SendService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Quick3ApplicationTests {
@Autowired
private SendService sendservice;// 注入接口
@Test
void contextLoads() {
sendservice.sendMail();// 接口调用方法
}
}
JavaMail发送多部件邮件
@Override
public void sendMail() throws MessagingException {
// SimpleMailMessage message = new SimpleMailMessage();
MimeMessage m = javaMailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(m);
// 设置发送右键的内容
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(context);
// 添加附件
File f1 = new File("D:\\1.txt");
message.addAttachment(f1.getName(),f1);
javaMailSender.send(message);
}
文章来源:https://www.toymoban.com/news/detail-608252.html
到了这里,关于SpringBoot整合JavaMail的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!