在 Aspose.Email 中读取邮件中的嵌入式附件
Contents
[
Hide
]
Aspose.Email - 从邮件中读取嵌入的电子邮件附件
有时,我们会收到包含其他电子邮件作为附件的邮件。这些嵌入的电子邮件是完整的消息,拥有自己的收件人列表、主题、正文,甚至附件。每条消息还可能再次包含嵌入的消息。使用 Aspose.Email Java,开发者可以将每个嵌入的邮件作为单独的消息进行访问。此示例演示了如何使用递归功能。
Java
MailMessage message = MailMessage.load(dataDir + \"embedded.msg\", MessageFormat.getMsg());
for (int i = 0; i < message.getAttachments().size(); i++)
{
Attachment att = (Attachment) message.getAttachments().get_Item(i);
System.out.println("Attachment Name: " + att.getName());
// Get the name of attachment. If msg subject contains characters like :, /, \ etc., replace with space
// because windows cannot save files with these characters
// also save first 50 characters as file name to avoid long file names
String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");
if (attFileName.length() > 50)
{
attFileName = attFileName.substring(0, 50);
}
String attExt = (att.getName().substring(att.getName().lastIndexOf("."), att.getName().lastIndexOf(".") + 4));
// Save the attachment to disk
att.save(dataPath + attFileName + attExt);
// Check if it is an orphaned text attachment file (ATT00001.txt....) and of type eml
if ((attExt.equals(".eml")) || (att.getContentType().getMediaType().equals("text/plain") && att.getName().contains(".txt") == true && att.getName().contains("ATT") == true))
{
// Try to load this text file in MailMessage
MailMessage attMsg = MailMessage.load(dataPath + attFileName + attExt, MessageFormat.getEml());
// Call the function recursively to parse this message and attachments
ParseMessage(attMsg);
}
}下载运行代码
下载示例代码
欲了解更多细节,请访问 从邮件中读取嵌入的电子邮件附件.