package org.ccpit.base.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.ccpit.base.user.User;

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 {
    private static String USERNAME;
    private static String PASSWORD;
    private static String SMTP;
    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");
        SMTP = configMap.get("email.smtp");
    }

    /**
     * 发送邮件
     * @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.smtp.host", SMTP);
        props.setProperty("mail.smtp.auth", "true");
        props.put("mail.smtp.user", USERNAME);
        props.put("mail.smtp.password", PASSWORD);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port",25);
        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;
    }
}
