Java—邮件发送

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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值