package com.jiayue.ssi.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
* 邮件发送工具
*
* @author xsl
* @since 2023/03/14
*/
@Component
public class SendMailUtil {
    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    String fromMailAddress;

    /**
     * 邮件发送
     * @param toMailAddress 目标地址
     * @param subject       邮件主题
     * @param mailContent   邮件内容
     * @throws Exception
     */
    public void executeSendMail(String[] toMailAddress,String subject,String mailContent) throws Exception{
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        mimeMessageHelper.setFrom(fromMailAddress);
        mimeMessageHelper.setTo(toMailAddress);
        mimeMessageHelper.setSubject(subject);
//        mimeMessageHelper.setText("口令：" + mailRandom + ",有效期4分钟。", true);
        mimeMessageHelper.setText(mailContent, true);
        javaMailSender.send(mimeMessage);
    }
}
