SpringBoot中快速实现邮箱发送代码解析
人气:0前言
在许多企业级项目中,需要用到邮件发送的功能,如:
- 注册用户时需要邮箱发送验证
- 用户生日时发送邮件通知祝贺
- 发送邮件给用户等
创建工程导入依赖
<!-- 邮箱发送依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- 小火龙代码简化 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency>
本工程依赖于SpringBoot,是基于SpringBoot再次对邮箱发送进行了一次封装。
创建Application.properties
# JavaMailSender 邮件发送的配置 spring.mail.host=smtp.qq.com spring.mail.username=lovelyWu98k@qq.com #授权码 spring.mail.password= spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.default-encoding=UTF-8
封装邮箱发送工具类
创建邮箱信息实体类,如下:
/** * 邮件信息实体 * * @author:伍梓涛 * @version:1.0.0 * @Modified By:SimpleWu * @CopyRright (c)2019-:YUM架构平台 */ @Data public class MailEntity { private String to; //收件人 private String subject;//标题 private String text;//正文 private boolean isHtml = false;//是否html邮件 private List<String> files;//附件地址 private Date sentDate;//发送时间 }
创建邮箱信息实体类工具,快捷创建邮件信息
/** * 邮箱发送信息统一处理封装 * * @author:伍梓涛 * @version:1.0.0 * @Modified By:SimpleWu * @CopyRright (c)2019-:YUM架构平台 */ @Slf4j public class MailEntityHelper { public static MimeMessageHelper craeteMessageHelper(MimeMessage mimeMessage, MailEntity mailBean) throws MessagingException { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); // mimeMessageHelper.setFrom(emailSender);//发件人 mimeMessageHelper.setTo(mailBean.getTo());//收件人 mimeMessageHelper.setSubject(mailBean.getSubject());//标题 mimeMessageHelper.setText(mailBean.getText(), mailBean.isHtml());//是否html mimeMessageHelper.setSentDate(mailBean.getSentDate() == null ? new Date() : mailBean.getSentDate());// 客户端邮箱发送时间 if (mailBean.getFiles() != null && mailBean.getFiles().size() > 0) { //附件添加 mailBean.getFiles().forEach(filename -> { String ext = filename.substring(filename.lastIndexOf(".") + 1, filename.length()); File diskfile = new File(filename); try { mimeMessageHelper.addAttachment(diskfile.getName(), diskfile); } catch (MessagingException e) { e.printStackTrace(); log.error("设置附件失败:{}", mailBean); } }); } return mimeMessageHelper; } }
创建邮箱发送工具类,进行邮箱发送:
/** * 邮箱发送工具类 * * @author:伍梓涛 * @version:1.0.0 * @Modified By:SimpleWu * @CopyRright (c)2019-:YUM架构平台 */ @Component @Slf4j public class MailHelper { @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String emailSender; private static MailHelper mailHelper; public MailHelper() { mailHelper = this; } /** * 构建静态获取实例 * * @return */ public static MailHelper buidler() { if (mailHelper == null) { return null; } return mailHelper; } /*public void sendSimpleMail(EmailContextPojo mailBean) { try { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //邮件发送人 simpleMailMessage.setFrom(emailSender); //邮件接收人 simpleMailMessage.setTo(mailBean.getTo()); //邮件主题 simpleMailMessage.setSubject(mailBean.getSubject()); //邮件内容 simpleMailMessage.setText(mailBean.getText()); javaMailSender.send(simpleMailMessage); } catch (Exception e) { e.printStackTrace(); log.error("邮件发送失败", e.getMessage()); } }*/ public void sendMail(MailEntity mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = MailEntityHelper.craeteMessageHelper(mimeMailMessage, mailBean); mimeMessageHelper.setFrom(emailSender);//发件人 javaMailSender.send(mimeMailMessage); } catch (Exception e) { log.error("邮件发送失败", e.getMessage()); } } public void sendMail(MailEntity... mailBeans) { //批量发送集合 List<MimeMessage> mimeMessages = new ArrayList<>(mailBeans.length); try { if (mailBeans != null || mailBeans.length > 0) { for (int i = 0; i < mailBeans.length; i++) { MimeMessage mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = MailEntityHelper.craeteMessageHelper(mimeMailMessage, mailBeans[i]); messageHelper.setFrom(emailSender);//发件人 mimeMessages.add(mimeMailMessage); } } MimeMessage[] resultMessage = new MimeMessage[mimeMessages.size()]; mimeMessages.toArray(resultMessage); javaMailSender.send((resultMessage)); } catch (Exception e) { log.error("邮件发送失败", e.getMessage()); } } }
在之前通过邮箱发送信息统一处理的工具类,对邮件信息实体进行封装后,这里就只需要传入对象参数即可。由于在这里使用的是SpringBoot已经封装好的,由于我们要把它当成工具类使用,每次需要注入并不方便。
所在在类中加了个静态对象,在Spring 容器实例化的时候给他初始化直接赋值给javaMailSender并且通过buidler()可以静态获取到实例对象,但是这样如果被重新创建对象后就会产生问题,所以最好在工具类中使用Spring 容器去获取 javaMailSender即可 。
发送邮件
1.发送普通邮件
MailEntity emailContextPojo = new MailEntity(); emailContextPojo.setSubject("Ycloud 邮箱标题"); emailContextPojo.setText("Ycloud 邮箱正文"); emailContextPojo.setTo(" 接收邮箱号 "); MailHelper.buidler().sendMail(emailContextPojo);
2.发送Html邮件
emailContextPojo.setHtml(true);
只需要将信息实体类设置成true即可。
3.发送附件
emailContextPojo.setFiles(Arrays.asList("D:\\Yum系统架构平台\\004.高系统部署\\004.PXC\\部署手册.docx"));
只需要增加附件路径即可。
注: 请勿直接在项目中使用邮箱发送
本文代码案例: https://github.com/450255266/open-doubi/tree/master/SpringBoot/email
您可能感兴趣的文章:
加载全部内容