package cn.reghao.autodop.dmaster.notification.service.notifier.email;

import cn.reghao.autodop.dmaster.notification.model.po.EmailAccount;
import cn.reghao.autodop.dmaster.notification.service.notifier.Notify;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

/**
 * 邮件通知
 *
 * @author reghao
 * @date 2021-02-25 19:23:16
 */
public class EmailNotify implements Notify<EmailMsg> {
    private String account;
    private String password;
    private InternetAddress from;
    private Session session;

    public EmailNotify(EmailAccount emailAccount) throws UnsupportedEncodingException {
        this.account = emailAccount.getUsername();
        this.password = emailAccount.getPassword();
        this.from = new InternetAddress(account, emailAccount.getPersonal(), "UTF-8");
        initSessioin(emailAccount.getSmtp());
    }

    private void initSessioin(String smtp) {
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", smtp);
        props.setProperty("mail.smtp.auth", "true");
        session = Session.getInstance(props);
        session.setDebug(true);
    }

    @Override
    public void send(String receiver, EmailMsg msg) throws Exception {
        MimeMessage message = message(emailDestinations(receiver), msg.getSubject(), msg.getContent());
        Transport transport = session.getTransport();
        transport.connect(account, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public void send(String receiver, String subject, String msg) throws Exception {
        MimeMessage message = message(emailDestinations(receiver), subject, msg);
        Transport transport = session.getTransport();
        transport.connect(account, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private InternetAddress[] emailDestinations(String receiver) throws UnsupportedEncodingException {
        String[] strs = receiver.split(",");
        int len = strs.length;
        InternetAddress[] addresses = new InternetAddress[len];
        for (int i = 0; i < len; i++) {
            addresses[i] = new InternetAddress(strs[i], "", "UTF-8");
        }
        return addresses;
    }

    private MimeMessage message(InternetAddress[] addresses, String subject, String content) throws MessagingException {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(from);
        message.setRecipients(MimeMessage.RecipientType.TO, addresses);
        message.setSubject(subject, "UTF-8");
        message.setContent(content, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        return message;
    }
}
