package org.dromara.wflow.utils;

import lombok.extern.slf4j.Slf4j;
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 jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;

/**
 * @author : willian fu
 * @version : 1.0
 */
@Slf4j
@Component
public class EmailUtil {

//    @Value("${spring.mail.username}")
    private String from = "xxx";

    @Autowired
    private JavaMailSender mailSender;


    public void sendHtmlMail(String subject, String html, String ...to) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        //true表示需要创建一个multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(html, true);
        mailSender.send(message);
        log.info("发送邮件[{}  {}]给[{}]成功", subject, html, to);
    }
}
