日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

javaweb实现邮箱接收验证码

發(fā)布時(shí)間:2023/12/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javaweb实现邮箱接收验证码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本篇介紹: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ì)象
      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));

      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 ="成功";

      } 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。