Java implements QQ mailbox verification code sending

Open the POP/SMTP service in the QQ mailbox
insert image description here
and import the required jar package

<!--QQ邮箱验证码所需jar包-->
<dependency>
	<groupId>javax.activation</groupId>
	<artifactId>activation</artifactId>
	<version>1.1.1</version>
</dependency>

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-email</artifactId>
	<version>1.4</version>
</dependency>

Test, the verification code can be randomly generated by the tool class, so I won't go into details here.

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendEmail {
    
    

    /**
     * 发送邮件代码
     * @param targetEmail 目标邮箱
     * @param authCode 验证码
     */
    public static void sendEmailCode(String targetEmail, String authCode) {
    
    
        try {
    
    
            SimpleEmail mail = new SimpleEmail();
            // 发送邮件的服务器
            mail.setHostName("smtp.qq.com");
            // 刚刚记录的授权码,是开启SMTP的密码
            mail.setAuthentication("[email protected]", "授权码");
            // 发送邮件的邮箱和发件人
            mail.setFrom("[email protected]", "桃花er");
            // 使用安全链接
            mail.setSSLOnConnect(true);
            // 接收的邮箱
            mail.addTo(targetEmail);
            // 邮件的主题
            mail.setSubject("注册验证码");
            // 邮件的内容
            mail.setMsg("验证码为:" + authCode);
            // 发送
            mail.send();
        } catch (EmailException e) {
    
    
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
    
    
        sendEmailCode("[email protected]", "123456");
    }
}

Guess you like

Origin blog.csdn.net/weixin_45930241/article/details/123249349