import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 邮件收发类
*
*
*/
public class MailManager {
/**
* 发送文本邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static void sendTextMail(String recipient, String subject, String content) {
sendMail(new String[] { recipient }, subject, content, false, null);
}
/**
* 发送文本邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static Boolean sendTextMail02(String recipient, String subject, String content) {
return sendMail02(new String[] { recipient }, subject, content, false, null);
}
/**
* 发送HTML邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static void sendHTMLMail(String recipient, String subject, String content) {
sendMail(new String[] { recipient }, subject, content, true, null);
}
/**
* 发送邮件
*
* @param recipientList
* 接收人邮件地址列表
* @param subject
* 邮件主题
* @param content
* 邮件正文
* @param isHTML
* 邮件正文是否为HTML格式,true为是,false为简单的文本格式
* @param attachments
* 附件列表
*/
public static void sendMail(String[] recipientList, String subject, String content, boolean isHTML, File[] attachments) {
if (recipientList == null || recipientList.length == 0) {
log.warn("Cannot send e-mail: no recipients.");
return;
}
String from = ConfManager.getConfig("mail-from");
String host = ConfManager.getConfig("mail-smtp-host");
String auth = ConfManager.getConfig("mail-smtp-auth");
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session session = Session.getDefaultInstance(props);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for (String recipient : recipientList) {
if (StringUtils.isNotBlank(recipient)) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
}
message.setSubject(subject);
if (attachments != null && attachments.length > 0) {
Multipart multipart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
if (isHTML) {
bodyPart.setContent(content, "text/html");
} else {
bodyPart.setText(content);
}
multipart.addBodyPart(bodyPart);
for (File att : attachments) {
if (att != null) {
bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(att);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(att.getName());
multipart.addBodyPart(bodyPart);
}
}
message.setContent(multipart);
} else {
if (isHTML) {
message.setContent(content, "text/html");
} else {
message.setText(content);
}
}
String user = ConfManager.getConfig("mail-user");
String password = ConfManager.getConfig("mail-password");
Transport.send(message, user, password);
log.info("E-mail has been sent successfully.");
} catch (MessagingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
throw new NanhuiRuntimeException(e.getMessage(), e);
}
}
/**
* 发送邮件
*
* @param recipientList
* 接收人邮件地址列表
* @param subject
* 邮件主题
* @param content
* 邮件正文
* @param isHTML
* 邮件正文是否为HTML格式,true为是,false为简单的文本格式
* @param attachments
* 附件列表
*/
public static Boolean sendMail02(String[] recipientList, String subject, String content, boolean isHTML, File[] attachments) {
if (recipientList == null || recipientList.length == 0) {
log.warn("Cannot send e-mail: no recipients.");
return false;
}
boolean success = false;
String from = ConfManager.getConfig("mail-from");
String host = ConfManager.getConfig("mail-smtp-host");
String auth = ConfManager.getConfig("mail-smtp-auth");
String nick = ConfManager.getConfig("mail-nick");
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session session = Session.getDefaultInstance(props);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, nick));
for (String recipient : recipientList) {
if (StringUtils.isNotBlank(recipient)) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
}
message.setSubject(subject);
if (attachments != null && attachments.length > 0) {
Multipart multipart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
if (isHTML) {
bodyPart.setContent(content, "text/html");
} else {
bodyPart.setText(content);
}
multipart.addBodyPart(bodyPart);
for (File att : attachments) {
if (att != null) {
bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(att);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(att.getName());
multipart.addBodyPart(bodyPart);
}
}
message.setContent(multipart);
} else {
if (isHTML) {
message.setContent(content, "text/html");
} else {
message.setText(content);
}
}
String user = ConfManager.getConfig("mail-user");
String password = ConfManager.getConfig("mail-password");
Transport.send(message, user, password);
success = true;
log.info("E-mail has been sent successfully.");
} catch (MessagingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
success = false;
throw new NanhuiRuntimeException(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
success = false;
throw new NanhuiRuntimeException(e.getMessage(), e);
}
return success;
}
}
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 邮件收发类
*
*
*/
public class MailManager {
/**
* 发送文本邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static void sendTextMail(String recipient, String subject, String content) {
sendMail(new String[] { recipient }, subject, content, false, null);
}
/**
* 发送文本邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static Boolean sendTextMail02(String recipient, String subject, String content) {
return sendMail02(new String[] { recipient }, subject, content, false, null);
}
/**
* 发送HTML邮件
*
* @param recipient
* 接收人邮件地址
* @param subject
* 邮件主题
* @param content
* 邮件正文
*/
public static void sendHTMLMail(String recipient, String subject, String content) {
sendMail(new String[] { recipient }, subject, content, true, null);
}
/**
* 发送邮件
*
* @param recipientList
* 接收人邮件地址列表
* @param subject
* 邮件主题
* @param content
* 邮件正文
* @param isHTML
* 邮件正文是否为HTML格式,true为是,false为简单的文本格式
* @param attachments
* 附件列表
*/
public static void sendMail(String[] recipientList, String subject, String content, boolean isHTML, File[] attachments) {
if (recipientList == null || recipientList.length == 0) {
log.warn("Cannot send e-mail: no recipients.");
return;
}
String from = ConfManager.getConfig("mail-from");
String host = ConfManager.getConfig("mail-smtp-host");
String auth = ConfManager.getConfig("mail-smtp-auth");
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session session = Session.getDefaultInstance(props);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for (String recipient : recipientList) {
if (StringUtils.isNotBlank(recipient)) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
}
message.setSubject(subject);
if (attachments != null && attachments.length > 0) {
Multipart multipart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
if (isHTML) {
bodyPart.setContent(content, "text/html");
} else {
bodyPart.setText(content);
}
multipart.addBodyPart(bodyPart);
for (File att : attachments) {
if (att != null) {
bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(att);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(att.getName());
multipart.addBodyPart(bodyPart);
}
}
message.setContent(multipart);
} else {
if (isHTML) {
message.setContent(content, "text/html");
} else {
message.setText(content);
}
}
String user = ConfManager.getConfig("mail-user");
String password = ConfManager.getConfig("mail-password");
Transport.send(message, user, password);
log.info("E-mail has been sent successfully.");
} catch (MessagingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
throw new NanhuiRuntimeException(e.getMessage(), e);
}
}
/**
* 发送邮件
*
* @param recipientList
* 接收人邮件地址列表
* @param subject
* 邮件主题
* @param content
* 邮件正文
* @param isHTML
* 邮件正文是否为HTML格式,true为是,false为简单的文本格式
* @param attachments
* 附件列表
*/
public static Boolean sendMail02(String[] recipientList, String subject, String content, boolean isHTML, File[] attachments) {
if (recipientList == null || recipientList.length == 0) {
log.warn("Cannot send e-mail: no recipients.");
return false;
}
boolean success = false;
String from = ConfManager.getConfig("mail-from");
String host = ConfManager.getConfig("mail-smtp-host");
String auth = ConfManager.getConfig("mail-smtp-auth");
String nick = ConfManager.getConfig("mail-nick");
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session session = Session.getDefaultInstance(props);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, nick));
for (String recipient : recipientList) {
if (StringUtils.isNotBlank(recipient)) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
}
message.setSubject(subject);
if (attachments != null && attachments.length > 0) {
Multipart multipart = new MimeMultipart();
BodyPart bodyPart = new MimeBodyPart();
if (isHTML) {
bodyPart.setContent(content, "text/html");
} else {
bodyPart.setText(content);
}
multipart.addBodyPart(bodyPart);
for (File att : attachments) {
if (att != null) {
bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(att);
bodyPart.setDataHandler(new DataHandler(source));
bodyPart.setFileName(att.getName());
multipart.addBodyPart(bodyPart);
}
}
message.setContent(multipart);
} else {
if (isHTML) {
message.setContent(content, "text/html");
} else {
message.setText(content);
}
}
String user = ConfManager.getConfig("mail-user");
String password = ConfManager.getConfig("mail-password");
Transport.send(message, user, password);
success = true;
log.info("E-mail has been sent successfully.");
} catch (MessagingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
success = false;
throw new NanhuiRuntimeException(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
log.error("Error when sending e-mail: " + e.getMessage());
success = false;
throw new NanhuiRuntimeException(e.getMessage(), e);
}
return success;
}
}