package io.renren.common.utils;

import java.io.File;
import java.util.Arrays;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import io.renren.common.utils.ConfigEmail;

public class MailUtils {

    /**
     * 发送邮件的方法
     * @Param to :给谁发邮件
     * @Param subject ： 主题
     * @Param text  : 内容
     *
     */
    public static void sendMail(String toEmail, final String subject,final String text){
        try{
            //1、创建邮件对象
            JavaMailSenderImpl javaMailSender = makeJavaMailSender();
            //10、创建
            MimeMessage mimeMessage = getMimeMessage(toEmail, subject, text, javaMailSender);
            // 11、发送邮件
            javaMailSender.send(mimeMessage);

            System.out.println("发往 " + toEmail + " 邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("发往 " + toEmail + " 邮件发送异常");
            e.printStackTrace();
        }
    }

    /**
     * 发送邮件的方法
     * @Param to :给谁发邮件
     * @Param subject ： 主题
     * @Param text  : 内容
     *
     */
    public static void sendMail(String[] toEmail, final String subject,final String text){
        try{
            //1、创建邮件对象
            JavaMailSenderImpl javaMailSender = makeJavaMailSender();
            //10、创建
            MimeMessage mimeMessage = getMimeMessage(toEmail, subject, text, javaMailSender);
            // 11、发送邮件
            javaMailSender.send(mimeMessage);

            System.out.println("发往 " + toEmail + " 邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("发往 " + toEmail + " 邮件发送异常");
            e.printStackTrace();
        }
    }

    /**
     * 创建邮件对象
     * @return
     */
    private static JavaMailSenderImpl makeJavaMailSender() {
        //1、创建邮件对象
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        //2、发邮件人邮箱
        // javaMailSender.setUsername(emailConfig.getUsername());
        javaMailSender.setUsername(ConfigEmail.username);
        //3、发邮件人邮箱密码（默认使用客户端的授权码）
        javaMailSender.setPassword(ConfigEmail.password);
        //4、设置邮件服务器主机名  SMTP服务器地址
        javaMailSender.setHost(ConfigEmail.host);
        //5、SMTP服务器: 默认端口
        javaMailSender.setPort(ConfigEmail.port);
        //6、//发送邮件协议名称
        javaMailSender.setProtocol(ConfigEmail.protocol);
        //7、编码格式
        javaMailSender.setDefaultEncoding(ConfigEmail.defaultencoding);

        //8、创建连接对象，连接到邮箱服务器
        Properties mailProperties = new Properties();
        //发送服务器需要身份验证,要采用指定用户名密码的方式去认证
        mailProperties.put("mail.smtp.auth", true);
        mailProperties.put("mail.smtp.starttls.enable", true);

        //9、添加连接对象到邮件对象中
        javaMailSender.setJavaMailProperties(mailProperties);

        return javaMailSender;
    }
    
    private static MimeMessage getMimeMessage(String toEmail, String subject, String text,
        JavaMailSenderImpl javaMailSender) throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //发件人
        mimeMessageHelper.setFrom(ConfigEmail.username);
        //收件人  : 可以发送给多个收件人，该方法有一个重载的 数组形参
        mimeMessageHelper.setTo(toEmail);
    //        mimeMessage.setContent();

        //邮件主题
        mimeMessageHelper.setSubject(subject);
        //邮件内容(html格式)
        mimeMessageHelper.setText(text, true);

        return mimeMessage;
    }

    private static MimeMessage getMimeMessage(String[] toEmail, String subject, String text,
        JavaMailSenderImpl javaMailSender) throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //发件人
        mimeMessageHelper.setFrom(ConfigEmail.username);
        //收件人  : 可以发送给多个收件人，该方法有一个重载的 数组形参
        mimeMessageHelper.setTo(toEmail);
    //        mimeMessage.setContent();

        //邮件主题
        mimeMessageHelper.setSubject(subject);
        //邮件内容(html格式)
        mimeMessageHelper.setText(text, true);

        return mimeMessage;
    }

    /**
     * 发送邮件的方法
     * @Param to :给谁发邮件
     * @Param subject ： 主题
     * @Param text  : 内容
     *
     */
    public static void sendMail(String[] toEmail, final String subject,final String text,final String[] attachmentFilename, final File[] file){
        try{
            //1、创建邮件对象
            JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
            //2、发邮件人邮箱
            // javaMailSender.setUsername(emailConfig.getUsername());
            javaMailSender.setUsername(ConfigEmail.username);
            //3、发邮件人邮箱密码（默认使用客户端的授权码）
            javaMailSender.setPassword(ConfigEmail.password);
            //4、设置邮件服务器主机名  SMTP服务器地址
            javaMailSender.setHost(ConfigEmail.host);
            //5、SMTP服务器: 默认端口
            javaMailSender.setPort(ConfigEmail.port);
            //6、//发送邮件协议名称
            javaMailSender.setProtocol(ConfigEmail.protocol);
            //7、编码格式
            javaMailSender.setDefaultEncoding(ConfigEmail.defaultencoding);
    
            //8、创建连接对象，连接到邮箱服务器
            Properties mailProperties = new Properties();
            //发送服务器需要身份验证,要采用指定用户名密码的方式去认证
            mailProperties.put("mail.smtp.auth", true);
            mailProperties.put("mail.smtp.starttls.enable", true);
    
            //9、添加连接对象到邮件对象中
            javaMailSender.setJavaMailProperties(mailProperties);
            
            //10、创建
            // MimeMessage mimeMessage = getMimeMessage(toEmail, subject, text, javaMailSender);
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();

            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            //发件人
            mimeMessageHelper.setFrom(ConfigEmail.username);
            //收件人  : 可以发送给多个收件人，该方法有一个重载的 数组形参
            mimeMessageHelper.setTo(toEmail);
        //        mimeMessage.setContent();
    
            //邮件主题
            mimeMessageHelper.setSubject(subject);
            //邮件内容(html格式)
            mimeMessageHelper.setText(text, true);
            //附件
            for (int i = 0; i < attachmentFilename.length; i++) {
                mimeMessageHelper.addAttachment(attachmentFilename[i], file[i]);
            }

            // 11、发送邮件
            javaMailSender.send(mimeMessage);

            System.out.println("发往 " + Arrays.toString(toEmail) + " 邮件发送成功");
        } catch (MessagingException e) {
            System.out.println("发往 " + Arrays.toString(toEmail) + " 邮件发送异常");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String[] toEmail = {"3146695982@qq.com", "282188280@qq.com"};
        String subject = "Spring-boot Email 测试";
        String text = "<h2>测试<h2><p>这是一个测试</p>";
        String[] filename = {"测试.txt", "测试2.txt"};

        File file1 = new File("E:/ecorr/PHPMailer/fujian1.pdf");
        File file2 = new File("E:/ecorr/PHPMailer/fujian2.pdf");
        File[] file = {file1, file2};

        MailUtils.sendMail(toEmail, subject, text, filename, file);
    }
}