/**
* 邮件客户端
*
* @author zhouqian
*
*/
public class MailClient {
/** 日志实例 */
Log logger = LogFactory.getLog(MailClient.class);
/** 消息对象 */
private Message message;
/** 邮件会话 */
private Session session;
public MailClient() {
super();
this.session = createDefaultSession();
}
public MailClient(Session session) {
super();
this.session = session;
}
public MailClient(Message message) {
this();
this.message = message;
}
public MailClient(Session session, Message message) {
super();
this.session = session;
this.message = message;
}
/**
* 创建邮件会话
*
* @return
*/
protected Session createDefaultSession() {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
return session;
}
/**
* 创建纯文本邮件
*
* @param recipientTO
* @param recipientCC
* @param recipientBCC
* @return
* @throws MessagingException
*/
protected Message createMimeMessage(String subject, String content,
String recipientTO, String recipientCC, String recipientBCC)
throws MessagingException {
Message message = createBlankMessage(recipientTO, recipientCC,
recipientBCC);
// 设置邮件标题
message.setSubject(subject);
// 设置邮件内容
message.setText(content);
// 设置发送时间
message.setSentDate(new Date(System.currentTimeMillis()));
// 存储邮件信息
message.saveChanges();
return message;
}
/**
* 创建带HTML内容的邮件
*
* @param subject
* @param content
* @param recipientTO
* @param recipientCC
* @param recipientBCC
* @return
* @throws MessagingException
*/
protected Message createHTMLMessage(String subject, String content,
String recipientTO, String recipientCC, String recipientBCC)
throws MessagingException {
Message message = createBlankMessage(recipientTO, recipientCC,
recipientBCC);
// 设置邮件标题
message.setSubject(subject);
// 设置发送时间
message.setSentDate(new Date(System.currentTimeMillis()));
// 创建存放邮件内容的BodyPart对象
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html;charset=gb2312");
// 创建一个MimeMultipart来存放BodyPart对象
Multipart mp = new MimeMultipart();
mp.addBodyPart(bp);
message.setContent(mp);
message.saveChanges();
return message;
}
/**
* 创建带附件的邮件
*
* @param subject
* @param content
* @param recipientTO
* @param recipientCC
* @param recipientBCC
* @return
* @throws MessagingException
*/
protected Message createAttachMessage(String subject, String content,
File attachment, String recipientTO, String recipientCC,
String recipientBCC) throws MessagingException {
Message message = createBlankMessage(recipientTO, recipientCC,
recipientBCC);
// 设置邮件标题
message.setSubject(subject);
// 设置发送时间
message.setSentDate(new Date(System.currentTimeMillis()));
// 创建存放邮件内容的BodyPart对象
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html;charset=gb2312");
// 创建一个MimeMultipart来存放BodyPart对象
Multipart mp = new MimeMultipart();
mp.addBodyPart(bp);
// 设置邮件的附件
bp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment.getName());
DataHandler dh = new DataHandler(fds);
try {
bp.setFileName(new String(attachment.getName().getBytes("gb2312")));
} catch (UnsupportedEncodingException e) {
final String errMess = "Caught exception while encoding file name:"
+ attachment.getName();
logger.error(errMess);
throw new MailException(errMess, e);
}
bp.setDataHandler(dh);
mp.addBodyPart(bp);
message.setContent(mp);
message.saveChanges();
return message;
}
/**
* 创建空白邮件
*
* @param recipientTO
* @param recipientCC
* @param recipientBCC
* @return
* @throws MessagingException
*/
protected Message createBlankMessage(String recipientTO,
String recipientCC, String recipientBCC) throws MessagingException {
Message message = new MimeMessage(session);
// 设置发件人
InternetAddress from = new InternetAddress("mfktfp2004@163.com");
message.setFrom(from);
// 设置收件人
InternetAddress to = new InternetAddress(recipientTO);
message.setRecipient(Message.RecipientType.TO, to);
if (StringUtil.isNotEmpty(recipientCC)) {
InternetAddress cc = new InternetAddress(recipientCC);
message.setRecipient(Message.RecipientType.CC, cc);
}
if (StringUtil.isNotEmpty(recipientBCC)) {
InternetAddress bcc = new InternetAddress(recipientBCC);
message.setRecipient(Message.RecipientType.BCC, bcc);
}
return message;
}
/**
* 发送邮件
*
* @param message
* @throws MessagingException
*/
public void sendEmail(Message message) throws MessagingException {
// 以smtp方式登陆邮箱
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", "mfktfp2004", "19850921"); // SMTP地址,用户名,密码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 发送纯文本邮件
*
* @param subject
* @param content
* @param recipientTO
* @param recipientCC
* @param recipientBCC
*/
public void sendMimeEmail(String subject, String content,
String recipientTO, String recipientCC, String recipientBCC) {
Message message = null;
try {
message = createMimeMessage(subject, content, recipientTO,
recipientCC, recipientBCC);
sendEmail(message);
} catch (MessagingException e) {
logger.error("Send mime email failure", e);
throw new MailException("Error sending email, failure", e);
}
}
/**
* 发送带HTML内容的邮件
*
* @param subject
* @param content
* @param recipientTO
* @param recipientCC
* @param recipientBCC
*/
public void sendHTMLEmail(String subject, String content,
String recipientTO, String recipientCC, String recipientBCC) {
Message message = null;
try {
message = createHTMLMessage(subject, content, recipientTO,
recipientCC, recipientBCC);
sendEmail(message);
} catch (MessagingException e) {
logger.error("Send html email failure", e);
throw new MailException("Error sending email, failure", e);
}
}
/**
* 发送带附件的邮件
*
* @param subject
* @param content
* @param recipientTO
* @param recipientCC
* @param recipientBCC
*/
public void sendAttachEmail(String subject, String content,
File attachment, String recipientTO, String recipientCC,
String recipientBCC) {
Message message = null;
try {
message = createAttachMessage(subject, content, attachment,
recipientTO, recipientCC, recipientBCC);
sendEmail(message);
} catch (MessagingException e) {
logger.error("Send html email failure", e);
throw new MailException("Error sending email, failure", e);
}
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public static void main(String[] args) {
MailClient client = new MailClient();
client.sendMimeEmail("test", "test", "zhouqian1103@163.com", null, null);
}
}
public class MailException extends RuntimeException {
private static final long serialVersionUID = 1L;
/** Throwable实例 */
protected Throwable throwable;
public MailException() {
super();
}
public MailException(String message) {
super(message);
}
public MailException(Throwable cause) {
this.throwable = cause;
}
public MailException(String message, Throwable cause) {
super(message);
this.throwable = cause;
}
public void printStackTrace(PrintStream ps) {
super.printStackTrace(ps);
if (throwable != null) {
ps.println("with nested Exception:" + throwable);
throwable.printStackTrace(ps);
}
}
public void printStackTrace(PrintWriter pw) {
super.printStackTrace(pw);
if (throwable != null) {
pw.println("with nested Exception:" + throwable);
throwable.printStackTrace(pw);
}
}
public String toString() {
if (throwable == null) {
return super.toString();
} else {
return super.toString() + "with nested exception:" + throwable;
}
}
public Throwable getThrowable() {
return throwable;
}
}