文章目录结尾前言
虽然现在登录都是手机验证码发送,但是工作当中也有些业务场景需要发送邮箱 。本文来介绍邮箱发送的使用和原理,做一些有用的总结 。
参考:
1.
2.
1.简单的一些总结:
动作
说明
备注
发送邮件
SMPT、MIME,是一种基于”推”的协议,通过SMPT协议将邮件发送至邮件服务器,MIME协议是对SMPT协议的一种补充,如发送图片附件等
SMTP 协议全称为 Simple Mail Transfer Protocol,译作简单邮件传输协议
接收邮件
POP、IMAP,是一种基于”拉”的协议,收件人通过POP协议从邮件服务器拉取邮件
个人总结:
使用qq邮箱进行发送需要开启POP3和SMTP协议 , 需要获得邮件服务器的授权码 。具体的配置过程可参考上面的链接 。
2.SpringBoot发送邮件:2.1添加依赖:
org.springframework.bootspring-boot-starter-mail
2.2账号配置;
#发送邮箱地址#这里以QQ邮箱为例#QQ邮箱服务器spring.mail.host=smtp.qq.com#QQ邮箱账户spring.mail.username=yourAccount@qq.com#QQ邮箱第三方授权码或者密码spring.mail.password=yourPassword#编码类型spring.mail.default-encoding=UTF-8
非qq邮箱注意端口和地址:
mail.address=xxx@xxx.comspring.mail.host= xxxspring.mail.username=xxxspring.mail.password=xxxspring.mail.port=587
重要说明:mail.address和spring.mail.username在qq邮箱里面是相同的html用按钮上传到邮箱 , 但是在公司的邮箱可以相同可以不相同,大部分是不相同的 , 大部分公司里的邮箱服务账号是没有后缀的 , 这个是采坑的地方,需要注意 。
2.3具体代码实现;
这里github上面有一个很不错的demo:
使用:
【23.SpringBoot发送邮箱总结】private final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);//这里是重点需要区分是qq邮箱和企业邮箱@Value("${spring.mail.username}")//使用@Value注入application.properties中指定的用户名private String from;@Autowired//用于发送文件服务private JavaMailSender mailSender;
发送服务的接口定义:
public interface MailService {/*** 发送普通文本邮件* @param to 收件人* @param subject 主题* @param content 内容*/void sendSimpleMail(String to, String subject, String content);/*** 发送HTML邮件* @param to 收件人* @param subject 主题* @param content 内容(可以包含等标签)*/void sendHtmlMail(String to, String subject, String content);/*** 发送带附件的邮件* @param to 收件人* @param subject 主题* @param content 内容* @param filePath 附件路径*/void sendAttachmentMail(String to, String subject, String content, String filePath);/*** 发送带图片的邮件* @param to 收件人* @param subject 主题* @param content 文本* @param rscPath 图片路径* @param rscId 图片ID,用于在标签中使用,从而显示图片*/void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);}
2.3.1发送的普通文本邮件:
public void sendSimpleMail(String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setTo(to);//收信人message.setSubject(subject);//主题message.setText(content);//内容message.setFrom(from);//发信人mailSender.send(message);}
2.3.2发送的html文件:
public void sendHtmlMail(String to, String subject, String content) {logger.info("发送HTML邮件开始:{},{},{}", to, subject, content);//使用MimeMessage,MIME协议MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper;//MimeMessageHelper帮助我们设置更丰富的内容try {helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);//true代表支持htmlmailSender.send(message);logger.info("发送HTML邮件成功");} catch (MessagingException e) {logger.error("发送HTML邮件失败:", e);}}
2.3.3发送的带附件文件:
public void sendAttachmentMail(String to, String subject, String content, String filePath) {logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath);MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper;try {helper = new MimeMessageHelper(message, true);//true代表支持多组件,如附件,图片等helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = file.getFilename();helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件mailSender.send(message);logger.info("发送带附件邮件成功");} catch (MessagingException e) {logger.error("发送带附件邮件失败", e);}}
2.3.4发送的带图片的邮件:
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {logger.info("发送带图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId);MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper;try {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);}}
2.3.5发送模板邮件:
模板邮件发送其实是基于html的模板发送,需要引入依赖:
org.springframework.bootspring-boot-starter-thymeleaf
2.在template文件夹下创建emailTemplate.html:
模板1:
邮件模板您好 , 感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!
激活账户
模板2:
xx亲爱的用户您好:
您本次的验证随机码为:
请在验证页面指定位置输入随机码,进行验证,有效期为15分钟 。
此邮件为xxxx育自动发送的电子邮件,无需回复!
模板单元测试:
/*** 指定模板发送邮件*/@Testpublic void testTemplateMail() {//向Thymeleaf模板传值 , 并解析成字符串Context context = new Context();context.setVariable("id", "001");String emailContent = templateEngine.process("emailTemplate", context);mailService.sendHtmlMail("receiver@email.com", "这是一个模板文件", emailContent);}
其他单元测试:
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;@RunWith(SpringRunner.class)@SpringBootTestpublic class ServiceTest {@Autowiredprivate MailService mailService;@Autowiredprivate TemplateEngine templateEngine;/*** 发送简单纯文本邮件*/@Testpublic void sendSimpleMail() {mailService.sendSimpleMail("receiver@email.com", "发送邮件测试", "大家好,这是我用springboot进行发送邮件测试");}/*** 发送HTML邮件*/@Testpublic void sendHtmlMail() {String content = "" + "大家好,这是springboot发送的HTML邮件" + "";mailService.sendHtmlMail("receiver@email.com", "发送邮件测试", content);}/*** 发送带附件的邮件*/@Testpublic void sendAttachmentMail() {String content = "" + "大家好,这是springboot发送的HTML邮件,有附件哦" + "";String filePath = "your file path";mailService.sendAttachmentMail("receiver@email.com", "发送邮件测试", content, filePath);}/*** 发送带图片的邮件*/@Testpublic void sendInlineResourceMail() {String rscPath = "your picture path";String rscId = "001";String content = "" + "大家好,这是springboot发送的HTML邮件,有图片哦" + ""+ "";mailService.sendInlineResourceMail("receiver@email.com", "发送邮件测试", content, rscPath, rscId);}}
结尾
上面就是整个的发送邮件的总结,其中一部分是参考的链接当中的内容,然后加入了自己的采坑总结,其中本文介绍的虽然是springboot发送邮件的流程,但是其实spring-boot-starter-mail依赖包下面也是相等于使用的javax.mail进行的发送邮件服务,其中的流程就是通过账号密码进行认证html用按钮上传到邮箱,然后认证完以后创建socket,进行发送,整个过程因为涉及到服务器的认证和socket的链接创建,所以有的时候比较耗时,实际开发当中到发送邮件那个方法采用异步@Async进行发送就可以 。
本文到此结束,希望对大家有所帮助!
猜你喜欢
- 绿萝黄叶又烂根?用1个小药片、2种水来养,叶子贼绿疯长爬满墙
- 为什么店家更愿意用微信支付而不是支付宝?
- 春天土壤硬邦邦,学会3个松土妙招,土壤软得像发糕
- 新生儿出生后有4个特征,意味着在娘胎里过得好,父母别担心
- 农行定期存单,没有设置密码没有到存款日期,现在急用有存款人身份证能取吗?
- 高智商宝宝一般有4个特征,你家孩子若中2个,家长就偷着乐吧
- 手游赚钱游戏排行榜前十名:只要思想不滑坡,方法总比困难多
- 女人对你动情的迹象,别忽视
- 建设自己地盘的第一步:阿里云网站域名备案步骤流程
