Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类

准备工作

发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务

如何开启 POP3/SMTP 服务:https://blog.csdn.net/weixin_44953227/article/details/112562613


依赖

mail.jar:https://repo1.maven.org/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar

activation.jar: https://repo1.maven.org/maven2/javax/activation/activation/1.1.1/activation-1.1.1.jar

Maven

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

封装邮件发送工具类

使用

// User user = new User("收件人用户名", "收件人密码", "收件人邮箱");
User user = new User(username, password, email);
// 用户注册成功, 使用线程来发送邮件, 防止阻塞
SendMail sendMail = new SendMail(user);
sendMail.start();

工具类

package com.pro.utils;

import com.pro.pojo.User;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

// 网站三秒原则
public class SendMail extends Thread {
    // 发送邮件的邮箱
    private String from = "33344466@qq.com";
    // 邮箱用户名
    private String username = "33344466@qq.com";
    // 授权码
    private String password = "absasyncawait";
    // 发送邮件的服务器地址
    private String host = "smtp.qq.com";

    private User user;

    public SendMail(User user) {
        this.user = user;
    }

    @Override
    public void run() {
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.host", host); // 设置qq邮件服务器
            prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
            prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码

            // 关于qq邮箱, 还要设置SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.ssl.socketFactory", sf);

            // 使用 JavaMail 发送邮件的5个步骤

            // 1. 定义整个应用程序所需要的环境信息的 Session 对象
            // 这一步是qq邮箱才有, 其他邮箱不用
            Session session = Session.getDefaultInstance(prop, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    // 发件人邮箱 用户名和授权码
                    return new PasswordAuthentication(username, password);
                }
            });

            // 开启 Session debugger 模式, 可以看到邮件发送的运行状态
            session.setDebug(true);

            // 2. 通过 Session得到 transport 对象
            Transport transport = session.getTransport();

            // 3. 使用邮箱用户名和授权码连上邮件服务器 (登陆)
            transport.connect(host, username, password);

            // 4. 创建邮件: 写邮件
            MimeMessage message = new MimeMessage(session);


            // ======== 写邮件 ========
            // 设置邮件的发件人
            message.setFrom(new InternetAddress(from));
            // 设置邮件的收件人
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail()));

            // 邮件标题
            message.setSubject("用户注册通知");
            // 邮件文本内容
            String content = "<div>" +
                    "<span>恭喜你注册成功,你的用户名是:" +
                    user.getEmail() +
                    ",你的密码是:" +
                    user.getPassword() +
                    ",请妥善保管。</span>" +
                    "<a href='https://www.tmall.com/' target='_blank'>" +
                    "<img width='100%' src='https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2672619668,2196295127&fm=26&gp=0.jpg' />" +
                    "</a>" +
                    "<h4 style='color: red'>This email is sent from Java, 本邮件由Java程序发送, from to 33344466</h4>" +
                    "</div>";
            message.setContent(content, "text/html; charset=UTF-8");
            // ======== 写邮件 ========


            // 5. 发送邮件
            transport.sendMessage(message, message.getAllRecipients());

            // 关闭连接
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值