package sendmail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 邮件发送
 * 
 * @author shenjinzhu
 */
public class SendMailUtil {

	private final static Log log = LogFactory.getLog(SendMailUtil.class);

	// 发送邮件的邮箱号（若有授权码则密码输入授权码）
	public static String myEmailAccount2 = "ccxe4zhishu@126.com";
	public static String myEmailPassword2 = "1q2w3e4r5t";
	public static String myEmailAccount = "jryqccxe@zhiweidata.com";
	public static String myEmailPassword = "1q2w3e4r";
	// 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
	// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com
	public static String myEmailSMTPHost = "smtp.ym.163.com";
	public static String myEmailSMTPHost2 = "smtp.126.com";

	/**
	 * 
	 * @param mail
	 *            接收者的邮箱号
	 * @param url
	 *            本地的附件地址
	 * @throws Exception
	 */
	public static void sendMessage(String mail, String url, String str) throws Exception {
		log.info(mail + "," + url + "," + str);
		// 创建参数配置, 用于连接邮件服务器的参数配置
		Properties props = new Properties();
		// 使用的协议（JavaMail规范要求）
		props.setProperty("mail.transport.protocol", "smtp");
		// 发件人的邮箱的 SMTP 服务器地址
		props.setProperty("mail.smtp.host", myEmailSMTPHost);
		// 需要请求认证
		props.setProperty("mail.smtp.auth", "true");
		final String smtpPort = "465";
		props.setProperty("mail.smtp.port", smtpPort);
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", smtpPort);
		Session session = Session.getDefaultInstance(props);
		session.setDebug(false); // 设置为debug模式, 可以查看详细的发送 log
		MimeMessage message;
		message = createMimeMessage(session, myEmailAccount, mail, url, str);
		Transport transport = session.getTransport();
		transport.connect(myEmailAccount, myEmailPassword);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

	public static void sendMessage2(String mail, String url, String str) throws Exception {
		log.info(mail + "," + url + "," + str);
		// 创建参数配置, 用于连接邮件服务器的参数配置
		Properties props = new Properties();
		// 使用的协议（JavaMail规范要求）
		props.setProperty("mail.transport.protocol", "smtp");
		// 发件人的邮箱的 SMTP 服务器地址
		props.setProperty("mail.smtp.host", myEmailSMTPHost2);
		// 需要请求认证
		props.setProperty("mail.smtp.auth", "true");
		final String smtpPort = "465";
		props.setProperty("mail.smtp.port", smtpPort);
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", smtpPort);
		Session session = Session.getDefaultInstance(props);
		session.setDebug(false); // 设置为debug模式, 可以查看详细的发送 log
		MimeMessage message;
		message = createMimeMessage(session, myEmailAccount2, mail, url, str);
		Transport transport = session.getTransport();
		transport.connect(myEmailAccount2, myEmailPassword2);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

	/**
	 * 创建一封只包含文本的简单邮件
	 * 
	 * @param session
	 *            和服务器交互的会话
	 * @param sendMail
	 *            发件人邮箱
	 * @param receiveMail
	 *            收件人邮箱
	 * @param url
	 *            本地文件地址
	 * @return
	 * @throws Exception
	 */
	public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String url,
			String strtitle) throws Exception {
		// 创建邮件
		MimeMessage message = new MimeMessage(session);
		SimpleDateFormat sfm = new SimpleDateFormat("yyyyMMdd");
		String str = sfm.format(new Date());
		message.setFrom(new InternetAddress(sendMail, "匹配信息结束", "UTF-8"));
		message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "UTF-8"));
		message.setSubject(str + "数据结果", "UTF-8");
		BodyPart messageBodyPart = new MimeBodyPart();
		String content = "";
		messageBodyPart.setContent(content, "text/html; charset=utf-8");
		// 创建多重消息
		Multipart multipart = new MimeMultipart();
		// 设置文本消息部分
		multipart.addBodyPart(messageBodyPart);
		// 附件部分
		messageBodyPart = new MimeBodyPart();
		// 文件本地地址
		String filename = url;
		DataSource source = new FileDataSource(filename);
		messageBodyPart.setDataHandler(new DataHandler(source));
		String hourstr = "";
		messageBodyPart.setFileName(sfm.format(new Date()) + hourstr + ".xls");
		multipart.addBodyPart(messageBodyPart);
		message.setContent(multipart);
		message.setSentDate(new Date());
		message.saveChanges();
		return message;
	}

	public static void main(String[] args) {
		try {
			SendMailUtil.sendMessage("shenjinzhu999@163.com", "C:/Users/Administrator/Desktop/zhishu.xls", "ccxe");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
