EmilUtils.java 4.47 KB
/*
 * Copyright (c) 2018, 2028, glsx and/or its affiliates. All rights reserved.
 * GLSX PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package com.glsx.task.utils;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
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 com.glsx.common.utils.PropertiesUtil;



/**  
* @ClassName: EmilUtils  
* @Description: TODO(这里用一句话描述这个类的作用)  
* @author Zheng Xiajun  
* @date 2020年3月24日 
*  
*/
public class EmilUtils {

	public final static Session EMAIL_SESSION = Session.getDefaultInstance(new Properties() {
		private static final long serialVersionUID = 7930835613494309306L;
		{
			this.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			this.setProperty("mail.smtp.socketFactory.fallback", "false");
			this.setProperty("mail.smtp.auth", "true");
		}
	});

	// 邮箱已开通,帐号:bhgreport@didihu.com.cn初始密码为:Gsx%2019 
	public final static String EMAIL_FROM_HOST = PropertiesUtil.getProperty("car.ocr.result.email.from.host");
	// "smtphz.qiye.163.com";
	public final static int EMAIL_FROM_PORT = Integer
			.parseInt(PropertiesUtil.getProperty("car.ocr.result.email.from.port"));
	public final static String EMAIL_FROM_ACCOUNT = PropertiesUtil.getProperty("car.ocr.result.email.from.account");
	public final static String EMAIL_FROM_PASSWORD = PropertiesUtil.getProperty("car.ocr.result.email.from.password");
	// public final static String EMAIL_TO_ACCOUNT = "zhengxj@qurongbao.com.cn";
	// public final static String EMAIL_TO_ACCOUNT = "linfang@didihu.com.cn";

	public final static String EMAIL_TO_ACCOUNT = PropertiesUtil.getProperty("car.ocr.result.email.to");
	public final static String SPLIT_CHAR = ";";

	public static StringBuilder EMAILCONTENT = new StringBuilder(
			"<table style=\"font-size:12px;\" width=\"97%\" border=\"1\" cellspacing=\"0\"  cellpadding=\"2\" bordercolor=\"#009900\">"
					+ "<tr><td width=\"11%\">订单编号</td><td width=\"10%\">订单信息</td><td width=\"8%\">识别结果</td>"
					+ "<td width=\"8%\">不通过原因</td><td >全车外观图</td><td width=\"8%\">识别结果</td><td width=\"8%\">不通过原因</td><td>车架号及设备SN合照</td></tr>");

	public static void sendEmail(String address, String title, String content) throws Exception {
		Transport transport = null;
		if (null != address && address.trim().length() > 0) {
			MimeMessage message = new MimeMessage(EMAIL_SESSION);
			message.setFrom(new InternetAddress(EMAIL_FROM_ACCOUNT));
			for (String addr : address.trim().split(SPLIT_CHAR)) {
				if (null != addr && addr.trim().length() > 0)
					message.addRecipient(Message.RecipientType.TO, new InternetAddress(addr.trim()));
			}
			message.setSubject(title);
			Multipart part = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(content, "text/html; charset=UTF-8");
			part.addBodyPart(contentPart);
			message.setContent(part);
			transport = EMAIL_SESSION.getTransport("smtp");
			transport.connect(EMAIL_FROM_HOST, EMAIL_FROM_PORT, EMAIL_FROM_ACCOUNT, EMAIL_FROM_PASSWORD);
			message.saveChanges();
			try {
				transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
			} finally {
				close(transport);
			}
		}
	}

	public static void close(Transport s) { // transport, store
		if (null != s) {
			try {
				s.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws Exception {
		EMAILCONTENT
				.append("<tr><td >81282020032024</td><td >有线+无线设备<br>安装单</td><td>不通过</td>"
						+ "<td >车牌号模糊</td><td >https://bhg.didihu.com.cn/group1/M00/07/39/wKgDPV5zIH2AYSF0AADiR2I_x7U420.jpg</td><td >不通过</td><td >不通过原因是XXX</td><td>https://bhg.didihu.com.cn/group1/M00/07/39/wKgDPV5zIH2AYSF0AADiR2I_x7U420.jpg</td></tr></table>");
		EmilUtils.sendEmail(EMAIL_TO_ACCOUNT, "2020年3月23日图像识别情况汇总", EMAILCONTENT.toString());
	}

}