引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
编写 application.properties 文件
#邮箱服务器
#关于服务器设置 https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=369
spring.mail.host=smtp.qq.com
#邮箱账户
spring.mail.username=1310072293@qq.com
#QQ邮箱第三方授权码
spring.mail.password=第三方授权码
#编码类型
spring.mail.default-encoding=UTF-8
具体代码
package me.yundongis.service.impl;
import me.yundongis.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailServiceImpl implements MailService {
//注入application.properties中指定的用户名
@Value("${spring.mail.username}")
private String from;
//用于发送文件
@Autowired
private JavaMailSender mailSender;
/**
* 发送普通文本邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);//收信人
message.setSubject(subject);//主题
message.setText(content);//内容
message.setFrom(from);//发信人
mailSender.send(message);
}
/**
* 发送HTML邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容(可以包含<html>等标签)
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
//使用MimeMessage,MIME协议
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
//MimeMessageHelper帮助我们设置更丰富的内容
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
//true代表支持html
helper.setText(content, true);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送带附件的邮件
*
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件路径
*/
@Override
public void sendAttachmentMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
//true代表支持多组件,如附件,图片等
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送带图片的邮件
*
* @param to 收件人
* @param subject 主题
* @param content 文本
* @param rscPath 图片路径
* @param rscId 图片ID,用于在<img>标签中使用,从而显示图片
*/
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
// 添加图片
helper.addInline(rscId, res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Controller
package me.yundongis.controller;
import me.yundongis.service.impl.MailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
//发送操作
@Autowired
private MailServiceImpl mailServiceimpl;
/**
* 发送普通邮件
*/
@RequestMapping("/sendSimpleMail")
public Object sendSimpleMail() {
mailServiceimpl.sendSimpleMail("1637378732@qq.com", "hello", "this is send mail test");
return "OK";
}
/**
* 发送HTML邮件
*/
@RequestMapping("/sendHtmlMail")
public Object sendHtmlMail() {
String html = "<h1>Hello</h1>";
mailServiceimpl.sendHtmlMail("1637378732@qq.com", "hello", html);
return "OK";
}
/**
* 发送带附件的邮件
*/
@RequestMapping("/sendAttachmentMail")
public Object sendAttachmentMail() {
String html = "<h1>Hello</h1>";
mailServiceimpl.sendAttachmentMail("1637378732@qq.com", "hello", html, "/Users/annie/Documents/ViewSonic VX2478-4K-525.icc");
return "OK";
}
/**
* 发送带图片的邮件
*/
@RequestMapping("/sendInlineResourceMail")
public Object sendInlineResourceMail() {
String rscPath = "/Users/annie/Documents/37401462.jpg";
String rscId = "001";
// 这里的 cid 必须要加
String content = "<img src='cid:" + rscId + "'>";
mailServiceimpl.sendInlineResourceMail("1637378732@qq.com", "hello", content, rscPath, rscId);
return "OK";
}
}
扩展内容
github
个人博客
网友评论