maven依赖
org.springframework.boot
spring-boot-starter-mail
2.6.4
编写工具类
package cn.sdu.weblab.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Component
public class MailUtil {private static final Logger logger = LoggerFactory.getLogger(MailUtil.class);
//@Value("${spring.mail.username}")
private static String from="cjt18338538827@163.com";
private static JavaMailSender mailSender;
@Autowired
publicvoid setMailSender(JavaMailSender javaMailSender) {
this.mailSender = javaMailSender;
}/**
* 简单文本邮件
*
* @param to接收者邮件
* @param subject 邮件主题
* @param contnet 邮件内容
*/
public static voidsendSimpleMail(String to, String subject, String contnet) {SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(contnet);
message.setFrom(from);
boolean b = mailSender == null;
mailSender.send(message);
}/**
* HTML 文本邮件
*
* @param to接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @throws MessagingException
*/
public static void sendHtmlMail(String to, String subject, String contnet) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
mailSender.send(message);
}/**
* 附件邮件
*
* @param to接收者邮件
* @param subject邮件主题
* @param contnetHTML内容
* @param filePath 附件路径
* @throws MessagingException
*/
public static void sendAttachmentsMail(String to, String subject, String contnet,
String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
mailSender.send(message);
}/**
* 图片邮件
*
* @param to接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @param rscPath 图片路径
* @param rscId图片ID
* @throws MessagingException
*/
public static void sendInlinkResourceMail(String to, String subject, String contnet,
String rscPath, String rscId) {
logger.info("发送静态邮件开始: {},{},{},{},{}", to, subject, contnet, rscPath, rscId);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("发送静态邮件成功!");
} catch (MessagingException e) {
logger.info("发送静态邮件失败: ", e);
}}}
推荐阅读
- 笔记|SpringBoot发送邮件(详细学习笔记)
- java|来杯JAVA
- java|Java Web 利用 session 实现简单的购物车(数量可以累加)
- 基于时间戳的唯一标识符的轻量级跟踪方法
- Java-类型转换、常用运算符总结
- ubuntu|Ubuntu 安装配置
- 性能调优及MyBatis开启batch模式
- Redis数据结构实战演练,看看微博、微信、购物车、抽奖小程序是如何使用的()
- 某课 java全栈工程师(从java后端到全栈,高级电商全栈系统大课)