加载、查看和解析 MSG 文件

本主题解释如何加载 Microsoft Outlook 消息文件 (*.msg)。 MapiMessage 类用于加载 MSG 文件,并提供多种静态加载函数以应对不同场景。以下代码片段展示如何从文件或流中加载 MSG 文件。

加载 MSG 文件

以下代码片段展示如何加载 MSG 文件。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");

// Get subject
System.out.println("Subject:" + msg.getSubject());

// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());

// Get body
System.out.println("Body" + msg.getBody());

// Get recipients information
System.out.println("Recipient: " + msg.getRecipients());

// Get attachments
for (MapiAttachment att : msg.getAttachments())
{
    System.out.println("Attachment Name: " + att.getFileName());
    System.out.println("Attachment Display Name: " + att.getDisplayName());
}

以下代码示例展示如何使用 MailMessage 加载 MSG 格式的消息。

MailMessage eml = MailMessage.load("message.msg");

需要注意的是,生成的消息会转换为 EML 格式,包括嵌入的消息附件。如果想保留原始消息的一些特定 MSG 格式属性,请不要使用此加载方法。

要保留嵌入消息附件的原始格式,请使用 MsgLoadOptions.PreserveEmbeddedMessageFormat 属性。

MsgLoadOptions msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.setPreserveEmbeddedMessageFormat(true);
MailMessage msg = MailMessage.load(stream, msgLoadOptions);

从流加载

以下代码片段展示如何从流中加载文件。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of MapiMessage from file
try (FileInputStream stream = new FileInputStream(dataDir + "message.msg"))
{
    // Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.load(stream);

    // Get subject
    System.out.println("Subject:" + msg.getSubject());

    // Get from address
    System.out.println("From:" + msg.getSenderEmailAddress());

    // Get body
    System.out.println("Body" + msg.getBody());

}

在不读取附件内容的情况下加载 MSG 文件

默认情况下, MapiMessage.load 将每个附件的内容与邮件一起读取到内存中。对于带有大附件的邮件,打开邮件所需的内存将随附件大小增加,即使您只需要标题、收件人或附件名称。

使用延迟加载时,附件内容保留在源中,仅在访问时读取。通过以下属性启用此功能: MsgLoadOptions 类:

  • setDeferAttachmentContent(boolean) - 当 true,附件内容在需要时才从源读取,而不是随邮件一起加载。默认值为 false.
  • setDeferralThreshold(int) - 延迟加载适用的最小内容大小(字节)。较小的附件始终随邮件一起加载。默认值为 4096。

延迟加载适用于二进制附件内容。以 OLE 对象或嵌入消息形式存储的附件会在加载消息时提取。

以下代码片段在不读取附件的情况下检查消息:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

// The file stays open until the message is disposed
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
    System.out.println("Subject: " + msg.getSubject());
    System.out.println("Recipients: " + msg.getRecipients().size());

    for (MapiAttachment attachment : msg.getAttachments()) {
        // Names and sizes are available without reading the content
        System.out.println("  " + attachment.getLongFileName() + " : " + attachment.getContentLength() + " bytes");
    }
}

以下代码片段仅提取消息的 PDF 附件。其他附件的内容永不从文件中读取:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
    for (MapiAttachment attachment : msg.getAttachments()) {
        if (!attachment.getLongFileName().endsWith(".pdf")) {
            continue;
        }

        try (InputStream source = attachment.openRead()) {
            Files.copy(source, Paths.get("out", attachment.getLongFileName()), StandardCopyOption.REPLACE_EXISTING);
        }
    }
}

以下代码片段从调用者拥有的流中加载消息:

MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);

// Keep the stream open while the message is in use
try (InputStream source = Files.newInputStream(Paths.get("large.msg"))) {
    MapiMessage msg = MapiMessage.load(source, options);
    byte[] data = msg.getAttachments().get(0).getBinaryData();
    System.out.println("Read " + data.length + " bytes");
}

要将附件内容作为流读取并在不将其加载到内存的情况下添加大附件,请参阅 处理消息附件.

将 EML 转换为 MSG 并保留嵌入的 EML 格式

EML 文件可以加载到 MapiMessage 类,通过实例化一个 MailMessage 对象并将其传递给 MapiMessage.fromMailMessage 方法。如果 EML 文件包含嵌入的 EML 文件,请使用 MapiConversionOptions.setPreserveEmbeddedMessageFormat 以保留嵌入 EML 文件的格式。下面的代码片段展示如何将 EML 文件加载到 MapiMessage 同时保留嵌入的 EML 文件的格式。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "email/";

MailMessage eml = MailMessage.load(dataDir + "sample.eml", new EmlLoadOptions());

MapiConversionOptions options = MapiConversionOptions.getUnicodeFormat();

//Preserve Embedded Message Format
options.setPreserveEmbeddedMessageFormat(true);

//Convert EML to MSG with Options
MapiMessage msg = MapiMessage.fromMailMessage(eml, options);