package ${basePackage}.frame.utils;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件发送工具类
 * <p>
 * 注注注: 使用时确保包含以下依赖，否则发送功能无法实现
 * <dependency>
 * <groupId>javax.mail</groupId>
 * <artifactId>mail</artifactId>
 * <version>1.4.7</version>
 * </dependency>
 * <p>
 * 注注注: JavaMailSender发送器，可通过Springboot配置注入，无需手动实例化
 * JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
 * javaMailSender.setDefaultEncoding(UTF-8);
 * javaMailSender.setHost(smtp.example.com);
 * javaMailSender.setUsername(**@example.com);
 * javaMailSender.setPassword(******);
 * javaMailSender.setPort(25);
 * <p>
 * 发送简单邮件
 * MailUtil.sendSimpleMail(javaMailSender, username, "**@example.com", "标题", "内容");
 * <p>
 * 发送HTML邮件
 * String html = "<html><body><h2>HTML邮件内容</h2><br><marquee>飞机飞走了.。。o O 0 ○~~~~~~~</marquee></body></html>";
 * MailUtil.sendHtmlMail(javaMailSender, username, "**@example.com", "给你的", html);
 * <p>
 * 发送附件邮件示例
 * MailUtil.sendAttachmentsMail(javaMailSender, username, "**@example.com", "给你的", html, "E:\\1.jpg");
 * <p>
 * 发送HTML带<img>示例
 * String resId = "id_001";
 * String html2 = "<html><body><h2>html邮件内容</h2><br><img src=\'cid:" + resId + "\'></img></body></html>";
 * MailUtil.sendInlineResourceMail(javaMailSender, username, "**@example.com", "标题", html2, "E:\\1.jpg", resId);
 */
public class MailUtil {

    /**
     * 发送简单邮件
     *
     * @param sender  发送器
     * @param from    发送人
     * @param to      接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public static void sendSimpleMail(JavaMailSender sender, String from, String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);
        sender.send(message);
    }

    /**
     * 发送HTML邮件
     *
     * @param sender  发送器
     * @param from    发送人
     * @param to      接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public static void sendHtmlMail(JavaMailSender sender, String from, String to, String subject, String content) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(content, true);
            sender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送附件邮件
     *
     * @param sender   发送器
     * @param from     发送人
     * @param to       接收人
     * @param subject  邮件主题
     * @param content  邮件内容
     * @param filePath 文件路径
     */
    public static void sendAttachmentsMail(JavaMailSender sender, String from, String to, String subject, String content, String filePath) {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            //此处可以添加多个附件
            helper.addAttachment(fileName, file);
            sender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送图片邮件
     *
     * @param sender  发送器
     * @param from    发送人
     * @param to      接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param rscPath 图片路径
     * @param rscId   资源ID
     */
    public static void sendInlineResourceMail(JavaMailSender sender, String from, String to, String subject, String content, String rscPath, String rscId) {
        MimeMessage message = sender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(content, true);

            //可以添加多个图片
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);

            sender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
