解决项目部署到阿里云发送不了QQ邮箱

首先,这是发送邮箱的类:

package com.zhifou.utils;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

public class SendQQMailUtil {

    private static Properties properties = new Properties();

    public static Random random = new Random();

    static {
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "14.18.245.164");// 主机名//这是qq的地址,这样写方便服务器解析
        properties.put("mail.smtp.port", "465");// 端口号
        properties.put("mail.smtp.socketFactory.port", "465");//设置ssl端口
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.localhost", "127.0.0.1");//主机映射
    }



    public static String sendMsg(String email) throws MessagingException {
        // 得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress("你的邮箱"));
        // 设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        // 设置邮件标题
        message.setSubject("随便");

        // 生成6位验证码
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<6;i++){
            sb.append(random.nextInt(10));
        }


        // 设置邮件内容
        message.setText("验证码为:" + sb.toString());
        // 得到邮差对象
        Transport transport = session.getTransport();
        // 连接自己的邮箱账户
        transport.connect("你的QQ邮箱", "密码");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        return sb.toString();
    }


}

其次,给 /etc/hosts文件添加映射

hostname先得到主机名

 

然后添加进去

调用方法:

String verifyCode = SendQQMailUtil.sendMsg(user.getEmail());//verifyCode:验证码

 

改了好久,不是500就是501,哭辽

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值