环境依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
配置:
spring.mail.host=xxx
spring.mail.username=xxx
spring.mail.password=xxx
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
发邮件test类:
若要做为工具类可以使用MailBean进行封装
package com.forezp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailSenderService {
@Autowired
private JavaMailSenderImpl mailSender;
/**
* 发送文本的邮件
*/
public void sendTxtMail() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// 设置收件人,寄件人
simpleMailMessage.setTo(new String[] {"xxx@sina.com"});
simpleMailMessage.setFrom("xxx@sina.com");
simpleMailMessage.setSubject("Spring Boot Mail邮件测试");
simpleMailMessage.setText("test文本。");
// 发送邮件
mailSender.send(simpleMailMessage);
}
/**
* 发送包含HTML文本的邮件
* @throws Exception
*/
public void sendHtmlMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
mimeMessageHelper.setTo("xxx@sina.com");
mimeMessageHelper.setFrom("xxx@sina.com");
mimeMessageHelper.setSubject("Spring Boot Mail邮件测试");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 邮件测试</h1></body>");
sb.append("</html>");
// 启用html
mimeMessageHelper.setText(sb.toString(), true);
// 发送邮件
mailSender.send(mimeMessage);
}
/**
* 发送包含图片的邮件
* @throws Exception
*/
public void sendAttachedImageMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setTo("xxx@sina.com");
mimeMessageHelper.setFrom("xxx@sina.com");
mimeMessageHelper.setSubject("Spring Boot Mail邮件测试");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 邮件测试</h1>");
// cid为固定写法,imageid指定一个唯一标识
sb.append("<img src=\"cid:imageid\"/></body>");
sb.append("</html>");
// 启用html
mimeMessageHelper.setText(sb.toString(), true);
// 设置imageId
FileSystemResource img = new FileSystemResource(new File("D:/xxx.jpg"));
mimeMessageHelper.addInline("imageid", img);
// 发送邮件
mailSender.send(mimeMessage);
}
/**
* 发送包含附件的邮件
* @throws Exception
*/
public void sendAttendedFileMail() throws Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
mimeMessageHelper.setTo("xxx@sina.com");
mimeMessageHelper.setFrom("xxx@sina.com");
mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试");
StringBuilder sb = new StringBuilder();
sb.append("<html><head></head>");
sb.append("<body><h1>spring 邮件测试</h1></body>");
sb.append("</html>");
// 使用html
mimeMessageHelper.setText(sb.toString(), true);
// 载入附件
FileSystemResource img = new FileSystemResource(new File("D:/xxx.jpg"));
mimeMessageHelper.addAttachment("image.jpg", img);
// 发送邮件
mailSender.send(mimeMessage);
}
}