SpringBoot使用qq邮箱发送邮件
Posted 小三毛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot使用qq邮箱发送邮件相关的知识,希望对你有一定的参考价值。
最近公司要做一个邮箱注册和重置密码的功能,因为之前就做过,但是不是Springboot项目,所以相对来说还是比较容易的,在这里记录一下。
一、引用Maven依赖 这里使用spring自带的邮件jar包
<!-- 邮件服务 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、yml配置
#邮箱配置
spring: mail: host: smtp.exmail.qq.com port: 465 protocol: smtp username: ******** //你的邮箱账号 password: ******** //你的登陆密码 default-encoding: UTF-8 properties: mail: smtp: auth: true starttls: enable: true required: true
三、创建EmailImpl类作为email组件
@Component public class EmailImpl { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Resource private RedisUtil redisUtil; @Value("${spring.mail.username}") public String from; @Value("${spring.mail.password}") public String password;// 登录密码 @Value("${spring.mail.protocol}") public String protocol;// 协议 @Value("${spring.mail.port}") public String port;// 端口 @Value("${spring.mail.host}") public String host;// 服务器地址 private String theme = "注册验证码"; private String reset = "重置验证码"; //初始化参数 public Session initProperties() { Properties properties = new Properties(); properties.setProperty("mail.transport.protocol", protocol); properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", port); // 使用smtp身份验证 properties.put("mail.smtp.auth", "true"); // 使用SSL,企业邮箱必需 start // 开启安全协议 MailSSLSocketFactory mailSSLSocketFactory = null; try { mailSSLSocketFactory = new MailSSLSocketFactory(); mailSSLSocketFactory.setTrustAllHosts(true); } catch (GeneralSecurityException e) { e.printStackTrace(); } properties.put("mail.smtp.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.socketFactory.fallback", "false"); properties.put("mail.smtp.socketFactory.port", port); Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); // 使用SSL,企业邮箱必需 end // TODO 显示debug信息 正式环境注释掉 session.setDebug(true); return session; } /** * 发送注册邮箱验证码 * @param sender * @return */ public Boolean sendhtmlEmail(String sender) { boolean lean = false; try { Session session = initProperties(); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from,"Tesra"));// 发件人,可以设置发件人的别名 // 收件人,多人接收 InternetAddress[] internetAddressTo = new InternetAddress().parse(sender); mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo); // 主题 mimeMessage.setSubject(theme); // 时间 mimeMessage.setSentDate(new Date()); // 容器类 附件 MimeMultipart mimeMultipart = new MimeMultipart(); // 可以包装文本,图片,附件 MimeBodyPart bodyPart = new MimeBodyPart(); String code = CodeUtils.getMathVal(); // 设置内容 getEmailHtml是邮箱内容模板 bodyPart.setContent(getEmailHtml(sender,code), "text/html; charset=UTF-8"); mimeMultipart.addBodyPart(bodyPart); // 添加图片&附件 // bodyPart = new MimeBodyPart(); // bodyPart.attachFile(fileSrc); // mimeMultipart.addBodyPart(bodyPart); mimeMessage.setContent(mimeMultipart); mimeMessage.saveChanges(); Transport.send(mimeMessage); redisUtil.set(sender,code, UserConstant.time); lean = true; } catch (MessagingException e) { e.printStackTrace(); lean = false; logger.error("发送注册邮件失败:"+sender+" ----"+e.getMessage()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); lean = false; logger.error("发送注册邮件失败:"+sender+" ----"+e.getMessage()); } return lean; } /** * 发送重置邮箱验证码 * @param sender * @return */ public Boolean sendHtmlEmailCZ(String sender) { Boolean lean = false; try { Session session = initProperties(); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from,"Tesra"));// 发件人,可以设置发件人的别名 // 收件人,多人接收 InternetAddress[] internetAddressTo = new InternetAddress().parse(sender); mimeMessage.setRecipients(Message.RecipientType.TO, internetAddressTo); // 主题 mimeMessage.setSubject(reset); // 时间 mimeMessage.setSentDate(new Date()); // 容器类 附件 MimeMultipart mimeMultipart = new MimeMultipart(); // 可以包装文本,图片,附件 MimeBodyPart bodyPart = new MimeBodyPart(); String code = CodeUtils.getMathVal(); // 设置内容 getEmailReset是发送邮箱的html模板 bodyPart.setContent(getEmailReset(sender,code), "text/html; charset=UTF-8"); mimeMultipart.addBodyPart(bodyPart); // 添加图片&附件 // bodyPart = new MimeBodyPart(); // bodyPart.attachFile(fileSrc); // mimeMultipart.addBodyPart(bodyPart); mimeMessage.setContent(mimeMultipart); mimeMessage.saveChanges(); Transport.send(mimeMessage); redisUtil.set(sender,code, UserConstant.time); lean = true; } catch (MessagingException e) { e.printStackTrace(); lean = false; logger.error("发送重置邮件失败:"+sender+" ----"+e.getMessage()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); lean = false; logger.error("发送重置邮件失败:"+sender+" ----"+e.getMessage()); } return lean; } }
四、邮箱html模板
/** * 注册邮箱模板 * @param to 邮箱号 * @param code 验证码 * @return */ public String getEmailHtml(String to,String code){ String[] tos = to.split("@"); String sub = tos[0].substring(0,2); String email = sub+"*******@"+tos[1]; String html = "<div style="padding: 0px 0px 0px 20px;box-sizing: border-box;color: #333333;font-family: "microsoft yahei";font-size: 14px">" + "<h3 style="font-weight: normal;font-size: 18px;">Test测试</h3>" + "<h4 style="color:#2672EC;font-size: 40px;margin-top: 24px;font-weight: normal;">账号注册验证码</h4>" + "<div style="margin-top: 40px;">您好,您正在使用<a href="javascript:;" target="_blank" style="color: #2672EC;text-decoration: none;">"+email+"</a>注册账号。</div>" + "<div style="margin-top: 30px;">您的注册验证码为:<em style="font-style: normal;font-weight: 600;">"+code+"</em></div>" + "<div style="margin-top: 35px;">谢谢!</div>" + "<div style="margin-top: 10px;">Test测试</div>" + "</div>"; return html; }
五、方法调用
@Autowired private EmailImpl emailImpl; //email邮箱号 emailImpl.sendHtmlEmail(email);
至此就完成了SpringBoot使用qq邮箱发送邮件。
以上是关于SpringBoot使用qq邮箱发送邮件的主要内容,如果未能解决你的问题,请参考以下文章