java发送邮件工具类

1. 普通java实现邮件发送

java发送邮件工具类_第1张图片

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


<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.0modelVersion>

    <groupId>com.itgroupId>
    <artifactId>emailTestartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>
    <dependencies>

        <dependency>
            <groupId>javax.mailgroupId>
            <artifactId>javax.mail-apiartifactId>
            <version>1.6.2version>
        dependency>
        <dependency>
            <groupId>com.sun.mailgroupId>
            <artifactId>javax.mailartifactId>
            <version>1.6.2version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.2version>
            <scope>testscope>
        dependency>
    dependencies>
project>

java发送邮件工具类_第2张图片

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 = "[email protected]";
        String title = "测试邮件";
        String text  = UUIDUtil.getUUID();

        boolean b = MailUtil.sendMail(email, title, text);
        System.out.println("发送成功");
    }
}

你可能感兴趣的:(java全栈学习,java,junit,开发语言)