package com.zhangyue.dj.bi.service.common.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
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.ArrayList;
import java.util.List;
import java.util.Properties;
import com.zhangyue.dj.bi.pojo.inner.Config;
import com.zhangyue.dj.bi.web.controller.common.CommonController;
/**
* TODO:DOCUMENT ME!
*
* @author ME
* @date 2019/10/18
*/
@Service
public class SendMail {
private static final Logger LOG = LoggerFactory.getLogger(CommonController.class);
public static void main(String[] args) throws MessagingException {
LOG.error("sendMail-----------111------------------");
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.zhangyue.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
properties.put("mail.smtp.ssl.trust", "smtp.zhangyue.com");//指定信任该 host,解决缺少证书的异常
// 得到回话对象
Session session = Session.getInstance(properties);
LOG.error("sendMail-----------222------------------");
// 获取邮件对象
Message message = new MimeMessage(session);
LOG.error("transport-----------333------------------");
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("xuyingjie@zhangyue.com"));
// 设置收件人邮箱地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xuyingjie@zhangyue.com"));
// 设置邮件标题
message.setSubject("xuyingjie-test");
// 设置邮件内容
message.setContent(getContent());
// 得到邮差对象
Transport transport = session.getTransport();
LOG.error("transport-----------666------------------");
// 连接自己的邮箱账户
transport.connect("xuyingjie@zhangyue.com", "XYJxyj880217!");
LOG.error("sendMail-----------444------------------");
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
LOG.error("sendMail-----------555------------------");
transport.close();
}
private static Multipart getContent() throws MessagingException {
List<Config> list = new ArrayList<>();
list.add(new Config("1、工作<br/>" +
"2、工作工作工作工作工作工作<br/>" +
"3、工作", "1、工作", 1));
list.add(new Config("3", "4", 1));
StringBuilder content = new StringBuilder("<html><head></head><body><p>hi,ALL</p>");
content.append(
"<table border=\"0\"width=\"95%\"height=\"50%\"cellpadding=\"1\"style=\"margin:15px\"cellspacing=\"1\"bordercolor=\"#D3D3D3\"bgcolor=\"#9F9F9F\">");
content.append("<tr bgcolor=\"#D3D3D3\" style=\"margin:10px\">" +
"<th style=\"padding:6;font-size:16\">第一列</th><th style=\"padding:6;font-size:16\">第二列</th>" +
"<th style=\"padding:6;font-size:16\">第三列</th><th style=\"padding:6;font-size:16\">第四列</th></tr>");
for (Config data : list) {
content.append("<tr bgcolor=\"#FFFFFF\">");
content.append("<td style=\"padding:6;font-size:14\">" + data.getcKey() + "</td>"); //第一列
content.append("<td style=\"padding:6;font-size:14\">" + data.getcValue() + "</td>"); //第二列
content.append("<td style=\"padding:6;font-size:14\">" + data.getType() + "</td>"); //第三列
content.append("<td style=\"padding:6;font-size:14\">" + data.getcKey() + "</td>"); //第四列
content.append("</tr>");
}
content.append("</table>");
content.append("</body></html>");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(content.toString(), "text/html; charset=utf-8");
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mp = new MimeMultipart("alternative");//mixed related alternative
mp.addBodyPart(htmlPart);
// 将MiniMultipart对象设置为邮件内容
return mp;
}
}