Spring Boot 邮件发送
pom文件依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-mail</artifactId> 4 </dependency>
配置文件
1 spring: 2 mail: 3 host: smtp.qq.com #协议 这边使用的QQ 4 username: xxxxxxx@qq.com #用户名 5 password: xxxxxx #邮箱授权码 6 default-encoding: UTF-8 #编码格式
创建Mail服务类
1 public interface MailService { 2 /** 3 * 发送普通文本邮件 4 * @param to 收件人 5 * @param subject 主题 6 * @param content 内容 7 */ 8 void sendSimpleMail(String to, String subject, String content); 9 /** 10 * 发送HTML邮件 11 * @param to 收件人 12 * @param subject 主题 13 * @param content 内容(可以包含<html>等标签) 14 */ 15 void sendHtmlMail(String to, String subject, String content); 16 /** 17 * 发送带附件的邮件 18 * @param to 收件人 19 * @param subject 主题 20 * @param content 内容 21 * @param filePath 附件路径 22 */ 23 void sendAttachmentMail(String to, String subject, String content, String filePath); 24 /** 25 * 发送带图片的邮件 26 * @param to 收件人 27 * @param subject 主题 28 * @param content 文本 29 * @param rscPath 图片路径 30 * @param rscId 图片ID,用于在<img>标签中使用,从而显示图片 31 */ 32 void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId); 33 34 }
1 @Service 2 public class MailServiceImpl implements MailService { 3 4 private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class); 5 6 @Value("${spring.mail.username}") 7 private String from; 8 9 @Autowired 10 private JavaMailSender mailSender; 11 12 @Override 13 public void sendSimpleMail(String to, String subject, String content) { 14 15 SimpleMailMessage message = new SimpleMailMessage(); 16 message.setTo(to);//收信人 17 message.setSubject(subject);//主题 18 message.setText(content);//内容 19 message.setFrom(from);//发信人 20 21 mailSender.send(message); 22 } 23 @Override 24 public void sendHtmlMail(String to, String subject, String content) { 25 26 logger.info("发送HTML邮件开始:{},{},{}", to, subject, content); 27 MimeMessage message = mailSender.createMimeMessage(); 28 29 MimeMessageHelper helper; 30 try { 31 helper = new MimeMessageHelper(message, true); 32 helper.setFrom(from); 33 helper.setTo(to); 34 helper.setSubject(subject); 35 helper.setText(content, true);//true代表支持html 36 mailSender.send(message); 37 logger.info("发送HTML邮件成功"); 38 } catch (MessagingException e) { 39 logger.error("发送HTML邮件失败:", e); 40 } 41 } 42 @Override 43 public void sendAttachmentMail(String to, String subject, String content, String filePath) { 44 45 logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath); 46 MimeMessage message = mailSender.createMimeMessage(); 47 48 MimeMessageHelper helper; 49 try { 50 helper = new MimeMessageHelper(message, true); 51 helper.setFrom(from); 52 helper.setTo(to); 53 helper.setSubject(subject); 54 helper.setText(content, true); 55 FileSystemResource file = new FileSystemResource(new File(filePath)); 56 String fileName = file.getFilename(); 57 helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件 58 mailSender.send(message); 59 logger.info("发送带附件邮件成功"); 60 } catch (MessagingException e) { 61 logger.error("发送带附件邮件失败", e); 62 } 63 64 65 } 66 @Override 67 public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) { 68 69 logger.info("发送带图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId); 70 MimeMessage message = mailSender.createMimeMessage(); 71 72 MimeMessageHelper helper; 73 try { 74 helper = new MimeMessageHelper(message, true); 75 helper.setFrom(from); 76 helper.setTo(to); 77 helper.setSubject(subject); 78 helper.setText(content, true); 79 FileSystemResource res = new FileSystemResource(new File(rscPath)); 80 helper.addInline(rscId, res);//重复使用添加多个图片 81 mailSender.send(message); 82 logger.info("发送带图片邮件成功"); 83 } catch (MessagingException e) { 84 logger.error("发送带图片邮件失败", e); 85 } 86 } 87 88 }
最后Test一波
1 @RunWith(SpringRunner.class) 2 @SpringBootTest(classes = MainApplication.class) 3 public class MailTest { 4 @Autowired 5 MailService mailService; 6 7 @Test 8 public void sendSimpleMail(){ 9 mailService.sendSimpleMail("xxxxx@qq.com","标题","内容"); 10 } 11 12 }