今天对Java发送邮件的稍微搜索整理了下,记下来沉淀一下。
package com.datadriver.fosun.sendemail.action;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import com.datadriver.common.util.log.Logger;
public class SendMailUtil {
public static String send(String host, String mail, String password,
String from, List<String> to, String title, String content,
String[] fileNames) {
String result = "邮件发送成功";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
props.put("mail.smtp.auth", "true"); // 这样才能通过验证
MyAuthenticator myauth = new MyAuthenticator(mail, password);
Session session = Session.getDefaultInstance(props, myauth);
//session.setDebug(true);
for (int i = 0; i < to.size(); i++) {
// Set the to address
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
try {
message.setFrom(new InternetAddress(from));
} catch (AddressException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
}
// Set the subject
try {
message.setSubject(title);
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
result = "设置邮件主题错误";
}
// Set the content
try {
if (fileNames != null && fileNames.length > 0) {
BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
mdp.setContent(content, "text/html;charset=GB2312");// 给BodyPart对象设置内容和格式/编码方式
Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对
// 象(事实上可以存放多个)
mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
// 把mm作为消息对象的内容
MimeBodyPart filePart;
FileDataSource filedatasource;
// 逐个加入附件
for (int j = 0; j < fileNames.length; j++) {
filePart = new MimeBodyPart();
filedatasource = new FileDataSource(fileNames[j]);
filePart
.setDataHandler(new DataHandler(filedatasource));
try {
filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mm.addBodyPart(filePart);
}
message.setContent(mm);
} else {
message.setText(content);
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
result = "设置邮件内容错误";
}
try {
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to.get(i)));
} catch (AddressException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
result = "邮件地址解析失败";
} catch (MessagingException e) {
// TODO Auto-generated catch block
result = "邮件信息错误";
}
try {
message.saveChanges();
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
result = "邮件发送失败";
}
try {
Transport.send(message);
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Logger.debug(e);
result = "邮件发送失败";
}
}
return result;
}
}