163邮箱发送验证码功能

package cn.newtouch.common.util;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;

/**

  • JavaMail发送邮件: java版本-与web无关
  • 前提是QQ邮箱里帐号设置要开启POP3/SMTP协议

*/
public class Mail_java {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
// 开启debug调试,以便在控制台查看
prop.setProperty(“mail.debug”, “true”);
// 设置邮件服务器主机名
/prop.setProperty(“mail.host”, “smtp.qq.com”);/
prop.setProperty(“mail.host”, “smtp.beijinglife.com”);
// 发送服务器需要身份验证
prop.setProperty(“mail.smtp.auth”, “true”);
// 发送邮件协议名称
prop.setProperty(“mail.transport.protocol”, “smtp”);
// 开启SSL加密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put(“mail.smtp.ssl.enable”, “true”);
prop.put(“mail.smtp.ssl.socketFactory”, sf);
// 创建session
Session session = Session.getInstance(prop);
// 通过session得到transport对象
Transport ts = session.getTransport();
// 连接邮件服务器:邮箱类型,帐号,POP3/SMTP协议授权码 163使用:smtp.163.com
/ts.connect(“smtp.qq.com”, “liluyao_z@qq.com”, “cvujdkiwhqsmcfja”);/
/ts.connect(“smtp.beijinglife.com”, “liluyao_z@qq.com”, “cvujdkiwhqsmcfja”);/
//参数邮箱官网地址,用户名,授权码
ts.connect(“115.159.234.192”, “liluyao@beijinglife.com.cn”, “LLYnvwang822”);
// 创建邮件
Message message = createSimpleMail(session);
// 发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}

/**
 * @Method: createSimpleMail
 * @Description: 创建一封只包含文本的邮件
 */
public static MimeMessage createSimpleMail(Session session) throws Exception {
    //  获取6为随机验证码
    String[] letters = new String[] {
            "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
            "A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
            "0","1","2","3","4","5","6","7","8","9"};
    String stringBuilder ="";
    for (int i = 0; i < 6; i++) {
        stringBuilder = stringBuilder + letters[(int)Math.floor(Math.random()*letters.length)];
    }

    // 创建邮件对象
    MimeMessage message = new MimeMessage(session);
    // 指明邮件的发件人
    /*message.setFrom(new InternetAddress("liluyao_z@qq.com"));*/
    message.setFrom(new InternetAddress("bi_auto@beijinglife.com.cn"));
    // 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
    /*message.setRecipient(Message.RecipientType.TO, new InternetAddress("liluyao_z@qq.com"));*/
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("bi_auto@beijinglife.com.cn"));
    // 邮件的标题
    message.setSubject("密码重置通知");
    // 邮件的文本内容
    message.setContent("您本次重置密码的验证码是"+stringBuilder+",验证码有效期为10分钟,转发给他人可能导致账号被盗,请勿泄露,谨防被骗", "text/html;charset=UTF-8");
    //设置十分钟后验证码过期
    // 返回创建好的邮件对象
    return message;
}

}

//将参数都改成自己需要的参数
//思路步骤:1、去所需要的邮箱官网设置里打开授权,会有一个授权码,拿到授权码放到指定的参数里
2、暂时测试邮件的发起人和收件人都是自己本人的邮箱地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用 thinkphp6 发送邮件验证码的示例: 1. 首先,需要在 `config/mail.php` 文件中进行邮箱配置: ```php return [ // 默认发送邮件设置 'default' => [ // 邮件服务器地址 'host' => 'smtp.163.com', // 邮件服务器端口 'port' => 465, // 发件人邮箱地址 'username' => 'your_email@example.com', // 邮箱授权码,非邮箱登录密码 'password' => 'your_email_password', // 邮箱加密方式,ssl 或 tls 'secure' => 'ssl', // 默认发件人 'from' => [ 'address' => 'your_email@example.com', 'name' => 'your_name', ], ], ]; ``` 2. 然后,创建一个 `MailService` 类,用于发送邮件: ```php <?php namespace app\service; use think\facade\Cache; use think\facade\Config; use think\facade\View; use think\facade\Lang; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class MailService { /** * 发送邮件验证码 * * @param string $email 邮箱地址 * @param string $type 验证码类型,例如 register、forget * @return boolean */ public function sendVerifyCode($email, $type) { // 生成验证码 $code = mt_rand(100000, 999999); // 邮件主题和内容 $subject = Lang::get('mail.' . $type . '_subject'); $body = View::fetch('mail/' . $type . '_body', ['code' => $code]); // 实例化 PHPMailer 对象 $mail = new PHPMailer(true); try { // 配置 SMTP 服务器 $mail->SMTPDebug = 0; // 调试:0 关闭,1 开启 $mail->isSMTP(); $mail->Host = Config::get('mail.default.host'); $mail->SMTPAuth = true; $mail->Username = Config::get('mail.default.username'); $mail->Password = Config::get('mail.default.password'); $mail->SMTPSecure = Config::get('mail.default.secure'); $mail->Port = Config::get('mail.default.port'); // 设置发件人、收件人、邮件主题、内容 $mail->setFrom(Config::get('mail.default.from.address'), Config::get('mail.default.from.name')); $mail->addAddress($email); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); // 将验证码存入缓存,有效期为 5 分钟 Cache::set('verify_code:' . $email, $code, 300); return true; } catch (Exception $e) { return false; } } } ``` 3. 最后,在控制器中调用 `MailService` 类的 `sendVerifyCode` 方法即可: ```php <?php namespace app\controller; use app\service\MailService; use think\facade\Request; class UserController { /** * 发送注册验证码 * * @return json */ public function sendRegisterVerifyCode() { $email = Request::post('email'); if (!validate_email($email)) { return json(['code' => -1, 'msg' => '邮箱地址不正确']); } $mailService = new MailService(); if ($mailService->sendVerifyCode($email, 'register')) { return json(['code' => 0, 'msg' => '验证码发送']); } else { return json(['code' => -1, 'msg' => '验证码发送失败']); } } } ``` 其中,`validate_email` 是一个自定义的函数,用于验证邮箱地址的合法性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值