Java发送QQ邮箱

614 阅读2分钟

描述

其中使用了两种方式实现,一种是自己封装实现工具类,发送邮箱。 另一种是,依赖hutool工具包,快速实现Java发送qq邮箱。 我们可以根据邮箱发送功能,实现验证码邮箱登录等操作。

maven依赖

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.1</version>
</dependency>
<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.5.0</version>
</dependency>

获取16位SMTP命令

在邮箱安全设置中开启POP3和SMTP服务,获取到密钥

image.png

自定义实现

/**
 * @author 苦瓜不苦
 * @date 2021/9/11
 */
public class EmailTest {


    @Test
    void test() throws MessagingException {
        // 创建Properties 类用于记录邮箱的一些属性
        Properties props = new Properties();
        // 表示SMTP发送邮件,必须进行身份验证
        props.put("mail.smtp.auth", "true");
        // 此处填写SMTP服务器
        props.put("mail.smtp.host", "smtp.qq.com");
        // 端口号,QQ邮箱端口587
        props.put("mail.smtp.port", "587");
        // 此处填写,写信人的账号
        props.put("mail.username", "***@foxmail.com");
        // 此处填写16位STMP口令
        props.put("mail.password", "scrvdf******vcahh");

        // 构建授权信息,用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String username = props.getProperty("mail.username");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(username, password);
            }
        };
        // 使用环境属性和授权信息,创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        
        // 设置自定义发件人昵称
		String nick = javax.mail.internet.MimeUtility.encodeText("我的昵称");
        String from = props.getProperty("mail.user");
        // 设置发件人
        InternetAddress form = new InternetAddress(nick+"<"+form+">");
        
        // 第二种方式
        // InternetAddress form = new InternetAddress(from,"我的昵称");
        
        message.setFrom(form);

        // 设置收件人的邮箱
        InternetAddress to = new InternetAddress("***@163.com");
        message.setRecipient(MimeMessage.RecipientType.TO, to);

        // 设置邮件标题
        message.setSubject("验证码");

        // 设置邮件的内容体
        message.setContent("68789", "text/html;charset=UTF-8");

        // 最后当然就是发送邮件啦
        Transport.send(message);
    }

}

hutool实现

在resource目录下新建mail.setting文件

# 邮件服务器的SMTP地址
host = smtp.qq.com
# 邮件服务器的SMTP端口
port = 465
# 发件人(必须正确,否则发送失败)
from = 苦瓜不苦<***@foxmail.com>
# 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
user = ***@foxmail.com
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码)
pass = scrvdf******vcahh
# 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
starttlsEnable = true

# 使用SSL安全连接
sslEnable = true
# 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
socketFactoryClass = javax.net.ssl.SSLSocketFactory
# 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true
socketFactoryFallback = true
# 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口456
socketFactoryPort = 465

# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0

测试代码

public class MailTest {

    @Test
    void test() {
        String text = MailUtil.sendText("***@163.com", "验证码", "123456");
        System.out.println("text = " + text);
    }
    
}