邮件通知是现代应用中常见的一种通信方式,特别是在需要及时反馈、告警或重要事件通知的场景下。Spring Boot提供了简单而强大的邮件发送功能,本文将通过pring Boot中使用JavaMailSender 接口实现邮件发送。
使用场景说明
注册验证:在用户注册、商户注册时,通过邮件发送验证码进行身份确认和账户激活。网站营销:向潜在用户发送营销信息或者广告邮件。数据监控、报警通知: 对业务类型数据及报警进行通知,达到一个监控目的。身份安全验证:在用户修改密码、解锁账户等关键操作时,通过邮件发送安全验证码来增强账户安全性。普通文本邮件、附件返送邮件:满足不同需求邮件发送需求。
实现说明
1、邮件依赖包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、配置参数(yml文件配置信息)、以下内容以163邮件为例。
spring:mail:host: smtp.163.com#邮箱名字 username: 东北大亨@163.com# 邮箱密码或者密钥、注意此处为授权邮箱授权码password: ZXESDSFGFGGGGSSport: 465protocol: smtpdefault-encoding: utf-8properties:mail:smtp:auth: truestarttls:enable: truerequired: truessl:enable: truesocketFactory:port: 465class: javax.net.ssl.SSLSocketFactory
3、创建邮件发送服务类
3.1 接口类
/*** @author :jack.zhao* @description :发送邮件接口* @version: V1.0.0* @create :2019/5/16 10:47*/ public interface IsendMailService { /*** 发送普通邮件* @param to 收件地址* @param subject 标题* @param text 正文*/void sendSimpleEmail(String to,String subject,String text);/*** @param to 收件地址* @param subject 标题* @param text 正文* @param attach 附件*/void sendAttachmentMail(String to, String subject, String text, MultipartFile attach) throws MessagingException, IOException; }
3.2 邮件发送接口实现类
package com.toll.northeasttycoon.service.Impl;import com.toll.auth.service.IEmailService; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ByteArrayResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.IOException;/*** @author :jack.zhao* @description :发送邮件服务类* @version: V1.0.0* @create :2019/5/16 10:48*/ @Service public class EmailSendServiceImpl implements ISendEmailService {/*** 发送邮件邮箱 */@Value("${spring.mail.username}")private String account;@Resourceprivate JavaMailSender javaMailSender;
@Overridepublic void sendSimpleEmail(String to, String subject, String text) {SimpleMailMessage message = new SimpleMailMessage();message.setTo(to);message.setSubject(subject);message.setText(text);message.setFrom(account);javaMailSender.send(message);}/*** @param to 收件地址* @param subject 标题* @param text 正文* @param attach 附件*/@Overridepublic void sendAttachmentMail(String to, String subject, String text, MultipartFile attach) throws MessagingException, IOException {MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);mimeMessageHelper.setFrom(account);mimeMessageHelper.setTo(new String[]{to});mimeMessageHelper.setSubject(subject);mimeMessageHelper.setText(text);// 获取附件的文件名和后缀名String fileName = attach.getOriginalFilename();String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);// 读取附件内容byte[] bytes = attach.getBytes();// 新的资源对象来存储附件内容ByteArrayResource attachmentResource = new ByteArrayResource(bytes);// 添加附件,使用完整的文件名(包括后缀名) mimeMessageHelper.addAttachment(fileName, attachmentResource);javaMailSender.send(mimeMailMessage);} }
3.3 调用发送邮件
@Resourceprivate IEmailService iEmailService;String rcvEmail = "接收邮件邮箱";// 发送主题String subject = "邮件主题";// 发送内容String content= “发送内容”;iEmailService.sendEmail(rcvEmail ,subject,content);
注意:在实现中可能遇到几个问题
1、550 User has no permission
2、发生 com.sun.mail.smtp.SMTPSenderFailedException: 553 Mail from must equal authorized user
3、535 Error:authentication failed
就是在配置文件yml配置的password应该是授权密码,而不是邮箱密码。
最后说下邮箱授权操作:在开启服务协议的时候需要发送短信,发送短信后在弹出框中选择已发送就会提示授权码。“保留授权码”