package com.easeye.test;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.alibaba.fastjson.JSONObject;
import com.sun.mail.smtp.SMTPAddressSucceededException;
import com.sun.mail.smtp.SMTPMessage;
import com.sun.mail.smtp.SMTPTransport;

public class Main {
	public static void main(String[] args) throws MessagingException,
			UnsupportedEncodingException, InterruptedException {
		JSONObject jsonObject = new JSONObject();

		//X-Easeye-API的设置,以下为可选设置
		jsonObject.put("category", "aa");
		//jsonObject.put("campaign", "bb");
		JSONObject jsonObjecta = new JSONObject();
		jsonObjecta.put("track_open", true);
		jsonObjecta.put("track_click", true);
		jsonObjecta.put("filter_by_new_contact_livelines", 0);
		jsonObjecta.put("track_subscription", false);
		jsonObjecta.put("custom_domain", "http://www.custom.com");
		jsonObject.put("send_options", jsonObjecta);

		Multipart multipart = new MimeMultipart("alternative");
		
		//添加正文内容，html格式的		
		MimeBodyPart htmlBodyPart = new MimeBodyPart();
		htmlBodyPart.setContent("html邮件内容", "text/html; charset=\"UTF-8\"");
		multipart.addBodyPart(htmlBodyPart);
		//添加正文内容，text格式的		
		MimeBodyPart textBodyPart = new MimeBodyPart();
		textBodyPart.setContent("text邮件内容", "text/plain; charset=\"UTF-8\"");
		multipart.addBodyPart(textBodyPart);
		
		//添加附件
//		MimeBodyPart attachmentBodyPart = new MimeBodyPart();
//        String filename = "/home/manisha/file.txt";
//        DataSource source = new FileDataSource(filename);
//        attachmentBodyPart.setDataHandler(new DataHandler(source));
//        attachmentBodyPart.setFileName(filename);
//        multipart.addBodyPart(attachmentBodyPart);

		Properties prop = new Properties();
		prop.put("mail.debug", true + "");
		prop.put("mail.smtp.timeout", 60 * 1000);
		prop.put("mail.smtp.connectiontimeout", 60 * 1000);
		prop.setProperty("mail.smtp.host", "smtp.effilink.co");
		prop.setProperty("mail.smtp.port", "1027");
		
		//ip认证设置
		//prop.put("mail.smtp.auth", "false");
		//prop.put("mail.smtp.host", "smtp.effilink.co");
		
		//指定本地绑定ip、端口
		//prop.put("mail.smtp.localaddress", "192.168.100.2");
		//prop.put("mail.smtp.localport", 23456);
		
		//ssl
//		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//		prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
//		prop.setProperty("mail.smtp.socketFactory.fallback", "false");
//		prop.setProperty("mail.smtp.socketFactory.port", "4027");
		
		Session ssn = Session.getInstance(prop, null);

		SMTPMessage message = new SMTPMessage(ssn);
		// 设置主题
		message.setSubject("邮件主题");
		// 设置控制参数，可选
		message.setHeader("X-Easeye-API", jsonObject.toJSONString());

		message.setContent(multipart, "text/html; charset=\"UTF-8\"");
		Address address = new InternetAddress("linghao.dong@easeye.com.cn",
				"From别名");
		// 设置发送方信息
		message.setFrom(address);
		//设置回复地址，支持写多个回复地址，但有的邮件软件兼容性不太好不一定能回复多个，建议只写一个
		Address[] replys = new Address[1];
		replys[0] = new InternetAddress("aaa@xxx.com");
		message.setReplyTo(replys);
		// 设置接收方信息
		message.addRecipients(RecipientType.TO, "shiyu.shen@easeye.net");
		//message.addRecipients(RecipientType.TO, "..shiyu.shen@easeye.net");
		// 设置需要抄送的接收方信息
		//message.addRecipients(RecipientType.CC, "ccc@easeye.net");
		// 设置需要密抄的接收方信息
		//message.addRecipients(RecipientType.BCC, "ddd@easeye.net");

		message.getAllRecipients();
		SMTPTransport tran = (SMTPTransport) ssn.getTransport("smtp");
		tran.connect("用户名不为空即可，建议填写联系邮箱", "api-key");
		//ip认证设置
		//tran.connect();
		
		tran.setReportSuccess(true);
		message.setSendPartial(true);
		message.saveChanges();
		try {
			tran.sendMessage(message, message.getAllRecipients());
			//Thread.sleep(10000);
			
		} catch (Exception e) {
			if (e.getCause() instanceof SMTPAddressSucceededException) {
				// 发送成功,或部分发送成功
				System.out.println("成功：" + e.getMessage());
				
			} else {
				System.out.println("失败：" + e.getMessage());
			}
			//Thread.sleep(10000);
		}
		finally {
			//Thread.sleep(10000);
			tran.close();
		}
		
	}
}
