java工具类-发送邮件工具类

1. 普通java实现邮件发送

在这里插入图片描述

1.1 创建maven项目,配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.it</groupId>
    <artifactId>emailTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
<!--    邮箱    mail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
<!--        测试工具 junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述

1.2 创建邮箱码值工具类

package com.it.util;

public class EntityCode {
    //发件人邮箱
    public static final String USER = "自己的邮箱号";
    //授权码
    public static final String PWD = "自己的邮箱号授权码";
    //发送给对应的app,如果是163邮箱:smtp.163.com
    public static final String HOST = "smtp.qq.com";
}

1.3 创建邮箱发送邮件的工具类

package com.it.util;

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

public class MailUtil {
    /**
     * 创建邮件消息
     * @return 创建的邮件消息
     */
    private static MimeMessage createMail(){
        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.auth",true);
            properties.put("mail.smtp.host", EntityCode.HOST);
            properties.put("mail.user",EntityCode.USER);
            properties.put("mail.password",EntityCode.PWD);
            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(EntityCode.USER, EntityCode.PWD);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            return message;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送一个简单的文本邮件
     * @param to 收件人邮箱
     * @param title 邮件标题
     * @param text  邮件内容
     * @return
     */
    public static boolean sendMail(String to,String title,String text){
        MimeMessage message = createMail();
        if (message==null){
            return false;
        }
        try {
            // 设置发件人
            InternetAddress form = new InternetAddress(EntityCode.USER);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
}

1.4 创建测试类

import com.it.util.MailUtil;
import com.it.util.UUIDUtil;
import org.junit.Test;

public class EmailTest {

    @Test
    public void Test1(){
        String email = "2428596932@qq.com";
        String title = "测试邮件";
        String text  = UUIDUtil.getUUID();

        boolean b = MailUtil.sendMail(email, title, text);
        System.out.println("发送成功");
    }
}
点击查看代码
posted @ 2022-11-24 16:13  故理  阅读(169)  评论(0编辑  收藏  举报