发送纯文本邮件servicepackage com.galaxy.mailsender.service;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;import javax.mail.MessagingException;import java.util.Date;/** * 邮件业务类 * @author Galaxy */@Servicepublic class MailService {/*** 注入邮件工具类*/@Autowiredprivate JavaMailSenderImpl javaMailSender;@Value("${spring.mail.username}")private String sendMailer;/*** 检测邮件信息类* @param to* @param subject* @param text*/private void checkMail(String to,String subject,String text){if(StringUtils.isEmpty(to)){throw new RuntimeException("邮件收信人不能为空");}if(StringUtils.isEmpty(subject)){throw new RuntimeException("邮件主题不能为空");}if(StringUtils.isEmpty(text)){throw new RuntimeException("邮件内容不能为空");}}/*** 发送纯文本邮件* @param to* @param subject* @param text*/public void sendTextMailMessage(String to,String subject,String text){try {//true 代表支持复杂的类型MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);//邮件发信人mimeMessageHelper.setFrom(sendMailer);//邮件收信人1或多个mimeMessageHelper.setTo(to.split(","));//邮件主题mimeMessageHelper.setSubject(subject);//邮件内容mimeMessageHelper.setText(text);//邮件发送时间mimeMessageHelper.setSentDate(new Date());//发送邮件javaMailSender.send(mimeMessageHelper.getMimeMessage());System.out.println("发送邮件成功:"+sendMailer+"->"+to);} catch (MessagingException e) {e.printStackTrace();System.out.println("发送邮件失败:"+e.getMessage());}}}
Controllerpackage com.galaxy.mailsender.controller;import com.galaxy.mailsender.service.MailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MailController {@Autowiredprivate MailService mailService;/*** 发送文本邮件* @param to* @param subject* @param text*/@RequestMapping("/sendTextMail")public void sendTextMail(String to,String subject,String text){mailService.sendTextMailMessage(to,subject,text);}}
测试
![SpringBoot-JavaMailSender接口实战](http://img.zhejianglong.com/231018/0H2226195-2.png)
文章插图
![SpringBoot-JavaMailSender接口实战](http://img.zhejianglong.com/231018/0H2224P8-3.png)
文章插图
结果
![SpringBoot-JavaMailSender接口实战](http://img.zhejianglong.com/231018/0H222H36-4.png)
文章插图
补充发送html邮件Spring Boot支持使用HTML发送邮件是通过MimeMessage来完成的 。
发送带附件的邮件MimeMessageHelper支持发送复杂邮件模板,支持文本、附件、HTML、图片等 。比如需要发送附件 , 则在上面的代码中通过调用helper的addAttachment(fileName, file)方法即可 。
例:发送HTML邮件
package com.example.emaildemo.controller;import com.example.emaildemo.service.MailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * 测试邮件发送 * @author galaxy */@RestControllerpublic class SendMailController {@Autowiredprivate MailService mailService;/*** 发送HTML邮件* @param to* @param subject* @param content*/@RequestMapping("/sendHtmlMailMessage")public void sendHtmlMailMessage(String to,String subject,String content){mailService.sendHtmlMailMessage(to,subject,content);}}
推荐阅读
- 巧用VBA实现:基于多个关键词模糊匹配Excel多行数据
- 详解ROMA Connect API 流控实现技术
- Object Detection 手把手教你使用LabVIEW OpenCV dnn实现物体识别含源码
- 实时营销引擎在vivo营销自动化中的实践 | 引擎篇04
- MatrixOne从入门到实战04——MatrixOne的连接和建表
- MatrixOne从入门到实践08——SSB性能测试
- 求笔仙的玩法真实的(亲身体验笔仙)
- 一 网络安全主动进攻之DNS基础和ettercap实现DNS流量劫持
- TCP和UDP的区别与联系以及网络字节序和主机字节序的转换函数实践
- 大数据技术之HBase原理与实战归纳分享-上