eureka、feign整合

yun-zuoyi
frin.fei 7 years ago
parent 89f13b93b4
commit bff5ec179d

@ -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_TEXTMAIL_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…
Cancel
Save