我的问题如下:
我的代码设置用于读取特定帐户的电子邮件。那部分完美无缺。
问题在于解析电子邮件消息。分隔附件和电子邮件正文(包含内嵌图像)。
我的代码是这样的:
Void readMessages(Folder folder){
Message[] messages = folder.getMessages();
// loading of message objects.
for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
final Message currentMessage = messages[messageNumber];
logger.info("Handling the mail with subject " + currentMessage.getSubject());
logger.info("Content type for the current message is " + currentMessage.getContentType());
final String messageFileName = currentMessage.getFileName();
logger.info("File name for the message " + messageFileName + ". File name is blank "
+ StringUtils.isBlank(messageFileName));
Object messageContentObject = currentMessage.getContent();
if (messageContentObject instanceof Multipart) {
Multipart multipart = (Multipart) messageContentObject;
// downloading all attachments....
int attachmentCount = multipart.getCount();
logger.info("Number of attachments ");
for (int i = 0; i < attachmentCount; i++) {
Part part = (Part) multipart.getBodyPart(i);
downloadAttachment(part, folderPath.toString());
}
}
}
}
}
private void downloadAttachment(Part part, String folderPath) throws Exception {
String disPosition = part.getDisposition();
String fileName = part.getFileName();
String decodedText = null;
logger.info("Disposition type :: " + disPosition);
logger.info("Attached File Name :: " + fileName);
if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) {
logger.info("DisPosition is ATTACHMENT type.");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName != null && disPosition == null) {
logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment");
File file = new File(folderPath + File.separator + decodedText);
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
} else if (fileName == null && disPosition == null) {
logger.info("DisPosition is Null type but file name is null. It is email body.");
File file = new File(folderPath + File.separator + "mail.html");
file.getParentFile().mkdirs();
saveEmailAttachment(file, part);
}
}
protected int saveEmailAttachment(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = null;
InputStream is = null;
int ret = 0, count = 0;
try {
bos = new BufferedOutputStream(new FileOutputStream(saveFile));
part.writeTo(new FileOutputStream(saveFile));
} finally {
try {
if (bos != null) {
bos.close();
}
if (is != null) {
is.close();
}
} catch (IOException ioe) {
logger.error("Error while closing the stream.", ioe);
}
}
return count;
}
我得到的问题是,当我运行此代码时,我得到一个HTML文件,但内联图像被替换为错误图像的符号,表示没有源的图像。
请帮帮我。如果需要更多信息,请告诉我。
我还尝试通过更改以下内容将正文保存为.eml
文件:
File file = new File(folderPath + File.separator + "mail.html");
到
File file = new File(folderPath + File.separator + "mail.eml");
BUt我得到了相同的结果。
答案 0 :(得分:1)
内联图像的引用被cid:
URN替换为<img src="cid:SOMEID">
,因为电子邮件中没有文件名。 SOMEID指的是Multipart&#34;对象&#34;的内容ID。
为了使其工作,您必须将多部分附件存储到文件(例如,临时名称),并用真实文件名替换cid
URN。
答案 1 :(得分:1)
我在下面编写了代码,将电子邮件正文转换为pdf,包括内联图片。 在代码中我用下载图像路径替换了图像代码(例如:cid:image001.jpg@01D17AAA.1EA2A6A0)。我正在构建&#34; hashmap&#34;下载图像时的图像键和下载路径。
HTMLWorker htmlWorker = new HTMLWorker(document);
if(bodyStr!=null)
{
//find inline images
inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory);
if(inlineImages!=null)
{
for (Map.Entry<String, String> entry : inlineImages.entrySet()) {
//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
bodyStr=bodyStr.replaceAll("cid:"+entry.getKey() , entry.getValue());
}
}
htmlWorker.parse(new StringReader(bodyStr));
}
通过传递项目下载内嵌图像。
private HashMap<String,String> downloadInLineImage(Item item, String dynamicOutputDirectory)
throws Exception, ServiceLocalException {
//create output directory if not present
//bind the item to a new email message. if you do not bind, then the getHasAttachments() function will fail
EmailMessage mostRecentMatch = (EmailMessage)item;
String from = mostRecentMatch.getFrom().getAddress();
String user =StringUtils.substringBefore(from, "@");
AttachmentCollection collection=item.getAttachments();
HashMap<String,String> inlineFiles=new HashMap<String,String>();
if(collection.getCount()>0)
{
for (Attachment attachment : collection.getItems()) {
if(attachment.getIsInline())
{
FileAttachment currentFile = (FileAttachment) attachment;
String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName();
File file=new File(filePath);
FileOutputStream fio=new FileOutputStream(file);
currentFile.load(fio);
inlineFiles.put(currentFile.getContentId(), filePath);
fio.close();
}
}
}