package ink.wgink.module.sms.builder.email;

import javax.mail.*;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: EmailSendImpl
 * @Description: 发送邮件
 * @Author: wanggeng
 * @Date: 2021/5/1 5:36 下午
 * @Version: 1.0
 */
public class EmailSend {

    private Message message;

    public EmailSend(Message message) {
        this.message = message;
    }

    /**
     * 文本邮件
     *
     * @param subject
     * @param text
     * @throws MessagingException
     */
    public void text(String subject, String text) throws MessagingException {
        message.setSubject(subject);
        // 设置邮件消息的主要内容
        message.setText(text);
        // 发送邮件
        Transport.send(message);
    }

    /**
     * html邮件
     *
     * @param subject
     * @param content
     * @throws MessagingException
     */
    public void html(String subject, String content) throws MessagingException {
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(content, "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        message.setSubject(subject);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        // 发送邮件
        Transport.send(message);
    }

}
