SpringBoot实现发邮件
作者:互联网
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.导入配置
spring:
mail:
default-encoding: UTF-8
username: 18747633468@163.com #1135365441@qq.com
password: SISQHSNBBOXFEJNW #tynmdlqdelfpjiha
host: smtp.163.com #smtp.qq.com
properties:
mail:
smtp:
ssl:
enable: true
3.工具类
package com.atguigu.blog.util;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
public class EmailUtil {
// 默认邮箱配置
private static final String FROM = "1899999468@163.com";
private static final String HOST = "smtp.163.com";
private static final String PASSWORD = "SISQHSNBBOXFEJNW";
/**
* 自定义发件人邮箱
* @param from 发件人邮箱
* @param to 收件人邮箱
* @param subject 邮件标题
* @param text 邮件内容
* @throws IOException
* @throws MessagingException
*/
public static void sendMail(String from, String[] to, String host, String password, String subject, String text, List<File> fileList) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setUsername(from);
mailSender.setPassword(password);
Properties prop = new Properties();
prop.put("mail.smtp.auth", false);// 参数设为true,需服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.timeout", 5000);
mailSender.setJavaMailProperties(prop);
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
mailSender.setHost(host);
mailSender.setPassword(password);
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setSubject(subject);
messageHelper.setText(text, true);
messageHelper.setTo(to);
messageHelper.setFrom(from);
if (fileList.size() != 0) {
for (File file : fileList) {
messageHelper.addAttachment(file.getName(), file);
}
}
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 默认发件人邮箱
* @param to 收件人邮箱
* @param subject 标题
* @param text 内容
* @param fileList 文件
*/
public static void sendMailNoFrom(String[] to, String subject, String text, List<File> fileList) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(HOST);
mailSender.setUsername(FROM);
mailSender.setPassword(PASSWORD);
Properties prop = new Properties();
prop.put("mail.smtp.auth", false);// 参数设为true,需服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.timeout", 5000);
mailSender.setJavaMailProperties(prop);
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setSubject(subject);
messageHelper.setText(text, true);
messageHelper.setTo(to);
messageHelper.setFrom(FROM);
if (fileList.size() != 0) {
for (File file : fileList) {
messageHelper.addAttachment(file.getName(), file);
}
}
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
4.测试
@GetMapping("/sendtoqq")
@Log("测试注解")
public Result sendtoqq() {
// 163
String[] to = {"1135365@qq.com", "3654666661@outlook.com"};// 收件人
List<File> files = new ArrayList<>();
File file = new File("/Users/lizhenghang/Desktop/网上购物系统设计与实现.docx");
files.add(file);
EmailUtil.sendMailNoFrom(to, "测试标题to qq", "测试内容to qq", files);
return Result.ok("发送成功!");
}
@GetMapping("/sendto163")
@Log("测试注解")
public Result sendto163() {
String from = "11366669991@qq.com";// 发件人邮箱
String[] to = {"186666468@163.com"};// 收件人邮箱,支持传
String password = "tynmdlqdelfpjiha";
String host = "smtp.qq.com";
List<File> files = new ArrayList<>();
File file = new File("/Users/lizhenghang/Desktop/网上购物系统设计与实现.docx");
files.add(file);
EmailUtil.sendMail(from, to, host, password, "测试标题11111 to 163", "测试内容11111 to 163", files);
return Result.ok("发送成功!");
}
标签:发邮件,mailSender,SpringBoot,实现,messageHelper,file,mail,com,String 来源: https://blog.csdn.net/qq_30398499/article/details/115636434