第一步:导入pom依赖

<!-- 发送Email -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

配置 application.yml

email:
  fromEmailSMTPHost: smtp.163.com  # 邮箱服务器地址
  fromEmailAccount: XXXXX@163.com # 邮箱账户
  fromEmailPassword: ZXIFXXXXXXHPXYY # 邮箱 授权码

第三步:添加工具类 EMailUtil.java


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @auth  zhanghy
 * @date  2021/8/31 11:04
 * @desc  邮件发送工具类
 */
@Component
public class EMailUtil {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static String fromEmailSMTPHost;
    private static String fromEmailAccount;
    private static String fromEmailPassword;


    public EMailUtil(@Value("${email.fromEmailSMTPHost}") String host,
                     @Value("${email.fromEmailAccount}") String account,
                     @Value("${email.fromEmailPassword}") String password){
        fromEmailSMTPHost = host;
        fromEmailAccount = account;
        fromEmailPassword = password;
    }

    /**
     * @auth   zhanghy
     * @date   2021/8/31 11:09
     * @desc   创建发送邮件Session
     * @return javax.mail.Session
     */
    public static Session createSession(String fromEmailSMTPHost){
        // 1. 创建参数配置, 用于连接邮件服务器的参数配置
        Properties props = new Properties();                    // 参数配置
        props.setProperty("mail.transport.protocol", "smtp");   // 使用的协议(JavaMail规范要求)
        props.setProperty("mail.smtp.host", fromEmailSMTPHost);   // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");            // 需要请求认证
        // 2. 根据配置创建会话对象, 用于和邮件服务器交互
        Session session = Session.getInstance(props);
        session.setDebug(false);                                 // 设置为debug模式, 可以查看详细的发送 log
        return session;
    }

    /**
     * @auth   zhanghy
     * @date   2021/8/31 14:06
     * @desc    发送Email
     * @pram   [fromAuth, subject, content, toEmail]
     * @return java.lang.Boolean
     */
    public static Boolean send(String fromAuth, String subject, String content, String... toEmail) throws Exception {
        return sendEmail(fromEmailAccount, fromEmailPassword, fromEmailSMTPHost, fromAuth, subject, content, toEmail);
    }

    /**
     * @auth   zhanghy
     * @date   2021/8/31 11:56
     * @desc   邮件发送
     * @pram   [fromEmailAccount, fromEmailPassword, fromEmailSMTPHost, fromAuth, subject, content, toEmail]
     * @return java.lang.Boolean
     */
    public static Boolean send(String fromEmailAccount,String fromEmailPassword,String fromEmailSMTPHost,
            String fromAuth, String subject, String content, String... toEmail) throws Exception {
        return sendEmail(fromEmailAccount, fromEmailPassword, fromEmailSMTPHost, fromAuth, subject, content, toEmail);
    }

    /**
     * @auth   zhanghy
     * @date   2021/8/31 14:15
     * @desc   发送邮件
     * @pram   [fromEmailAccount 邮件账户, fromEmailPassword 邮件认证码, fromEmailSMTPHost 邮箱服务器, fromAuth 邮箱发送者, subject 主题, content 发送内容, toEmail 邮件接受者]
     * @return java.lang.Boolean
     */
    private static Boolean sendEmail(String fromEmailAccount, String fromEmailPassword, String fromEmailSMTPHost, String fromAuth, String subject, String content, String[] toEmail) throws Exception {
        Session session = createSession(fromEmailSMTPHost);
        MimeMessage message = createMimeMessage(fromEmailAccount,session, fromAuth, subject, content, toEmail);
        Transport transport = session.getTransport();
        transport.connect(fromEmailAccount, fromEmailPassword);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        return true;
    }

    /**
     * @auth   zhanghy
     * @date   2021/8/31 11:10
     * @desc   创建 Message
     * @pram   [javax.mail.Session, java.lang.String, java.lang.String, java.lang.String, java.lang.String...]
     * @return javax.mail.internet.MimeMessage
     */
    private static MimeMessage createMimeMessage(String fromEmailAccount,Session session,String fromAuth, String subject, String content, String ...toEmail) throws Exception {
        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);
        // 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
        message.setFrom(new InternetAddress(fromEmailAccount, fromAuth, "UTF-8"));
        // 3. To: 收件人(可以增加多个收件人、抄送、密送)
        for(String email: toEmail){
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email, "峰迪爱撕", "UTF-8"));
        }
        // 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
        message.setSubject(subject, "UTF-8");
        // 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
        message.setContent(content, "text/html;charset=UTF-8");
        // 6. 设置发件时间
        message.setSentDate(new Date());
        // 7. 保存设置
        message.saveChanges();
        return message;
    }



}

 

最后修改于 2021-08-31 14:24:04
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇