pom.xml:
<dependencies>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- jasypt -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
application.properties: 这里使用jasypt加密,可参考文章:SpringBoot整合Jasypt实现对配置文件加密
jasypt.encryptor.password=944ec5b7-148f-4db6-ae0e-612a5106507b
#邮箱服务器地址
spring.mail.host=smtp.163.com
#用户名
spring.mail.username=wanyingjing@163.com
#密码
spring.mail.password=
#编码
spring.mail.default-encoding=UTF-8
MailApplication:
@SpringBootApplication
public class MailApplication {
public static void main(String[] args) {
SpringApplication.run(MailApplication.class, args);
}
}
1、发送简单邮件
MailApplicationTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private MailService mailService;
@Test
public void simpleMail() throws Exception {
String to = "wanyingjing@163.com";
String subject = "简单邮件";
String content = "测试发送简单邮件";
mailService.sendSimpleMail(to, subject, content);
}
}
MailService:
public interface MailService {
/**
* 发送简单邮件
*
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to, String subject, String content);
}
MailServiceImpl:
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);//发件人
message.setTo(to);//目标
message.setSubject(subject);//主题
message.setText(content);//内容
try {
mailSender.send(message);
logger.info("一份简单邮件已发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}
}
}
2、发送html邮件
MailApplicationTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private MailService mailService;
@Test
public void htmlMail() throws Exception {
String to = "wanyingjing@163.com";
String subject = "html邮件";
String content = "<html>\n" + "<body>\n" + " <h1>hello world ! 这是一封html邮件!</h1>\n" + "</body>\n" + "</html>";
mailService.sendHtmlMail(to, subject, content);
}
}
MailService:
public interface MailService {
/**
* 发送HTML邮件
*
* @param to
* @param subject
* @param content
*/
public void sendHtmlMail(String to, String subject, String content);
}
MailServiceImpl:
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
logger.info("一份html邮件已成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
}
3、发送携带附件的邮件
MailApplicationTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private MailService mailService;
@Test
public void attachmentsMail() throws Exception {
String to = "wanyingjing@163.com";
String subject = "带附件的邮件";
String content = "您有附件,请注意查收!";
String filePath = "C:\\Users\\EDZ\\Desktop\\application.log";//附件
mailService.sendAttachmentsMail(to, subject, content, filePath);
}
}
MailService:
public interface MailService {
/**
* 发送带有附件的邮件
*
* @param to
* @param subject
* @param content
* @param filePath
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath);
}
MailServiceImpl:
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
mailSender.send(message);
logger.info("一份带附件的邮件已发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
}
}
4、发送携带静态资源的邮件
MailApplicationTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private MailService mailService;
@Test
public void staticResourceMail() throws Exception {
String to = "wanyingjing@163.com";
String subject = "带图片的邮件";
String rscId = "neo006";
String content = "<html><body>详情请看:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "C:\\Users\\EDZ\\Desktop\\微信截图_20190531102741.png";
mailService.sendInlineResourceMail(to, subject, content, imgPath, rscId);
}
}
MailService:
public interface MailService {
/**
* 发送带静态资源的邮件
*
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
}
MailServiceImpl:
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper 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);
logger.info("一份带静态资源的邮件已发送。");
} catch (MessagingException e) {
logger.error("发送带静态资源的邮件时发生异常!", e);
}
}
}
5、发送html页面模板的邮件
MailApplicationTests:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private MailService mailService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void templateMail() {
Context context = new Context();
String emailContent = templateEngine.process("emailTemplate", context);
System.out.println(emailContent);
String to = "wanyingjing@163.com";
String sbuject = "模板邮件";
mailService.sendHtmlMail(to, sbuject, emailContent);
}
}
MailService:
public interface MailService {
/**
* 发送HTML邮件
*
* @param to
* @param subject
* @param content
*/
public void sendHtmlMail(String to, String subject, String content);
}
MailServiceImpl:
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
logger.info("一份html邮件已成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
}