strEmailEml爲eml文件,通過postMethod.getResponseBodyAsString()獲取字符串
/**
* 創建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);
}
}
}