package com.lovenav.utils;





import com.lovenav.entity.User;
import com.sun.mail.smtp.SMTPSendFailedException;
import com.sun.mail.util.MailConnectException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;

/*
 * 基于JDK实现邮件发送
 * 主要是实现激活码的发送
 * */
public class EmailUtils {

    public static String sendEmail(User user, HashMap<String,String> configMap) {

        //邮箱 lx_teach@163.com
//        String myAccount = "482370576@qq.com";
//        //授权码   java168
//        String myPass = "hlilinclupppbice";
//        //邮箱服务器
//        String SMTPHost = "smtp.qq.com";
        String myAccount=configMap.get("email_sendEmail");
        String myPass=configMap.get("email_password");
        String SMTPHost = configMap.get("email_host");

        //设置属性信息
        Properties prop = new Properties();
        //设置协议
        prop.setProperty("mail.transport.protocol", "smtp");
        //邮件服务器
        prop.setProperty("mail.smtp.host", SMTPHost);
        //认证
        prop.setProperty("mail.smtp.auth", "true");
        //1、创建会话
        Session session = Session.getDefaultInstance(prop);
        String activecode=RandomUtils.createActive();
        //设置是否需要调试
        session.setDebug(false);
        //2、创建发送信息




        //4发送信息操作
        try {
            MimeMessage message = createMsg(session, myAccount, user, activecode);
            Transport tran = session.getTransport();
            //连接
            tran.connect(myAccount, myPass);
            //发送消息
            tran.sendMessage(message, message.getAllRecipients());
            //关闭
            tran.close();

        }catch (SMTPSendFailedException s){
            s.printStackTrace();
            return "该邮箱不存在";
        }catch (MailConnectException m){
            m.printStackTrace();
            return "配置文件有错误";
        }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "配置文件有错误";}

        return activecode;
    }

    //生成邮件消息
    private static MimeMessage createMsg(Session session, String myAccount, User user,String activecode) {
        //创建消息对象
        MimeMessage message = new MimeMessage(session);
        //设置
        try {
            //3.1发送方
            message.setFrom(new InternetAddress(myAccount, "官方邮件", "utf-8"));
            //3.2设置接收方
            /*
             * MimeMessage.RecipientType.TO
             * MimeMessage.RecipientType.CC
             * MimeMessage.RecipientType.BCC
             * */
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(user.getUserEmail()));
            //3.3 设置主题
            message.setSubject("激活码", "utf-8");
            //获取本机的ip地址
//            String ip = Inet4Address.getLocalHost().getHostAddress();
//            String url = "http://" + ip + ":80/activate?e=" + Base64Utils.encode(user.getEmail()) + "&c=" + Base64Utils.encode(user.getActivatecode());
            //设置正文信息
//            message.setContent(user.getUsername() + ",欢迎你加入我们<br>为了更好体验我们的产品，请<a href='" + url + "'>点击激活 " + url + "</a>", "text/html;charset=utf-8");
            File file=new File("src/main/resources/static/sendEmailActivecode.html");
            Document document= Jsoup.parse(file,"utf-8");
            document.getElementById("0").getElementsByClass("button").append(String.valueOf(activecode.charAt(0)));
            document.getElementById("1").getElementsByClass("button").append(String.valueOf(activecode.charAt(1)));
            document.getElementById("2").getElementsByClass("button").append(String.valueOf(activecode.charAt(2)));
            document.getElementById("3").getElementsByClass("button").append(String.valueOf(activecode.charAt(3)));
            message.setContent( document.toString(), "text/html;charset=utf-8");
            //设置日期
            message.setSentDate(new Date());
            //保存
            message.saveChanges();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return message;
    }
}
