import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.activation.DataHandler;
import javax.mail.Authenticator;
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 javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;
public class MailUtil {
String from = ""; // 发件人
String host = ""; // SMTP地址
String subject = ""; // 主题
String body = ""; // 邮件内容
String user = ""; // 用户名
String passwd = ""; // 密码
String fileName = ""; // 附件名
byte[] fileContent = null;// 附件内容
private String[] receiverAddress; // 收件人
public MailUtil(String[] receiverAddress, String from, String smtpServer,
String subject, String user, String passwd) {
// 初始化收件人、发件人、主题等
this.receiverAddress = receiverAddress;
this.from = from;
this.host = smtpServer;
this.subject = subject;
this.user = user;
this.passwd = passwd;
}
// 该方法用于添加邮件内容
public void setBody(String mailbody) {
this.body = mailbody;
}
// 该方法用于添加附件
public void setFile(String fileName, byte[] fileContent) {
this.fileName = fileName;
this.fileContent = fileContent;
}
public String[] getReceiverAddress() {
return receiverAddress;
}
public void setReceiverAddress(String[] receiverAddress) {
this.receiverAddress = receiverAddress;
}
// 发送信件的方法
public boolean sendMail() {
// 创建Properties对象
Properties props = System.getProperties();
// 创建信件服务器
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", user);
props.put("mail.smtp.auth", "true");
// 邮件服务器登录验证
Authenticator auth = new SMTPAuthenticator(user, passwd);
// 初始化Session实例
Session session = Session.getInstance(props, auth);
try {
// 创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = null;
List addressList = new ArrayList();
// 过滤不合法的收信地址
for (int i = 0; i < receiverAddress.length; i++) {
if (receiverAddress[i] != null
&& !receiverAddress[i].equals("")
&& this.isEmail(receiverAddress[i])) {
addressList.add(receiverAddress[i]);
}
}
if (addressList.size() > 0) {
address = new InternetAddress[addressList.size()];
for (int j = 0; j < addressList.size(); j++) {
address[j] = new InternetAddress((String) addressList
.get(j));
}
// 添加收件人
msg.setRecipients(Message.RecipientType.TO, address);
// 添加主题
msg.setSubject(subject);
// 加入邮件内容
BodyPart bp = new MimeBodyPart();
bp.setContent("" + body, "text/html;charset=GB2312");
Multipart mp = new MimeMultipart();
mp.addBodyPart(bp);
// 添加附件
if (fileContent != null) {
setFileByByte(mp, fileContent, fileName);
}
// Multipart加入到信件
msg.setContent(mp);
// 设置信件头的发送日期
msg.setSentDate(new Date());
// 发送信件
Transport.send(msg);
}
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
return false;
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public boolean isEmail(String inputChar) {
Pattern pattern = Pattern.compile(".+@.+\\..+");
Matcher matcher = pattern.matcher(inputChar);
return matcher.matches();
}
public void setFileByByte(Multipart mp, byte[] fileInfo, String fileName)
throws MessagingException, UnsupportedEncodingException {
MimeBodyPart mbp = new MimeBodyPart();
DataHandler dh = new DataHandler(new ByteArrayDataSource(fileInfo,
"application/octet-stream"));
mbp.setDataHandler(dh);
mbp.setFileName(MimeUtility.encodeText(fileName, "GBK", "B"));
mp.addBodyPart(mbp);
}
}
public class SMTPAuthenticator extends javax.mail.Authenticator {
private String username;
private String password;
public SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}