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 形式を保持したまま EML を MSG に変換

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