javaweb实现邮箱接收验证码
本篇介紹:web端通過(guò)java實(shí)現(xiàn)郵箱發(fā)送驗(yàn)證碼
引入依賴(lài)
<!-- 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;
/**
-
郵箱發(fā)送驗(yàn)證碼服務(wù)
-
@author xjm
-
@version 2020-07-06
*/
@Slf4j
public class MailUtils {/**
-
郵箱發(fā)送驗(yàn)證碼服務(wù)
-
@param phonePersonMail 收件人郵箱地址
-
@param templateParam 驗(yàn)證碼
-
@return
*/
public static String smsSendEmailCode(String phonePersonMail, String templateParam) {
String companyEmail = Global.getConfig(“companyEmail”) != null ? Global.getConfig(“companyEmail”) : “yunlingkl”; // 企業(yè)郵箱
String companyEmailServer = Global.getConfig(“companyEmailServer”) != null ? Global.getConfig(“companyEmailServer”) : “smtp.126.com”; //郵箱SMTP服務(wù)器地址
String companyName = Global.getConfig(“companyName”) != null ? Global.getConfig(“companyName”) : “云領(lǐng)”; // 企業(yè)單位名稱(chēng)
String emailAuthCode = Global.getConfig(“emailAuthCode”) != null ? Global.getConfig(“emailAuthCode”) : “NJUQUCXNKMYBBZCW”;// 郵箱授權(quán)碼
String result = “”;
// 獲取系統(tǒng)屬性
Properties properties = System.getProperties();
// 設(shè)置郵件服務(wù)器
properties.setProperty(“mail.smtp.host”, companyEmailServer);
properties.put(“mail.smtp.auth”, “true”);//阿里云服務(wù)器禁用25端口,所以服務(wù)器上改為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”);// 獲取默認(rèn)session對(duì)象
message.setText("尊敬的用戶(hù):\n" +"您好!感謝您使用【"+companyName+"】平臺(tái)找回密碼服務(wù),您正在進(jìn)行郵箱驗(yàn)證,本次請(qǐng)求的驗(yàn)證碼為: " + templateParam + "(為了保障您賬號(hào)的安全性,請(qǐng)?jiān)?分鐘內(nèi)完成驗(yàn)證)\n" +"\n" +"\n" +companyName+"賬號(hào)團(tuán)隊(duì)\n" + DateUtils.dateFormat(new Date(), DateUtils.DATE_PATTERN));//設(shè)置發(fā)送內(nèi)容// 發(fā)送消息Transport.send(message);result ="成功";
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(companyEmail, emailAuthCode); //發(fā)件人郵件用戶(hù)名、授權(quán)碼
}
});
try {
// 創(chuàng)建默認(rèn)的 MimeMessage 對(duì)象
MimeMessage message = new MimeMessage(session);
// Set Subject: 頭部字段–標(biāo)題
message.setSubject("【"+companyName+"】平臺(tái)找回密碼服務(wù)");
// Set From: 頭部字段–發(fā)件人郵箱
message.setFrom(new InternetAddress(companyEmail+"@126.com"));
// Set To: 頭部字段–收件人郵箱
message.addRecipient(Message.RecipientType.TO, new InternetAddress(phonePersonMail));} catch (MessagingException mex) {
mex.printStackTrace();
}
return result;
}
-
}
本篇文章暫且介紹到這里,具體實(shí)現(xiàn)根據(jù)業(yè)務(wù)來(lái)實(shí)現(xiàn)這里僅提供工具類(lèi)
感謝支持!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
總結(jié)
以上是生活随笔為你收集整理的javaweb实现邮箱接收验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android电视kodi安装失败,电视
- 下一篇: 计算机派位志愿填报技巧,小升初电脑派位操