java 讀取eml檔案附件方法(附件為base64轉碼)
阿新 • • 發佈:2019-01-26
/**
* 建立amr檔案
* @param file
* @param strEmailEml
* @throws Exception
*/
private static void createAmrFile(String strEmailEml) throws Exception {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
InputStream inMsg = new ByteArrayInputStream(strEmailEml.getBytes("UTF-8"));
Message msg = new MimeMessage(session, inMsg);
Object o = msg.getContent();
if (o instanceof Multipart) {
Multipart multipart = (Multipart) o;
reMultipart(multipart);
} else if (o instanceof Part) {
Part part = (Part) o;
rePart(part);
} else {
System.out.println("型別" + msg.getContentType());
System.out.println("內容" + msg.getContent());
}
}
/**
* @param part 解析內容
* @throws Exception
*/
private static void rePart(Part part) throws Exception {
if (part.getDisposition() != null) {
String strFileNmae = part.getFileName();
if(!StringUtils.isEmpty(strFileNmae))
{ // MimeUtility.decodeText解決附件名亂碼問題
strFileNmae=MimeUtility.decodeText(strFileNmae);
System.out.println("附件名稱: "+ strFileNmae);
InputStream in = part.getInputStream();// 開啟附件的輸入流
// 讀取附件位元組並存儲到檔案中
FileOutputStream out = new FileOutputStream(file.getPath()+"/"+strFileNmae);
int data;
while ((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
}
System.out.println("內容型別: "+ MimeUtility.decodeText(part.getContentType()));
System.out.println("附件內容:" + part.getContent());
} else {
if (part.getContentType().startsWith("text/plain")) {
System.out.println("文字內容:" + part.getContent());
} else {
// System.out.println("HTML內容:" + part.getContent());
}
}
}
/**
* @param multipart 接卸包裹(含所有郵件內容(包裹+正文+附件))
* @throws Exception
*/
private static void reMultipart(Multipart multipart) throws Exception {
// 依次處理各個部分
for (int j = 0, n = multipart.getCount(); j < n; j++) {
Part part = multipart.getBodyPart(j);// 解包, 取出 MultiPart的各個部分,
// 也可能是另一個小包裹(MultipPart)
// 判斷此包裹內容是不是一個小包裹, 一般這一部分是 正文 Content-Type: multipart/alternative
if (part.getContent() instanceof Multipart) {
Multipart p = (Multipart) part.getContent();// 轉成小包裹
// 遞迴迭代
reMultipart(p);
} else {
rePart(part);
}
}
}
* 建立amr檔案
* @param file
* @param strEmailEml
* @throws Exception
*/
private static void createAmrFile(String strEmailEml) throws Exception {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
InputStream inMsg = new ByteArrayInputStream(strEmailEml.getBytes("UTF-8"));
Message msg = new MimeMessage(session, inMsg);
Object o = msg.getContent();
if (o instanceof Multipart) {
Multipart multipart = (Multipart) o;
reMultipart(multipart);
} else if (o instanceof Part) {
Part part = (Part) o;
rePart(part);
} else {
System.out.println("型別" + msg.getContentType());
System.out.println("內容" + msg.getContent());
}
}
/**
* @param part 解析內容
* @throws Exception
*/
private static void rePart(Part part) throws Exception {
if (part.getDisposition() != null) {
String strFileNmae = part.getFileName();
if(!StringUtils.isEmpty(strFileNmae))
{ // MimeUtility.decodeText解決附件名亂碼問題
strFileNmae=MimeUtility.decodeText(strFileNmae);
System.out.println("附件名稱: "+ strFileNmae);
InputStream in = part.getInputStream();// 開啟附件的輸入流
// 讀取附件位元組並存儲到檔案中
FileOutputStream out = new FileOutputStream(file.getPath()+"/"+strFileNmae);
int data;
while ((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
}
System.out.println("內容型別: "+ MimeUtility.decodeText(part.getContentType()));
System.out.println("附件內容:" + part.getContent());
} else {
if (part.getContentType().startsWith("text/plain")) {
System.out.println("文字內容:" + part.getContent());
} else {
// System.out.println("HTML內容:" + part.getContent());
}
}
}
/**
* @param multipart 接卸包裹(含所有郵件內容(包裹+正文+附件))
* @throws Exception
*/
private static void reMultipart(Multipart multipart) throws Exception {
// 依次處理各個部分
for (int j = 0, n = multipart.getCount(); j < n; j++) {
Part part = multipart.getBodyPart(j);// 解包, 取出 MultiPart的各個部分,
// 也可能是另一個小包裹(MultipPart)
// 判斷此包裹內容是不是一個小包裹, 一般這一部分是 正文 Content-Type: multipart/alternative
if (part.getContent() instanceof Multipart) {
Multipart p = (Multipart) part.getContent();// 轉成小包裹
// 遞迴迭代
reMultipart(p);
} else {
rePart(part);
}
}
}