1.邮箱支持引入到pom

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
      <dependency><groupId>cn.hutool</groupId><artifactId>hutool-core</artifactId><version>5.4.0</version></dependency>

2.验证码生成,随机数

 String verifCode = RandomUtil.randomNumbers(6);

3.邮箱发送验证码工具类

package com.mp.common.core.utils;import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import java.util.Properties;/*** @author Ling* @description: TODO 发送邮件工具类* @date 2020/3/19 15:11*/
public class EmailSendUtils {/*** gmail邮箱*/static String username = "邮箱";/*** 密码*/static String password = "密码";/*** 反馈邮件邮箱*/static String contactUsEmail = "contact@kiri-innov.com";/** gmail邮箱SSL方式*/private static void gmailssl(Properties props) {final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";props.put("mail.debug", "true");props.put("mail.smtp.host", "smtp.gmail.com");props.put("mail.smtp.ssl.enable", "true");props.put("mail.smtp.socketFactory.class", SSL_FACTORY);props.put("mail.smtp.port", "465");props.put("mail.smtp.socketFactory.port", "465");props.put("mail.smtp.auth", "true");}//gmail邮箱的TLS方式private static void gmailtls(Properties props) {props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.host", "smtp.gmail.com");props.put("mail.smtp.port", "587");}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderEnglish(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("pet App");String tips = "respected user: \n" +"Hello and welcome xiaoming, thank you for your registration.We are happy to start to get to know you better, Your login \n" +"email is " + email + ". Please fill in the following 6-digit verification code:\n"+ content + "\n" +"The verification code is valid within 30 minutes, please do not reply to this email, thank you!";msg.setText(tips);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderChina(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("pet App");String tips = "尊敬的用户: \n" +"你好,欢迎使用, 我们很高兴开始更好地了解您,您的登录 \n" +"邮箱为 " + email + ". 请回填如下6位验证码:\n"+ content + "\n" +"验证码在30分钟内有效,请勿回复此电子邮件,谢谢!";msg.setText(tips);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderSpain(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("pet App");String tips = "usuario respetado: \n" +"Hola y bienvenido,  gracias por tu registro. Estamos felices de empezar a conocerte mejor\n" +"Su correo electrónico de inicio de sesión es " + email + ". Complete el siguiente código de verificación de 6 dígitos:\n"+ content + "\n" +"El código de verificación es válido en 30 minutos, no responda a este correo electrónico, ¡gracias! ";msg.setText(tips);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderFrench(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("pet App");String tips = "utilisateur respecté: \n" +"Bonjour et bienvenue, merci pour votre inscription. Nous sommes heureux de commencer à mieux vous connaître" +"Votre email de connexion est  " + email + ". Veuillez remplir le code de vérification à 6 chiffres suivant:\n"+ content + "\n" +"Le code de vérification est valable dans les 30 minutes, merci de ne pas répondre à cet e-mail, merci ! ";msg.setText(tips);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderTurkey(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("pet App");String tips = "saygıdeğer kullanıcı: \n" +"Merhaba ve hoşgeldiniz, kaydınız için teşekkür ederiz. Sizi daha yakından tanımaya başladığımız için mutluyuz" +" Giriş e-postanız  " + email + "'dur. Lütfen aşağıdaki 6 haneli doğrulama kodunu doldurun:\n"+ content + "\n" +"Doğrulama kodu 30 dakika içinde geçerlidir, lütfen bu e-postayı yanıtlamayın, teşekkür ederiz!";msg.setText(tips);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送注册邮件** @param email 邮箱*/public static void gMailRegisterSender(String email, String content) {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("Phiz App");String tips = "Thank you for using Phiz App. Please click below link to complete your request.\n\n\n" +"Thank you for using Phiz App. Please click below link to complete your request. this link expires in 15 minutes.\n\n\n";msg.setText(tips + content);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** 通过gmail邮箱发送邮件** @param email 邮箱*/public static void gmailSenderHtml(String email, String content) throws MessagingException {// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象Multipart mainPart = new MimeMultipart();// 创建一个包含HTML内容的MimeBodyPartBodyPart html = new MimeBodyPart();// 设置HTML内容html.setContent(content, "text/html; charset=utf-8");mainPart.addBodyPart(html);// 将MiniMultipart对象设置为邮件内容// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));msg.setSubject("Phiz App");
//            msg.setText(content);msg.setContent(mainPart);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}/*** gmail发送用户反馈邮件** @param feedBackEmail 用户名* @param type          类型* @param time          时间* @param content       内容*/public static void gmailSendConstUs(String name, String feedBackEmail, String type, String time, String content) {
//        反馈用户:XXX
//        反馈类型:XXX
//        反馈时间:XXX
//        反馈内容:XXX// Get a Properties objectProperties props = new Properties();//选择ssl方式gmailssl(props);Session session = Session.getDefaultInstance(props,new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});// -- Create a new message --Message msg = new MimeMessage(session);// -- Set the FROM and TO fields --try {msg.setFrom(new InternetAddress(username));msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(contactUsEmail));msg.setSubject("Contact us");name = "反馈用户:" + name + "\n\n";feedBackEmail = "反馈用户邮箱:" + feedBackEmail + "\n\n";type = "反馈类型:" + type + "\n\n";time = "反馈时间:" + time + "\n\n";msg.setText(name + feedBackEmail + type + time + content);msg.setSentDate(new Date());Transport.send(msg);} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}
}

邮箱发送验证码工具类相关推荐

  1. Spring Boot 整合163或者qq邮箱发送验证码

    前记 最近做的项目中注册功能要用到验证码,本来想用阿里云的短信服务,但是有点小贵,就另辟捷径,选择了用邮箱来发送验证码.记录一下整合过程,也希望能帮助一些有需要的人. 正文 1.Spring Boot ...

  2. 实战总结:邮箱发送验证码

    java邮箱发送验证码 目前项目中需要同时支持短信和邮箱验证,短信用的是腾讯云就不多说了,在此分享一下邮箱验证码发送. 首先,作为发送邮箱,需要开启POP3/SMTP/IMAP,登录邮箱–设置–账户– ...

  3. SpringBoot通过qq邮箱发送验证码

    SpringBoot通过qq邮箱发送验证码 1.开启qq授权码 2.编写配置文件 spring:#邮箱验证mail:##163 smtp.163.com(反垃圾系统发送不了了)##qq smtp.qq ...

  4. SpringBoot实现邮箱发送验证码(QQ,163等同理)

    SpringBoot实现邮箱发送验证码 准备工作(依赖和yml文件的配置) 邮件的发送代码 补充 准备工作(依赖和yml文件的配置) 我们要远程操纵邮箱就需要获得每个邮箱的密钥: 1.先进入你的qq邮 ...

  5. aliyun短信服务包含随机生成四位数字验证码工具类

    .1.pom文件 <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sd ...

  6. thinkphp5 邮箱发送验证码

    想通过邮箱发送验证码 需要第三方类库 PHPMailer 上GitHub下载最新版第三方类库 https://github.com/PHPMailer/PHPMailer/ 下载后解压打开,在src文 ...

  7. java生成验证码工具类_Java生成图形验证码工具类

    生成验证码效果 validatecode.java 验证码生成类 package cn.dsna.util.images; import java.awt.color; import java.awt ...

  8. java邮箱发送验证码_java 邮箱发送验证码

    packagecom.*******.util.email;importorg.apache.commons.mail.EmailException;importorg.apache.commons. ...

  9. golang邮箱发送验证码

    常用邮箱: QQ 邮箱 POP3 服务器地址:qq.com(端口:995) SMTP 服务器地址:smtp.qq.com(端口:465/587) 163 邮箱: POP3 服务器地址:pop.163. ...

最新文章

  1. 移动端前端开发注意点(未完待续)
  2. bq4050读固件_stm32f767 实现模拟SMBUS驱动bq4050
  3. 蓝牙模块怎么指定查询另一个蓝牙模块_蓝牙模块以IPEX端口外接天线、PCB板载天线最为常见...
  4. vue 实例数据绑定 指令 事件
  5. 分布式系统的工程化开发方法
  6. 软件测试 学习之路 linux基础命令 (二)
  7. app 模拟器抓包 burpsuite_来看黑客是如何使用Proxifier+burpsuite代理https协议数据包...
  8. DOS批处理全面教程
  9. 手机写代码 termux
  10. 前端使用goeasy
  11. excel科学计数法还原成字符串方法
  12. 搜pos不为了收单而收单,互联网是个神奇的动物
  13. 行人属性数据集pa100k_澎思科技荣登「2019 AI 最佳掘金案例年度榜单」,视频结构化技术助力AI产业化落地...
  14. 《早秋客舍》赏析-[唐]杜牧古诗
  15. 【企业高管】CIO vs CEO:主要区别是什么?
  16. 不懂代码如何制作公司网站—壹间网络
  17. a链接实现alt提示
  18. vue elementui 表格搜索筛选栏组件封装
  19. vite简介,使用vite创建项目的优势
  20. IP通信中音频编解码技术与抗丢包技术概要

热门文章

  1. NRF52832学习笔记(19)——TWI(I2C)接口使用
  2. 日语计算机专业学生自我介绍,计算机系毕业生的自我介绍
  3. android如何设置图标,Android APP如何设置显示图标
  4. 迅驰时尚盛典,果然不错!
  5. Runtime objc4-779.1 为什么不能向一个已存在的类添加成员变量?有什么办法达到相同的效果(2)?
  6. 管理信息系统期末报告:大象基金信息管理系统分析与设计报告
  7. Js框架的JQuery基本用法
  8. 增值税发票生成EXCEL——调用百度开发接口
  9. 2020年A证(安全员)多少钱及A证(安全员)考试申请表
  10. 商品价格变化监控记录方法