package org.jeecg.modules.message.handle.impl;

import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.message.entity.MailAttacch;
import org.jeecg.modules.message.handle.ISendMsgHandle;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import sun.misc.BASE64Encoder;

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class EmailSendMsgHandle implements ISendMsgHandle {

	@Override
	public void SendMsg(String es_receiver, String es_title, String es_content) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		SimpleMailMessage message = new SimpleMailMessage();
		// 设置发送方邮箱地址
		String fromName= null;//设置发件人名称
		try {
			fromName = MimeUtility.encodeText("Frontage Project Management");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
//		String fromEmail="<17887951939@163.com>";
		 String fromEmail="<itgroup@frontagelab.com.cn>";
		try {
			message.setFrom(String.valueOf(new InternetAddress(fromName+fromEmail)));
		} catch (AddressException e) {
			e.printStackTrace();
		}
		// message.setFrom("itgroup@frontagelab.com.cn");
		// message.setFrom("17887951939@163.com");
		message.setTo(es_receiver);
		message.setSubject(es_title);
		message.setText(es_content);
		mailSender.send(message);
	}

	@Override
	public void peopleSendMsg(String from, String es_receiver, String es_title, String es_content) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		SimpleMailMessage message = new SimpleMailMessage();
		// 设置发送方邮箱地址
		message.setFrom(from);
		message.setTo(es_receiver);
		message.setSubject(es_title);
		message.setText(es_content);
		mailSender.send(message);
	}

	@Override
	public void SendMsgAndFile(String es_receiver, String es_title, String es_content, List<MailAttacch> attachs) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		// SimpleMailMessage message = new SimpleMailMessage();
		// // 设置发送方邮箱地址
		// message.setFrom("17887951939@163.com");
		// message.setTo(es_receiver);
		// message.setSubject(es_title);
		// message.setText(es_content);
		// mailSender.send(message);
		MimeMessage mimeMessage=mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
			String fromName= MimeUtility.encodeText("Frontage Project Management");//设置发件人名称
//			 String fromEmail="<17887951939@163.com>";
			String fromEmail="<itgroup@frontagelab.com.cn>";
			helper.setFrom(new InternetAddress(fromName+fromEmail));
			helper.setTo(es_receiver);
			helper.setSubject(es_title);
			helper.setText(es_content);
			if (attachs != null && attachs.size()>0){
				for (MailAttacch attach : attachs){
					InputStream input = new ByteArrayInputStream(attach.getFileContent());
					DataSource dataSource = new ByteArrayDataSource(input, "application/octet-stream");
					// String fileName = URLEncoder.encode(attach.getFileName(), "UTF-8");
					BASE64Encoder enc = new BASE64Encoder();
					String fileName="=?UTF-8?B?"+enc.encode(attach.getFileName().getBytes("utf-8"))+"?=";
					helper.addAttachment(fileName, dataSource);
				}
			}
			mailSender.send(mimeMessage);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void SendMsgAndFileAndreceivers(String[] es_receiver, String es_title, String es_content, List<MailAttacch> attachs) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		// SimpleMailMessage message = new SimpleMailMessage();
		// // 设置发送方邮箱地址
		// message.setFrom("17887951939@163.com");
		// message.setTo(es_receiver);
		// message.setSubject(es_title);
		// message.setText(es_content);
		// mailSender.send(message);
		MimeMessage mimeMessage=mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
			// helper.setFrom("itgroup@frontagelab.com.cn");
			String fromName= MimeUtility.encodeText("Frontage Project Management");//设置发件人名称
//			String fromEmail="<17887951939@163.com>";
			 String fromEmail="<itgroup@frontagelab.com.cn>";
			helper.setFrom(new InternetAddress(fromName+fromEmail));
			helper.setTo(es_receiver);
			helper.setSubject(es_title);
			helper.setText(es_content);
			if (attachs != null && attachs.size()>0){
				for (MailAttacch attach : attachs){
					InputStream input = new ByteArrayInputStream(attach.getFileContent());
					DataSource dataSource = new ByteArrayDataSource(input, "application/octet-stream");
					// String fileName = URLEncoder.encode(attach.getFileName(), "UTF-8");
					BASE64Encoder enc = new BASE64Encoder();
					String fileName="=?UTF-8?B?"+enc.encode(attach.getFileName().getBytes("utf-8"))+"?=";
					helper.addAttachment(fileName, dataSource);
				}
			}
			mailSender.send(mimeMessage);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void peopleSendMsgAndFile(String from, String es_receiver, String es_title, String es_content, List<MailAttacch> attachs) {
		JavaMailSender mailSender = (JavaMailSender) SpringContextUtils.getBean("mailSender");
		// SimpleMailMessage message = new SimpleMailMessage();
		// // 设置发送方邮箱地址
		// message.setFrom("17887951939@163.com");
		// message.setTo(es_receiver);
		// message.setSubject(es_title);
		// message.setText(es_content);
		// mailSender.send(message);
		MimeMessage mimeMessage=mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
			helper.setFrom(from);
			helper.setTo(es_receiver);
			helper.setSubject(es_title);
			helper.setText(es_content);
			if (attachs != null && attachs.size()>0){
				for (MailAttacch attach : attachs){
					InputStream input = new ByteArrayInputStream(attach.getFileContent());
					DataSource dataSource = new ByteArrayDataSource(input, "application/octet-stream");
					helper.addAttachment(attach.getFileName(), dataSource);
				}
			}
			mailSender.send(mimeMessage);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}
