package com.ifish.mail;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Mail {

    private MimeMessage mimeMsg; //MIME邮件对象 
    private Session session; //邮件会话对象 
    private Properties props; //系统属性 
    private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
    private static boolean needAuth = true;//smtp是否需要认证 
    private static boolean isDebug = false;//是否开启调试
    //smtp认证用户名和密码 
    private static String smtp = "smtp.qq.com";//SMTP服务器
    private static String username = "service@ifish7.com";
    private static String password = "Ifish72015";
    private static String from = "service@ifish7.com";
    //private static String to = "fun@ifish7.com";
    private static String to = "help@ifish7.com";
    private static Logger log = LoggerFactory.getLogger(Mail.class);

    /**
     * Constructor
     *
     * @param smtp 邮件发送服务器
     */
    public Mail(String smtp) {
        setSmtpHost(smtp);
        createMimeMessage();
    }

    /**
     * 设置邮件发送服务器
     *
     * @param hostName String
     */
    public void setSmtpHost(String hostName) {
        if (props == null) {
            props = System.getProperties(); //获得系统属性对象
        }
        //设置SMTP主机 
        props.put("mail.smtp.host", hostName);
        //设置SMTP是否需要验证
        props.put("mail.smtp.auth", needAuth);
        props.put("mail.smtp.starttls.enable", needAuth);
    }

    /**
     * 创建MIME邮件对象
     *
     * @return
     */
    public boolean createMimeMessage() {
        try {
            session = Session.getDefaultInstance(props, null); //获得邮件会话对象 
        } catch (Exception e) {
            log.error("获取邮件会话对象时发生错误！" + e.toString());
            return false;
        }
        try {
            mimeMsg = new MimeMessage(session); //创建MIME邮件对象 
            mp = new MimeMultipart();
            return true;
        } catch (Exception e) {
            log.error("创建MIME邮件对象失败！" + e.toString());
            return false;
        }
    }

    /**
     * 设置邮件主题
     *
     * @param mailSubject
     * @return
     */
    public boolean setSubject(String mailSubject) {
        try {
            mimeMsg.setSubject(mailSubject);
            return true;
        } catch (Exception e) {
            log.error("设置邮件主题失败" + e.toString());
            return false;
        }
    }

    /**
     * 设置邮件正文
     *
     * @param mailBody String
     */
    public boolean setBody(String mailBody) {
        try {
            BodyPart bp = new MimeBodyPart();
            bp.setContent(mailBody, "text/html;charset=GBK");
            mp.addBodyPart(bp);
            return true;
        } catch (Exception e) {
            log.error("设置邮件正文失败" + e.toString());
            return false;
        }
    }

    /**
     * 设置发信人
     *
     * @param from String
     */
    public boolean setFrom(String from) {
        try {
            mimeMsg.setFrom(new InternetAddress(from)); //设置发信人 
            return true;
        } catch (Exception e) {
            log.error("设置发信人失败" + e.toString());
            return false;
        }
    }

    /**
     * 设置收信人
     *
     * @param to String
     */
    public boolean setTo(String to) {
        try {
            mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            return true;
        } catch (Exception e) {
            log.error("设置收信人失败" + e.toString());
            return false;
        }
    }

    /**
     * 发送邮件
     */
    public boolean sendOut() {
        try {
            mimeMsg.setContent(mp);
            mimeMsg.saveChanges();
            Session mailSession = Session.getInstance(props, null);
            mailSession.setDebug(isDebug);
            Transport transport = mailSession.getTransport("smtp");
            transport.connect((String) props.get("mail.smtp.host"), username, password);
            transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
            //transport.send(mimeMsg);
            transport.close();
            return true;
        } catch (Exception e) {
            log.error("发送邮件失败" + e.toString());
            return false;
        }
    }

    /**
     * 调用sendOut方法完成邮件发送
     *
     * @param smtp
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param username
     * @param password
     * @return boolean
     */
    public static boolean send(String subject, String content) {
        Mail theMail = new Mail(smtp);

        if (!theMail.setSubject(subject)) {
            return false;
        }
        if (!theMail.setFrom(from)) {
            return false;
        }
        if (!theMail.setTo(to)) {
            return false;
        }
        if (!theMail.setBody(content)) {
            return false;
        }
        if (!theMail.sendOut()) {
            return false;
        }
        return true;
    }

}
