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);
}
}
}