可以查看该博主的文章介绍的很详细,包含了发邮件的多种情况:
https://www.iteye.com/blog/412887952-qq-com-2305992
如果想发送的邮件包含压缩包可以参考这篇文章
https://www.cnblogs.com/qq931399960/p/11409044.html
记得先写这个
@Autowired
private JavaMailSenderImpl mailSender;
public void sendMailAttach() throws Exception {
Properties mailProperties = mailSender.getJavaMailProperties();
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailSender.getUsername());
helper.setTo(mailProperties.getProperty("receiver").split(","));
// 第二个参数为是否支持html,如果为true,则表示支持,此时如果写入<h2>nice</h2>,则nice会被加粗,默认为false
helper.setText("nice day2", true);
helper.setSubject("测试2");
// 或者使用new FiledataSource
FileSystemResource file = new FileSystemResource(new File("F:\\learn\\workplace\\webtest.zip"));
helper.addAttachment("webdemo.zip", file);
mailSender.send(message);
}