インライン添付ファイルと通常添付ファイルを区別する

Contents
[ ]

この記事では、インライン添付と通常添付を区別する方法について説明します。 MapiMessage。決定時に、本文のタイプは MapiMessage は以下のように考慮されます:

  • プレーンテキスト本文: プレーンテキスト本文のメールメッセージはチェックする必要がありません。この種のメッセージではすべての添付が常に通常添付となります。
  • HTML 本文: HTML 本文のメッセージの場合、添付ファイルは PR_ATTACH_FLAGS (0x37140003) プロパティを含むだけでなく、インライン添付の場合はその値が 0x00000004 である必要があります。この条件が満たされると、さらに PR_ATTACH_CONTENT_LOCATION と PR_ATTACH_CONTENT_ID タグに基づいて添付の性質が決まります。ただし、PR_ATTACH_FLAGS MAPI タグが存在しない場合は、PR_ATTACH_DISPOSITION (0x3716001F または 0x3716001E) プロパティで添付タイプが判定されます。
  • RTF 本文: 本文が RTF の場合、すべての OLE 添付はインライン添付となります。すべての OLE 添付の PR_ATTACH_METHOD の値は 0x00000006 です。

以下のコードサンプルは、インライン添付と通常添付をプログラムで区別する方法を示しています。isInlineAttachment 関数は添付ファイルと MapiMessage を入力パラメータとして受け取り、添付がインラインであれば true を返します。

public static boolean isInlineAttachment(MapiAttachment att, MapiMessage msg) {
    if (msg.getBodyType() == BodyContentType.PlainText)
        // ignore indications for plain text messages
        return false;
    else if (msg.getBodyType() == BodyContentType.Html) {
        // check the PidTagAttachFlags property
        Long attachFlagsValue = att.getPropertyLong(MapiPropertyTag.PR_ATTACH_FLAGS);
        if (attachFlagsValue != null && (attachFlagsValue > 3 || attachFlagsValue < 1)) {
            // check PidTagAttachContentId property
            String contentId = att.getProperties().containsKey(MapiPropertyTag.PR_ATTACH_CONTENT_ID)
                    ? att.getPropertyString(MapiPropertyTag.PR_ATTACH_CONTENT_ID)
                    : att.getPropertyString(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W);
            if (contentId != null && !contentId.isEmpty() && msg.getBodyHtml().contains("cid:" + contentId)) {
                return true;
            }
            // check PidTagAttachContentLocation property
            String contentLocation = att.getProperties().containsKey(MapiPropertyTag.PR_ATTACH_CONTENT_LOCATION)
                    ? att.getPropertyString(MapiPropertyTag.PR_ATTACH_CONTENT_LOCATION)
                    : att.getPropertyString(MapiPropertyTag.PR_ATTACH_CONTENT_LOCATION_W);
            if (contentLocation != null && !contentLocation.isEmpty() && msg.getBodyHtml().contains(contentLocation)) {
                return true;
            }
        }
        return "inline".equals(att.getPropertyString(0x3716001F)) || "inline".equals(att.getPropertyString(0x3716001E));
    } else if (msg.getBodyType() == BodyContentType.Rtf) {
        // If the body is RTF, then all OLE attachments are inline attachments.
        // OLE attachments have 0x00000006 for the value of the PidTagAttachMethod property
        Long attachMethod = att.getPropertyLong(MapiPropertyTag.PR_ATTACH_METHOD);
        return attachMethod != null && attachMethod == 0x00000006;
    } else {
        throw new ArgumentOutOfRangeException();
    }
}

このコードスニペットは isInlineAttachment() 関数を使用して添付ファイルを評価します。

Java

String fileName = ("Sample.msg");
MapiMessage message = MapiMessage.fromFile(fileName);
MapiAttachmentCollection attachments = message.getAttachments();
for (int i = 0; i < attachments.size(); i++) {
    MapiAttachment attachment = (MapiAttachment) attachments.get(i);
    if (isInlineAttachment(attachment, message)) {
        System.out.println(attachment.getLongFileName() + " is inline attachment");
    } else {
        System.out.println(attachment.getLongFileName() + " is regular attachment");
    }
}