javaweb实现邮箱接收验证码
本篇介紹:web端通過java實現郵箱發送驗證碼
引入依賴
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-email</artifactId><version>1.5</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4</version></dependency>package com.ycg.tab.utils;
import com.ycg.tab.common.Global;
import lombok.extern.slf4j.Slf4j;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
/**
-
郵箱發送驗證碼服務
-
@author xjm
-
@version 2020-07-06
*/
@Slf4j
public class MailUtils {/**
-
郵箱發送驗證碼服務
-
@param phonePersonMail 收件人郵箱地址
-
@param templateParam 驗證碼
-
@return
*/
public static String smsSendEmailCode(String phonePersonMail, String templateParam) {
String companyEmail = Global.getConfig(“companyEmail”) != null ? Global.getConfig(“companyEmail”) : “yunlingkl”; // 企業郵箱
String companyEmailServer = Global.getConfig(“companyEmailServer”) != null ? Global.getConfig(“companyEmailServer”) : “smtp.126.com”; //郵箱SMTP服務器地址
String companyName = Global.getConfig(“companyName”) != null ? Global.getConfig(“companyName”) : “云領”; // 企業單位名稱
String emailAuthCode = Global.getConfig(“emailAuthCode”) != null ? Global.getConfig(“emailAuthCode”) : “NJUQUCXNKMYBBZCW”;// 郵箱授權碼
String result = “”;
// 獲取系統屬性
Properties properties = System.getProperties();
// 設置郵件服務器
properties.setProperty(“mail.smtp.host”, companyEmailServer);
properties.put(“mail.smtp.auth”, “true”);//阿里云服務器禁用25端口,所以服務器上改為465端口
properties.put(“mail.smtp.socketFactory.port”, “465”);
properties.put(“mail.smtp.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
properties.setProperty(“mail.smtp.socketFactory.fallback”, “false”);
properties.setProperty(“mail.smtp.socketFactory.port”, “465”);// 獲取默認session對象
message.setText("尊敬的用戶:\n" +"您好!感謝您使用【"+companyName+"】平臺找回密碼服務,您正在進行郵箱驗證,本次請求的驗證碼為: " + templateParam + "(為了保障您賬號的安全性,請在5分鐘內完成驗證)\n" +"\n" +"\n" +companyName+"賬號團隊\n" + DateUtils.dateFormat(new Date(), DateUtils.DATE_PATTERN));//設置發送內容// 發送消息Transport.send(message);result ="成功";
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(companyEmail, emailAuthCode); //發件人郵件用戶名、授權碼
}
});
try {
// 創建默認的 MimeMessage 對象
MimeMessage message = new MimeMessage(session);
// Set Subject: 頭部字段–標題
message.setSubject("【"+companyName+"】平臺找回密碼服務");
// Set From: 頭部字段–發件人郵箱
message.setFrom(new InternetAddress(companyEmail+"@126.com"));
// Set To: 頭部字段–收件人郵箱
message.addRecipient(Message.RecipientType.TO, new InternetAddress(phonePersonMail));} catch (MessagingException mex) {
mex.printStackTrace();
}
return result;
}
-
}
本篇文章暫且介紹到這里,具體實現根據業務來實現這里僅提供工具類
感謝支持!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
總結
以上是生活随笔為你收集整理的javaweb实现邮箱接收验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android电视kodi安装失败,电视
- 下一篇: 计算机派位志愿填报技巧,小升初电脑派位操