package com.elab.alert.utils;

import com.elab.alert.model.mail.MailAuthenticator;
import com.elab.alert.model.mail.MailSenderInfo;

import javax.mail.*;
import javax.mail.internet.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * 发送邮件
 *
 * @author : liukx
 * @create : 2018/11/19 10:41
 * @email : liukx@elab-plus.com
 */
public class EmailUtils {

    /**
     * 发送邮件
     *
     * @param mailInfo
     * @return
     */
    public static boolean send(MailSenderInfo mailInfo) {
        return send(mailInfo, null);
    }


    /**
     * 发送邮件
     *
     * @param mailInfo 待发送的邮件信息
     */
    public static boolean send(MailSenderInfo mailInfo, List<InputStream> fileList) {
        MailAuthenticator mailAuthenticator = null;
        Properties pro = mailInfo.getProperties();
        if (mailInfo.isValidate()) {
            mailAuthenticator = new MailAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());
        }
        Session sendMailSession = Session.getInstance(pro, mailAuthenticator);
        try {
            Message mailMessage = new MimeMessage(sendMailSession);
            Address from = new InternetAddress(mailInfo.getFromAddress());
            mailMessage.setFrom(from);
            String to[] = {mailInfo.getToAddress()};
            String toList = getMailList(to);
            InternetAddress[] iaToList = InternetAddress.parse(toList);
            mailMessage.addRecipients(Message.RecipientType.TO, iaToList);
            mailMessage.setSubject(MimeUtility.encodeText(mailInfo.getSubject(), "UTF-8", "B"));
            mailMessage.setSentDate(new Date());
            Multipart mainPart = new MimeMultipart();
            //添加邮件内容
            BodyPart html = new MimeBodyPart();
            html.setContent(mailInfo.getContent(), mailInfo.getType() + "; charset=utf-8");
            mainPart.addBodyPart(html);
            mailMessage.setContent(mainPart);
            //添加附件
//            if (ObjectUtils.isNotEmpty(fileList)) {
//                BodyPart enclosurePart = new MimeBodyPart();
//                ByteArrayDataSource fileDataSource = new ByteArrayDataSource(emailFile);
//                enclosurePart.setDataHandler(new DataHandler(fileDataSource));
//                enclosurePart.setFileName(MimeUtility.encodeText(fileDataSource.getName(), "UTF-8", "B"));
//                mainPart.addBodyPart(enclosurePart);
//            }
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
            return false;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 处理发送多人邮件
     *
     * @param mailArray
     * @return
     */
    private static String getMailList(String[] mailArray) {
        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();
    }


}

