最近公司在做发送邮件的项目,本人以前没有接触过,看了网上的代码之后,整理了一下自己的学习心得。希望跟大家一起分享!!!
这里包含 多收件人 多抄送人 多附件
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import com.hc.mail.handler.MailHandler;
/**
* @author 作者 hujingbo
* @version 创建时间:2017-9-20 上午9:42:09
* 类说明 群发邮件 并且带有附件
*/
public class SendMultipleMail {
public static void main(String[] args) throws Exception {
List<String> filepath = new ArrayList<String>();
filepath.add("D:/123.jpg");
filepath.add("D:/students.xls");
sendMail("1051064497@qq.com ,huxiaoming625@163.com","1051064497@qq.com ,huxiaoming625@163.com", "注册信息邮件","注册邮件,有附件", filepath);
}
/**
* @throws Exception
* @Description:
* @Anthor: hujingbo
* @param to
* 收件人列表
* @param cc
* 抄送人列表
* @param subject
* 发送邮件的主题
* @param body
* 发送邮件的内容
* @param filepath
* 添加附件的路径
* @return
* @throws
*/
public static boolean sendMail(String to ,String cc, String subject, String body,List<String> filepath) throws Exception {
if (subject == null) {
subject = "无主题邮件";
}
if (body == null) {
body = "";
}
// 加载配置信息
MailHandler md = new MailHandler();
Properties prop = md.conf;
String hostName = prop.getProperty("mail.host");
String username = prop.getProperty("mail.username");
String password = prop.getProperty("mail.password");
String from = "service@pjsfax.com"; // 发件人信息
System.out.println("配置信息:" + hostName + "/" + username + "/" + password);
Session session = Session.getInstance(prop);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setHeader("邮件标题", "dfsdfsdf");
MimeMultipart multipart = new MimeMultipart();
// 邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是邮件正文内容", "text/html;charset=UTF-8");
multipart.addBodyPart(text);
// 多个收件人地址
InternetAddress[] addressesTo = null;
if (to != null && to.trim().length() > 0) {
String[] receiveList = to.split(",");
int receiveCount = receiveList.length;
if (receiveCount > 0) {
addressesTo = new InternetAddress[receiveCount];
for (int i = 0; i < receiveCount; i++) {
addressesTo[i] = new InternetAddress(receiveList[i]);
}
}
} else {
System.out.println("None receiver!");
return false;
}
// 多个抄送人地址
InternetAddress[] addressesCc = null;
if (cc != null && cc.trim().length() > 0) {
String[] copyList = to.split(",");
int copyCount = copyList.length;
if (copyCount > 0) {
addressesCc = new InternetAddress[copyCount];
for (int i = 0; i < copyCount; i++) {
addressesCc[i] = new InternetAddress(copyList[i]);
}
}
}
//多个附件
if (filepath != null && filepath.size() > 0) {
for (String path : filepath) {
MimeBodyPart bodyPart = new MimeBodyPart();
DataSource dh = new FileDataSource(path);
bodyPart.setDataHandler(new DataHandler(dh));
bodyPart.setFileName(dh.getName());
multipart.addBodyPart(bodyPart);
}
}
message.setContent(multipart);
message.setRecipients(RecipientType.TO, addressesTo);
message.setRecipients(RecipientType.CC, addressesCc);
message.setSubject(subject);
message.setSentDate(new Date());
message.saveChanges();
Transport transport = session.getTransport();
transport.connect(hostName, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}
}