1.环境
JDK6
Java MAIL
2. 代码
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.ParseException;
import org.apache.log4j.Logger;
public class MimeDecoder {
private Logger logger = Logger.getLogger(MimeDecoder.class);
private MimeMessage message;
private String encoding = "UTF-8"; //MIME正文编码
private String txtContent; //纯文本正文
private String htmlContent; //超文本正文
private Map<String, MimeAttachment> attachmentMap = new HashMap<String, MimeAttachment>(); //附件, 内嵌文件
private Map<String, String> headers = new LinkedHashMap<String, String>();
/**
* 构造MIME解码器
* @param text String MIME原始报文
* @param encoding String MIME正文默认编码, 仅当MIME正文没有指定编码时使用
* @throws MessagingException
*/
public MimeDecoder(String text, String encoding)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(text.getBytes()));
this.encoding = encoding;
}
public MimeDecoder(String text)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(text.getBytes()));
}
public MimeDecoder(byte[] data, String encoding)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(data));
this.encoding = encoding;
}
public MimeDecoder(byte[] data)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(data));
}
public MimeDecoder(InputStream is, String encoding)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), is);
this.encoding = encoding;
}
public MimeDecoder(InputStream is)throws MessagingException{
message = new MimeMessage(Session.getDefaultInstance(new Properties()), is);
}
public String getHeader(String name){
return headers.get(name);
}
public Set<String> headNameSet(){
return headers.keySet();
}
public String getTxtContent(){
return txtContent;
}
public byte[] getTxtData()throws UnsupportedEncodingException{
if(txtContent != null){
return txtContent.getBytes(encoding);
}
return null;
}
public String getHtmlContent(){
return htmlContent;
}
public byte[] getHtmlData()throws UnsupportedEncodingException{
if(htmlContent != null){
return htmlContent.getBytes(encoding);
}
return null;
}
public Iterator<Map.Entry<String, MimeAttachment>> attachmentIterator(){
return attachmentMap.entrySet().iterator();
}
public Collection<MimeAttachment> attachmentCollections(){
return attachmentMap.values();
}
private void updateEncoding(String contentType)throws ParseException{
if(contentType != null){
ContentType ct = new ContentType(contentType);
String e = ct.getParameter("charset");
if(e != null){
this.encoding = e;
}
}
}
public void decode(){
try {
decodeHeaders();
Object messageBody = message.getContent();
if(message.isMimeType("text/html")){//MIME超文本正文
updateEncoding(message.getContentType());
htmlContent = (String)messageBody;
}else if(message.isMimeType("text/*")){//MIME纯文本正文
updateEncoding(message.getContentType());
txtContent = (String)messageBody;
}else if(message.isMimeType("multipart/*")){ //带附件的MIME处理
Multipart multipart = (Multipart)messageBody;
int count = multipart.getCount();
for (int i=0, n=count; i<n; i++) {
MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(i);
decodeBodyPart(part);
}
}
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
protected void decodeHeaders()throws MessagingException{
Enumeration<?> headEnumeration = message.getAllHeaders();
while(headEnumeration.hasMoreElements()){
Header header = (Header)headEnumeration.nextElement();
headers.put(header.getName(), header.getValue());
}
}
protected void decodeBodyPart(MimeBodyPart part)throws MessagingException, IOException{
String disposition = part.getDisposition();
if (disposition != null) {//如果是附件
MimeAttachment mimeAttachment= new MimeAttachment(part);
attachmentMap.put(mimeAttachment.getName(), mimeAttachment);
}else {
if(part.isMimeType("text/html")){//MIME超文本正文
updateEncoding(part.getContentType());
htmlContent = (String)part.getContent();
}else if(part.isMimeType("text/*")){//MIME纯文本正文
updateEncoding(part.getContentType());
txtContent = (String)part.getContent();
}else if(part.isMimeType("multipart/*")){//嵌套multipart
Multipart multipart = (Multipart)part.getContent();
int count = multipart.getCount();
for (int i=0, n=count; i<n; i++) {
MimeBodyPart p = (MimeBodyPart)multipart.getBodyPart(i);
decodeBodyPart(p);
}
}else{
logger.info("不支持的MIME正文媒体类型: " + part.getContentType());
}
}
}
}
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeUtility;
public class MimeAttachment {
protected byte[] data;
protected String name;
protected boolean isInline = false;
protected ContentType ct;
public MimeAttachment(MimeBodyPart part)throws IOException, MessagingException{
String disposition = part.getDisposition();
if (disposition != null) {//如果是附件
ct = new ContentType(part.getContentType());
if(disposition.equals(Part.ATTACHMENT)){
this.name = nameDecode(part.getFileName());
}else if(disposition.equals(Part.INLINE)){
this.name = nameDecode(part.getContentID());
this.isInline = true;
}
parse(part);
}
}
protected void parse(MimeBodyPart part)throws IOException, MessagingException{
InputStream is = part.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int data = -1;
while((data = is.read()) != -1){
baos.write(data);
}
this.data = baos.toByteArray();
}
protected String nameDecode(String s)throws UnsupportedEncodingException{
return MimeUtility.decodeText(s);
}
public byte[] getData(){
return data;
}
public String getName(){
return name;
}
public File saveTo(String dir)throws IOException{
if(data != null){
File f = new File(new File(dir), name);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
bos.write(data);
bos.close();
return f;
}
return null;
}
public boolean isInline(){
return isInline;
}
public ContentType getContentType(){
return ct;
}
}
3. 测试
public static void main(String args[])throws Exception{
MimeDecoder decoder = new MimeDecoder(new FileInputStream("d:/test/开源项目.eml"));
decoder.decode();
if(decoder.getTxtContent() != null){
System.out.println("MIME纯文本正文: " + decoder.getTxtContent());
}
if(decoder.getHtmlContent() != null){
System.out.println("MIME超文本正文: " + decoder.getHtmlContent());
}
//保存附件
String dir = "d:/test";
Collection<MimeAttachment> attachments = decoder.attachmentCollections();
for(MimeAttachment attachment : attachments){
attachment.saveTo(dir);
}
}