package com.dgtly.system.util;

import com.dgtly.system.domain.Mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

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

@Component
public class MailUtils {
   /* @Autowired
    JavaMailSender javaMailSender;*/
  /*  public void  sendMail(String from,String to,String subject,String text){
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom(from);//发送者
        smm.setTo(to);//收件人
//        smm.setCc(cc);//抄送人
        smm.setSubject(subject);//邮件主题
        smm.setText(text);//邮件内容
        javaMailSender.send(smm);//发送邮件
    }

    public void sendMailHtml(String from,String to,String subject,String text) {
        MimeMessage mailMessage=javaMailSender.createMimeMessage();
        //需要借助Helper类
        MimeMessageHelper helper=new MimeMessageHelper(mailMessage);

        try {
            helper.setFrom(from);
            helper.setTo(to);
//            helper.setBcc(cc);
            helper.setSubject(subject);
            helper.setSentDate(new Date());//发送时间
            helper.setText(text,true);//第一个参数要发送的内容，第二个参数是不是Html格式。

            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendFileMail(String from, String to, String subject, String text, File file){
        try {
            MimeMessage mm = javaMailSender.createMimeMessage();
            MimeMessageHelper mmh = new MimeMessageHelper(mm,true);
            mmh.setFrom(from);
            mmh.setTo(to);
            mmh.setSubject(subject);
            mmh.setText(text);
            mmh.addAttachment(file.getName(), file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/
    public  MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, Mail mail) throws Exception {
        // 1. 创建一封邮件
        MimeMessage message = new MimeMessage(session);

        // 2. From: 发件人
        message.setFrom(new InternetAddress(sendMail, mail.getFrom(), "UTF-8"));

        // 3. To: 收件人（可以增加多个收件人、抄送、密送）
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, mail.getTo(), "UTF-8"));

        // 4. Subject: 邮件主题
        message.setSubject(mail.getSubject(), "UTF-8");

        // 5. Content: 邮件正文（可以使用html标签）
        message.setContent(mail.getContent(), "text/html;charset=UTF-8");

        // 6. 设置发件时间
        message.setSentDate(new Date());

        // 7. 保存设置
        message.saveChanges();

        return message;
    }


    /*@Test
    public void sendFileMailTest(){
        String context="<b>尊敬的用户：</b><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好，管理员已为你申请了新的账号，"+
                "请您尽快通过<a href=\"http://www.liwz.top/\">链接</a>登录系统。"
                +"<br>修改密码并完善你的个人信息。<br><br><br><b>员工管理系统<br>Li，Wan Zhi</b>";
//        sendFileMail("1447151459@qq.com","qiaoxm@dgtis.com","发送带附件的邮件","邮件发送成功啦!",new File("D:\\test.png"));
        sendMailHtml("1447151459@qq.com","13323710223@189.cn","发送html的邮件",context);
    }*/
}
