Java发送邮件工具类

工具类代码如下:

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 发送邮件工具类
 * 基于JavaMail
 * @author MrXiao
 * @version 1.0.0
 */
@Configuration
@PropertySource("classpath:config/config.properties")
public class MailUtil {

    // 发件人电子邮箱
    private static String from;
    // 指定发送邮件的主机
    private static String host;
    private static String userName;
    private static String password;
    private static String subject;

    public static void sendMail(String mailAddress, String authCode) throws Exception {
        // 获取系统属性
        Properties properties = new Properties();
        properties.load(MailUtil.class.getClassLoader().getResourceAsStream("config/config.properties"));
        
        // 设置邮件服务器
        properties.put("mail.smtp.host", host);
        // 开启ssl加密
        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password); // 发件人邮件用户名、密码
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailAddress));
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(userName));
            // Set Subject: 头部头字段
            message.setSubject(subject);
            // 获取随机验证码

            // 设置消息体
            message.setText("随机数:" + authCode + "开发测试使用");
            // 发送消息
            Transport.send(message);
            System.out.println("邮件发送成功——————————————————————");
            System.out.println("收件人:" + mailAddress);
            System.out.println("验证码:" + authCode);
            System.out.println("邮件发送结束——————————————————————");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    @Value("${mail.from}")
    public void setFrom(String from) {
        MailUtil.from = from;
    }

    @Value("${mail.host}")
    public void setHost(String host) {
        MailUtil.host = host;
    }

    @Value("${mail.userName}")
    public void setUserName(String userName) {
        MailUtil.userName = userName;
    }

    @Value("${mail.password}")
    public void setPassword(String password) {
        MailUtil.password = password;
    }

    @Value("${mail.subject}")
    public void setSubject(String subject) {
        MailUtil.subject = subject;
    }

}

配置文件内容如下:

# Mail Config
# 发件人邮箱
email.from=
# 发件人邮件服务器 例如:smtp.126.com
email.host=smtp.126.com
# 发件人用户名 注意:QQ邮箱比较坑,只写QQ号就好,不要写全址
[email protected]
# 邮箱密码 注意:这个密码是授权码,不是邮箱密码
email.password=ShouQuanMa0217

你可能感兴趣的:(Java发送邮件工具类)