package com.cim.idm.wmsextend.generic.wmsmail;

import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;


public class SimpleMailSender {   
	
	private static Log log = LogFactory.getLog("SimpleMailSender");

	public boolean sendTextMail(MailSenderInfo mailInfo) {   
		// 判断是否需要身份认证   
		MyAuthenticator authenticator = null;   
		Properties pro = mailInfo.getProperties();  
		if (mailInfo.isValidate()) {   
			// 如果需要身份认证，则创建一个密码验证器   
			authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());   
		}  
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session   
		//Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
		Session sendMailSession = Session.getInstance(pro,authenticator);  
		try {   
			// 根据session创建一个邮件消息   
			Message mailMessage = new MimeMessage(sendMailSession);   
			// 创建邮件发送者地址   
			Address from = new InternetAddress(mailInfo.getFromAddress());   
			// 设置邮件消息的发送者   
			mailMessage.setFrom(from);   
			// 创建邮件的接收者地址，并设置到邮件消息中   
			// Address to = new InternetAddress(mailInfo.getToAddress());   
			//mailMessage.setRecipient(Message.RecipientType.TO,to);
			//创建邮件的接收地址（数组）
			String[] to=mailInfo.getToAddress();
			InternetAddress[] sendTo = new InternetAddress[to.length];
			for (int i = 0; i < to.length; i++)
			{
				sendTo[i] = new InternetAddress(to[i]);
			}
			mailMessage.setRecipients(MimeMessage.RecipientType.TO, sendTo);
			// 设置邮件消息的主题   
			mailMessage.setSubject(mailInfo.getSubject());   
			// 设置邮件消息发送的时间   
			mailMessage.setSentDate(new Date());   
			// 设置邮件消息的主要内容   
			String mailContent = mailInfo.getContent();   
			mailMessage.setText(mailContent);   
			// 发送邮件   
			Transport.send(mailMessage);  
			return true;   
		} catch (MessagingException ex) {   
			ex.printStackTrace();   
		}   
		return false;   
	}   


	public  boolean sendHtmlMail(MailSenderInfo mailInfo) throws MessagingException{
//		log.info("发送邮件准备  mailInfo => " + JSON.toJSONString(mailInfo));
		log.info("发送邮件准备开始  mailInfo => ");
		// 判断是否需要身份认证
		MyAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();
		//如果需要身份认证，则创建一个密码验证器 
		if (mailInfo.isValidate()) {
			authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		
		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		//Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
		Session sendMailSession = Session.getInstance(pro,authenticator);
		// 根据session创建一个邮件消息 
		Message mailMessage = new MimeMessage(sendMailSession);
		// 创建邮件发送者地址 
		Address from = new InternetAddress(mailInfo.getFromAddress());
		// 设置邮件消息的发送者 
		mailMessage.setFrom(from); 
		log.info("发送邮件准备  1 => ");
		//创建邮件的接收地址（数组）
		String[] to=mailInfo.getToAddress();
		InternetAddress[] sendTo = new InternetAddress[to.length];
		for (int i = 0; i < to.length; i++)
		{
			sendTo[i] = new InternetAddress(to[i]);
		}
		log.info("收件人地址 sendTo => " + JSON.toJSONString(sendTo));
		mailMessage.setRecipients(MimeMessage.RecipientType.TO, sendTo);
		// 设置邮件消息的主题   
		mailMessage.setSubject(mailInfo.getSubject());   
		// 设置邮件消息发送的时间   
		mailMessage.setSentDate(new Date());   
		// MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象   
		Multipart mainPart = new MimeMultipart();   
		// 创建一个包含HTML内容的MimeBodyPart   
		BodyPart html = new MimeBodyPart();   
		// 设置HTML内容     建立第一部分： 文本正文
		html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");   
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容   建立第二部分：附件
		mailMessage.setContent(mainPart);

		if(mailInfo.getAttachFileNames().length>0){
			for(int i=0;i<mailInfo.getAttachFileNames().length;i++){
				if (!mailInfo.getAttachFileNames()[i].equals("")) {
					// 建立第二部分：附件    
					html = new MimeBodyPart();
					// 获得附件
					DataSource source = new FileDataSource(mailInfo.getAttachFileNames()[i]);
					//设置附件的数据处理器
					html.setDataHandler(new DataHandler(source));
					// 设置附件文件名
					html.setFileName(mailInfo.getAttachFileNames()[i]);
					// 加入第二部分
					mainPart.addBodyPart(html);   
				}
			}
		}
		log.info("发送邮件准备  2 => ");
		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
		CommandMap.setDefaultCommandMap(mc);
		Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
		log.info("发送邮件准备  3 => ");
		// 发送邮件   
		Transport.send(mailMessage);
		log.info("发送邮件准备  4 => ");
		return true;   
	}   
	
	public static class MyAuthenticator extends Authenticator{  
		String userName=null;  
		String password=null;  

		public MyAuthenticator(){  
		}  
		public MyAuthenticator(String username, String password) {   
			this.userName = username;   
			this.password = password;   
		}   
		protected PasswordAuthentication getPasswordAuthentication(){  
			return new PasswordAuthentication(userName, password);  
		}  
	}
}