parent
59806414b9
commit
ec137693cf
@ -0,0 +1,202 @@
|
||||
package cn.estsh.i3plus.core.apiservice.util;
|
||||
|
||||
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @Description : 邮件工具类
|
||||
* @Reference :
|
||||
* @Author : yunhao
|
||||
* @CreateDate : 2018-11-14 19:24
|
||||
* @Modify:
|
||||
**/
|
||||
public class MailUtil {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class);
|
||||
|
||||
// 收件人
|
||||
private Address[] to = null;
|
||||
private Address[] cc = null;
|
||||
private String from = "";
|
||||
private String nick = "";
|
||||
private String title = "";
|
||||
private String content = "";
|
||||
private String smtpHost = "";
|
||||
private int smtpPort = 25;
|
||||
private String content_type = "";
|
||||
|
||||
private String smtpUser = "";
|
||||
private String smtpPassword = "";
|
||||
private boolean isAuthenticationSMTP = false;
|
||||
|
||||
|
||||
//初始化服务器邮箱参数
|
||||
public MailUtil() {
|
||||
try {
|
||||
Properties mailInfo = PropertiesLoaderUtils.loadAllProperties("mail.properties");
|
||||
this.smtpHost = mailInfo.get("mail.host").toString();
|
||||
this.smtpPort = Integer.parseInt(mailInfo.get("mail.port").toString());
|
||||
this.from = mailInfo.get("mail.user") + "";
|
||||
this.nick = mailInfo.get("mail.nick") + "";
|
||||
this.smtpUser = mailInfo.get("mail.user") + "";
|
||||
this.smtpPassword = mailInfo.get("mail.password") + "";
|
||||
this.isAuthenticationSMTP = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置收件人地址
|
||||
*
|
||||
* @param aEmail
|
||||
* 收件人Email地址
|
||||
*/
|
||||
public void setTo(String aEmail) {
|
||||
String[] s = new String[1];
|
||||
s[0] = aEmail;
|
||||
this.to = getAddress(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置多个收件人地址
|
||||
*
|
||||
* @param Emails
|
||||
* 收件人Email地址
|
||||
*/
|
||||
public void setTo(String[] Emails) {
|
||||
this.to = getAddress(Emails);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置抄送地址
|
||||
*
|
||||
* @param aEmail
|
||||
* 抄送地址
|
||||
*/
|
||||
public void setCC(String aEmail) {
|
||||
String[] s = new String[1];
|
||||
s[0] = aEmail;
|
||||
this.cc = getAddress(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置多个抄送地址
|
||||
*
|
||||
* @param Emails
|
||||
* 抄送地址
|
||||
*/
|
||||
public void setCC(String[] Emails) {
|
||||
this.cc = getAddress(Emails);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置邮件主题
|
||||
*
|
||||
* @param mailTitle
|
||||
* 邮件主题
|
||||
*/
|
||||
public void setSubject(String mailTitle) {
|
||||
this.title = mailTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置邮件文字内容
|
||||
*
|
||||
* @param mailContent
|
||||
* 邮件文字内容
|
||||
*/
|
||||
public void setBody(String mailContent) {
|
||||
this.content = mailContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置邮件字符类型
|
||||
*
|
||||
* @param contentType
|
||||
* 请从CommonConstWords.MAIL_MODE_TEXT和MAIL_MODE_HTML中选择
|
||||
*/
|
||||
public void setContentType(String contentType) {
|
||||
this.content_type = contentType;
|
||||
}
|
||||
|
||||
private Address[] getAddress(String[] add) {
|
||||
Address[] a = new Address[add.length];
|
||||
for (int i = 0; i < add.length; i++) {
|
||||
try {
|
||||
a[i] = new InternetAddress(add[i]);
|
||||
} catch (AddressException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
/**
|
||||
* 发送邮件
|
||||
*/
|
||||
public void send() {
|
||||
try {
|
||||
// 乱码
|
||||
BASE64Encoder enc = new BASE64Encoder();
|
||||
|
||||
Properties server = new Properties();
|
||||
server.put("mail.smtp.port", String.valueOf(this.smtpPort));
|
||||
server.put("mail.smtp.host", this.smtpHost);
|
||||
if (this.isAuthenticationSMTP) {
|
||||
server.put("mail.smtp.auth", "true");
|
||||
}
|
||||
|
||||
Session conn = Session.getInstance(server, null);
|
||||
|
||||
MimeMessage msg = new MimeMessage(conn);
|
||||
if (nick != null && !"".equals(nick)) {
|
||||
msg.setSubject("=?GB2312?B?" + enc.encode(nick.getBytes())+ "?=");
|
||||
msg.setFrom(new InternetAddress(nick + " <" + from + ">"));
|
||||
} else {
|
||||
msg.setFrom(new InternetAddress(this.from));
|
||||
}
|
||||
// 收件人
|
||||
if (this.to != null) {
|
||||
msg.setRecipients(Message.RecipientType.TO, this.to);
|
||||
}
|
||||
// 抄送
|
||||
if (this.cc != null) {
|
||||
msg.setRecipients(Message.RecipientType.CC, this.cc);
|
||||
}
|
||||
|
||||
msg.setSubject("=?GB2312?B?" + enc.encode(this.title.getBytes())+ "?=");
|
||||
msg.setSubject(this.title);
|
||||
|
||||
// 是HTML格式的邮件
|
||||
msg.setContent(this.content, this.content_type);
|
||||
|
||||
msg.saveChanges();
|
||||
if (this.isAuthenticationSMTP) {
|
||||
Transport transport = conn.getTransport("smtp");
|
||||
transport.connect(this.smtpHost, this.smtpUser, this.smtpPassword);
|
||||
transport.sendMessage(msg, msg.getAllRecipients());
|
||||
transport.close();
|
||||
} else {
|
||||
Transport.send(msg, msg.getAllRecipients());
|
||||
}
|
||||
LOGGER.info("邮件发送成功");
|
||||
} catch (javax.mail.internet.AddressException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isHtmlModeMail() {
|
||||
return this.content_type.equals(CommonConstWords.MAIL_MODE_HTML);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
mail.host = smtphm.qiye.163.com
|
||||
mail.port = 25
|
||||
mail.user = dongshang@estsh.com
|
||||
mail.password = Aa111111
|
||||
mail.nick = 东尚消息
|
Loading…
Reference in New Issue