[color=red][/color]
[code="java"]package byd.core;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sun.misc.BASE64Encoder;
/**
@author Kou Hongtao
*/
public class Email {
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
/**
private List alternativeList;
private String mixedBoundary;
private String mixedBoundaryNextPart;
/**
private static enum TextType {
PLAIN("plain"), HTML("html");
private String v;
private TextType(String v) {
this.v = v;
}
public String getValue() {
return this.v;
}
}
static {
// MIME Media Types
contentTypeMap = new HashMap();
contentTypeMap.put("xls", "application/vnd.ms-excel");
contentTypeMap.put("xlsx", "application/vnd.ms-excel");
contentTypeMap.put("xlsm", "application/vnd.ms-excel");
contentTypeMap.put("xlsb", "application/vnd.ms-excel");
contentTypeMap.put("doc", "application/msword");
contentTypeMap.put("dot", "application/msword");
contentTypeMap.put("docx", "application/msword");
contentTypeMap.put("docm", "application/msword");
contentTypeMap.put("dotm", "application/msword");
}
/**
/**
/**
@return 返回文件对应的MIME类型。
*/
private String getPartContentType(String fileName) {
String ret = null;
if (null != fileName) {
int flag = fileName.lastIndexOf(".");
if (0 <= flag && flag < fileName.length() - 1) {
fileName = fileName.substring(flag + 1);
}
ret = contentTypeMap.get(fileName);
}
if (null == ret) {
ret = defaultAttachmentContentType;
}
return ret;
}
/**
/**
/**
/**
@return 返回一个所有单元合并后的字符串。
*/
private String getAllParts() {
StringBuilder sbd = new StringBuilder(LINE_END);
sbd.append(mixedBoundaryNextPart);
sbd.append(LINE_END);
sbd.append("Content-Type: ");
sbd.append("multipart/alternative");
sbd.append(";");
sbd.append("boundary=\"");
sbd.append(boundary).append("\""); // 邮件类型设置
sbd.append(LINE_END);
sbd.append(LINE_END);
sbd.append(LINE_END);
addPartsToString(alternativeList, sbd, getBoundaryNextPart());
sbd.append(getBoundaryNextPart()).append("--");
sbd.append(LINE_END);
addPartsToString(partSet, sbd, mixedBoundaryNextPart);
sbd.append(LINE_END);
sbd.append(mixedBoundaryNextPart).append("--");
sbd.append(LINE_END);
// sbd.append(boundaryNextPart).
// append(LINE_END);
alternativeList.clear();
partSet.clear();
return sbd.toString();
}
private void addPartsToString(List list, StringBuilder sbd,
String nextPartString) {
int partCount = list.size();
for (int i = 0; i < partCount; i++) {
Email attachment = list.get(i);
String attachmentContent = attachment.getContent();
if (null != attachmentContent && 0 < attachmentContent.length()) {
sbd.append(nextPartString).append(LINE_END);
sbd.append("Content-Type: ");
sbd.append(attachment.getContentType());
sbd.append(LINE_END);
sbd.append("Content-Transfer-Encoding: ");
sbd.append(attachment.getContentTransferEncoding());
sbd.append(LINE_END);
String cd = attachment.getContentDisposition();
if (null != cd) {
sbd.append("Content-Disposition: ");
sbd.append(cd);
sbd.append(LINE_END);
}
sbd.append(LINE_END);
sbd.append(attachmentContent);
sbd.append(LINE_END);
}
}
}
/**
private String listToMailString(List mailAddressList) {
StringBuilder sbd = new StringBuilder();
if (null != mailAddressList) {
int listSize = mailAddressList.size();
for (int i = 0; i < listSize; i++) {
if (0 != i) {
sbd.append(";");
}
sbd.append("<").append(mailAddressList.get(i)).append(">");
}
}
return sbd.toString();
}
private List getrecipient() {
List list = new ArrayList();
list.addAll(to);
list.addAll(cc);
list.addAll(bcc);
return list;
}
/**
/**
private void addContent(String text, TextType type) {
if (null != text) {
MailPart part = new MailPart();
part.setContent(toBase64(text));
part.setContentType("text/" + type.getValue() + ";charset=\""
+ charset + "\"");
alternativeList.add(part);
}
}
/**
public void addTo(String mailAddress) {
this.to.add(mailAddress);
}
public void addCc(String mailAddress) {
this.cc.add(mailAddress);
}
public void addBcc(String mailAddress) {
this.bcc.add(mailAddress);
}
/**
/**
文件编码格式
*/
public void addAttachment(String fileName, InputStream attachmentStream,
String charset) {
try {
byte[] bs = null;
if (null != attachmentStream) {
int buffSize = 1024;
byte[] buff = new byte[buffSize];
byte[] temp;
bs = new byte[0];
int readTotal = 0;
while (-1 != (readTotal = attachmentStream.read(buff))) {
temp = new byte[bs.length];
System.arraycopy(bs, 0, temp, 0, bs.length);
bs = new byte[temp.length + readTotal];
System.arraycopy(temp, 0, bs, 0, temp.length);
System.arraycopy(buff, 0, bs, temp.length, readTotal);
}
}
if (null != bs) {
MailPart attachmentPart = new MailPart();
charset = null != charset ? charset : Charset.defaultCharset()
.name();
String contentType = getPartContentType(fileName)
+ ";name=\"=?" + charset + "?B?" + toBase64(fileName)
+ "?=\"";
attachmentPart.setCharset(charset);
attachmentPart.setContentType(contentType);
attachmentPart.setContentDisposition("attachment;filename=\"=?"
+ charset + "?B?" + toBase64(fileName) + "?=\"");
attachmentPart.setContent(toBase64(bs));
partSet.add(attachmentPart);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != attachmentStream) {
try {
attachmentStream.close();
attachmentStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
Runtime.getRuntime().gc();
Runtime.getRuntime().runFinalization();
}
}
/**
@return 邮件服务器反回的信息
*/
public String send() {
// 对象申明
// 当邮件发送完毕后,以下三个对象(Socket、
// PrintWriter,
// BufferedReader)需要关闭。
Socket socket = null;
PrintWriter pw = null;
BufferedReader br = null;
try {
socket = new Socket(host, 25);
pw = new PrintWriter(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
StringBuilder infoBuilder = new StringBuilder(
"\nServer info: \n------------\n");
// 与服务器建立连接
pw.write("HELO ".concat(host).concat(LINE_END)); // 连接到邮件服务
if (!readResponse(pw, br, infoBuilder, "220"))
return infoBuilder.toString();
pw.write("AUTH LOGIN".concat(LINE_END)); // 登录
if (!readResponse(pw, br, infoBuilder, "250"))
return infoBuilder.toString();
pw.write(toBase64(user).concat(LINE_END)); // 输入用户名
if (!readResponse(pw, br, infoBuilder, "334"))
return infoBuilder.toString();
pw.write(toBase64(password).concat(LINE_END)); // 输入密码
if (!readResponse(pw, br, infoBuilder, "334"))
return infoBuilder.toString();
pw.write("MAIL FROM:<" + from + ">" + LINE_END); // 发件人邮箱地址
if (!readResponse(pw, br, infoBuilder, "235"))
return infoBuilder.toString();
List<String> recipientList = getrecipient();
// 收件邮箱地址
for (int i = 0; i < recipientList.size(); i++) {
pw.write("RCPT TO:<" + recipientList.get(i) + ">" + LINE_END);
if (!readResponse(pw, br, infoBuilder, "250"))
return infoBuilder.toString();
}
// System.out.println(
// getAllSendAddress());
pw.write("DATA" + LINE_END); // 开始输入邮件
if (!readResponse(pw, br, infoBuilder, "250"))
return infoBuilder.toString();
flush(pw);
// 设置邮件头信息
StringBuffer sbf = new StringBuffer("From: <" + from + ">"
+ LINE_END); // 发件人
sbf.append("To: " + listToMailString(to) + LINE_END);// 收件人
sbf.append("Cc: " + listToMailString(cc) + LINE_END);// 收件人
sbf.append("Bcc: " + listToMailString(bcc) + LINE_END);// 收件人
sbf.append("Subject: " + subject + LINE_END);// 邮件主题
SimpleDateFormat sdf = new SimpleDateFormat(simpleDatePattern);
sbf.append("Date: ").append(sdf.format(new Date()));
sbf.append(LINE_END); // 发送时间
sbf.append("Content-Type: ");
sbf.append(contentType);
sbf.append(";");
sbf.append("boundary=\"");
sbf.append(mixedBoundary).append("\""); // 邮件类型设置
sbf.append(LINE_END);
// sbf.append(
// "This is a multi-part message in MIME format."
// );
// sbf.append(LINE_END);
// 添加邮件正文单元
addContent();
// 合并所有单元,正文和附件。
sbf.append(getAllParts());
// System.out.println(
// "///////////\n" +
// sbf.toString() +
// "///////////////\n");
// 发送
sbf.append(LINE_END).append(".").append(LINE_END);
pw.write(sbf.toString());
readResponse(pw, br, infoBuilder, "354");
flush(pw);
// QUIT退出
pw.write("QUIT" + LINE_END);
if (!readResponse(pw, br, infoBuilder, "250"))
return infoBuilder.toString();
flush(pw);
return infoBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
return "Exception:>" + e.getMessage();
} finally {
// 释放资源
try {
if (null != socket)
socket.close();
if (null != pw)
pw.close();
if (null != br)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
// this.to.clear();
// this.cc.clear();
// this.bcc.clear();
this.partSet.clear();
}
}
/**
/**
public String getBoundaryNextPart() {
return boundaryNextPart;
}
public void setBoundaryNextPart(String boundaryNextPart) {
this.boundaryNextPart = boundaryNextPart;
}
public String getDefaultAttachmentContentType() {
return defaultAttachmentContentType;
}
public void setDefaultAttachmentContentType(
String defaultAttachmentContentType) {
this.defaultAttachmentContentType = defaultAttachmentContentType;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public List getTo() {
return to;
}
public void setTo(List to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getBoundary() {
return boundary;
}
public void setBoundary(String boundary) {
this.boundary = boundary;
}
public String getContentTransferEncoding() {
return contentTransferEncoding;
}
public void setContentTransferEncoding(String contentTransferEncoding) {
this.contentTransferEncoding = contentTransferEncoding;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getContentDisposition() {
return contentDisposition;
}
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
public String getSimpleDatePattern() {
return simpleDatePattern;
}
public void setSimpleDatePattern(String simpleDatePattern) {
this.simpleDatePattern = simpleDatePattern;
}
public String getContent() {
return content;
}
/**
public boolean isAllowReadSocketInfo() {
return isAllowReadSocketInfo;
}
public void setAllowReadSocketInfo(boolean isAllowReadSocketInfo) {
this.isAllowReadSocketInfo = isAllowReadSocketInfo;
}
/**
@param args
*/
public static void main(String[] args) {
// 应用示例:线程化发送邮件
new Thread() {
@Override
public void run() {
System.out.println("SENDER-" + this.getId() + ":/>"
+ "开始发送邮件...");
// 创建邮件对象
Email mail = new Email();
mail.setHost("smtp.byd.com"); // 邮件服务器地址
mail.setFrom("kou.hongtao@byd.com"); // 发件人邮箱
mail.addTo("kou.hongtao@byd.com"); // 收件人邮箱
mail.addCc("zhao.xiaowei@byd.com");
mail.addBcc("");
mail.setSubject("研发物料邮件发送测试"); // 邮件主题
mail.setUser("kou.hongtao"); // 用户名
mail.setPassword("byd@user"); // 密码
// 邮件正文
mail.addHtmlContent("<h3>研发物料邮件测试,请不要回复!</h3>");
// mail.addAttachment("add.js"); // 添加附件
System.out.println(mail.send()); // 发送
System.out.println("SENDER-" + this.getId() + ":/>"
+ "邮件已发送完毕!");
}
}.start();
}
}
[/code]