列如:邮件配置:
application-test.properties
#################Email config start###############################
MAIL_SMTPSERVER=192.168.3.253
##if it is more than one,please separating it by "," For example "aa@icil.net,bb@icil.net"
MAIL_SEND_TO=qqq.@163.com
MAIL_FROM=www@qq.net
MAIL_SEND_TO_FAIL=afsa2_test@qq.net #################Email config end###############################
对应的类:
@Component
public class EmailConfig { private static String MAIL_SMTPSERVER; private static String MAIL_SEND_TO; private static String MAIL_FROM; private static String MAIL_SEND_TO_FAIL; public static String getMAIL_SMTPSERVER() {
return MAIL_SMTPSERVER;
} @Value("${MAIL_SMTPSERVER}")
public void setMAIL_SMTPSERVER(String mAIL_SMTPSERVER) {
MAIL_SMTPSERVER = mAIL_SMTPSERVER;
} public static String getMAIL_SEND_TO() {
return MAIL_SEND_TO;
} @Value("${MAIL_SEND_TO}")
public void setMAIL_SEND_TO(String mAIL_SEND_TO) {
MAIL_SEND_TO = mAIL_SEND_TO;
} public static String getMAIL_FROM() {
return MAIL_FROM;
} @Value("${MAIL_FROM}")
public void setMAIL_FROM(String mAIL_FROM) {
MAIL_FROM = mAIL_FROM;
} public static String getMAIL_SEND_TO_FAIL() {
return MAIL_SEND_TO_FAIL;
} @Value("${MAIL_SEND_TO_FAIL}")
public void setMAIL_SEND_TO_FAIL(String mAIL_SEND_TO_FAIL) {
MAIL_SEND_TO_FAIL = mAIL_SEND_TO_FAIL;
} }
MailUtils:
package com.icil.esolution.utils; import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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 javax.mail.internet.MimeUtility; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.icil.esolution.config.EmailConfig; /**
* ********************************************
*
* @ClassName: MailUtils
*
* @Description:
*
* @author Sea
*
* @date 24 Aug 2018 8:34:09 PM
*
***************************************************
*/
@SuppressWarnings("all")
public class MailUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);
private static final String MAIL_SEND_TO_FAIL="MAIL_SEND_TO_FAIL";
private Vector file = new Vector();// 附件文件集合 public static boolean sendMail(String subject, String content) {
boolean mails = new MailUtils().sendMails(null, null, subject, content, null);
return mails;
}
/**
* this use default config
* @param subject
* @param content
* @return
*/
public static boolean sendFailedMail(String subject, String content) {
//String tofail = properties.getProperty(MAIL_SEND_TO_FAIL);
//String tocommon = properties.getProperty(Constant.MAIL_SEND_TO);
String tofail = EmailConfig.getMAIL_SEND_TO_FAIL();
String tocommon = EmailConfig.getMAIL_SEND_TO();
boolean mails = new MailUtils().sendMails(tofail+","+tocommon, null, subject, content, null);
return mails;
} public static boolean sendMail(String to, String subject, String content) {
boolean mails = new MailUtils().sendMails(to, null, subject, content, null);
return mails;
} public static boolean sendMail(String to, String from, String subject, String content) {
boolean mails = new MailUtils().sendMails(to, from, subject, content, null);
return mails;
} /**
* @return
* @Description: 发送邮件,发送成功返回true 失败false
* @author sea
* @Date 2018年8月3日
*/
private boolean sendMails(String to1, String from1, String subject, String content, String filename) { String to = EmailConfig.getMAIL_SEND_TO();
String from = EmailConfig.getMAIL_FROM();
String host = EmailConfig.getMAIL_SMTPSERVER();
LOGGER.info("to is {}",to);
LOGGER.info("from is {}",from);
LOGGER.info("host is {}",host);
String username = "";
String password =""; if (to1 != null) {
to = to1;
}
if (from1 != null) {
from = from1;
}
if(content==null){
content = "";
}
// 构造mail session
Properties props = new Properties();
props.put("mail.smtp.host", host);
// props.put("mail.smtp.port", "25");//
props.put("mail.smtp.auth","false"); // Session session = Session.getDefaultInstance(props, new Authenticator() {
// public PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(username, password);
// }
// });
Session session=Session.getDefaultInstance(props);
try {
// 构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from)); // msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to));
// msg.addRecipients(Message.RecipientType.CC, address);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
subject = transferChinese(subject);
msg.setSubject(subject); // 构造Multipart
Multipart mp = new MimeMultipart(); // 向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setContent(content, "text/html;charset=utf-8"); // 向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent); // 向Multipart添加附件
Enumeration efile = file.elements();
while (efile.hasMoreElements()) { MimeBodyPart mbpFile = new MimeBodyPart();
filename = efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
filename = new String(fds.getName().getBytes(), "ISO-8859-1"); mbpFile.setFileName(filename);
// 向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile); } file.removeAllElements();
// 向Multipart添加MimeMessage
msg.setContent(mp);
msg.setSentDate(new Date());
msg.saveChanges(); // 发送邮件
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (Exception mex) {
mex.printStackTrace();
return false;
}
return true;
} /**
* @param strText
* @return
* @Description: 把主题转为中文 utf-8
* @Date 2017年6月22日 上午10:37:07
*/
public String transferChinese(String strText) {
try {
strText = MimeUtility.encodeText(new String(strText.getBytes(), "utf-8"), "utf-8", "B");
} catch (Exception e) {
e.printStackTrace();
}
return strText;
} public void attachfile(String fname) {
file.addElement(fname);
} /**
*
* @Description:test ok 2018-08-24
*
* @author Sea
*
* @date 24 Aug 2018 8:34:09 PM
*/
public static void main(String[] args) {
boolean mails = new MailUtils().sendMails("", "", "hello", "I love you icil", null);
System.out.println(mails);
} }