package org.ccpit.base.utils;

import org.apache.log4j.Logger;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * 邮件发送工具类
 * Date: 2015年10月29日 09:58
 *
 * @author 孙其鹏
 * @version 1.0
 */
public class MailSender {
    public static String USERNAME;
    public static String PASSWORD;
    public static String HOST;
    private static final Logger logger = Logger.getLogger(MailSender.class);

    static {
        Map<String, String> configMap = ReadConfigUtil.getPlatformConfig();
        USERNAME = configMap.get("email.name");
        PASSWORD = configMap.get("email.password");
        HOST = configMap.get("email.host");
    }

    /**
     * 发送邮件
     *
     * @param targetAddress 收件人
     * @param subject       主题
     * @param body          内容
     * @return 发送成功
     */
    public static boolean send(String targetAddress, String subject, String body) {
        EmailBody emailBody = new EmailBody();
        emailBody.setContent(body);
        emailBody.setSubject(subject);
        emailBody.setSentDate(new Date());
        return send(targetAddress, emailBody);
    }

    /**
     * 发送邮件
     *
     * @param: @param userName 邮箱账号
     * @param: @param password 邮箱密码
     * @param: @param targetAddress 目标邮箱地址
     * @param: @param mimeDTO 邮件部分参数
     * @return: boolean
     */
    public static boolean send(String targetAddress, EmailBody body) {
        Properties props = new Properties();
        props.put("mail.pop.host", HOST);
        props.setProperty("mail.pop.auth", "true");
        props.put("mail.pop.user", USERNAME);
        props.put("mail.pop.password", PASSWORD);
        props.put("mail.pop.starttls.enable", "true");
        props.put("mail.transport.protocol", "POP3");
        props.put("mail.pop.port", 465);
        Session session = Session.getInstance(props, new PopupAuthenticator(USERNAME, PASSWORD));

        boolean success = false;
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(USERNAME));
            msg.setRecipients(Message.RecipientType.TO, targetAddress);
            msg.setSubject(body.getSubject());
            msg.setSentDate(body.getSentDate());
            BodyPart mdp = new MimeBodyPart();
            mdp.setContent(body.getContent(), "text/html;charset=utf-8");
            Multipart mm = new MimeMultipart();
            mm.addBodyPart(mdp);
            msg.setContent(mm);
            msg.saveChanges();

            //发送邮件
            Transport.send(msg);
            success = true;
            logger.info("成功发送了邮件到" + targetAddress + " 主题:[" + body.getSubject() + "] 内容[" + body.getContent() + "]");
        } catch (Exception e) {
            success = false;
            logger.warn("到" + targetAddress + "的邮件发送失败，" + e);
//            throw new RuntimeException("email send error",e);
        }
        return success;
    }
}
