Différencier les pièces jointes en ligne et normales

Contents
[ ]

Cet article explique comment différencier les pièces jointes en ligne des pièces jointes normales en utilisant MapiMessage. Lors de la décision, le type de corps de MapiMessage est pris en compte comme suit :

  • Corps texte brut : Les messages électroniques avec un corps en texte brut n’ont pas besoin d’être vérifiés, car toutes les pièces jointes dans ces messages sont toujours des pièces jointes normales.
  • Corps HTML : Pour les messages avec un corps HTML, la pièce jointe doit non seulement contenir la propriété PR_ATTACH_FLAGS (0x37140003), mais sa valeur doit être égale à 0x00000004 pour les pièces jointes en ligne. Si cette condition est remplie, cela dépend ensuite des balises PR_ATTACH_CONTENT_LOCATION et PR_ATTACH_CONTENT_ID pour déterminer la nature de la pièce jointe. Cependant, en l’absence de la balise MAPI PR_ATTACH_FLAGS, la pièce jointe est vérifiée via la propriété PR_ATTACH_DISPOSITION (0x3716001F ou 0x3716001E) pour déterminer le type de pièce jointe.
  • Corps RTF : Si le corps est au format RTF, alors toutes les pièces jointes OLE sont des pièces jointes en ligne. La valeur de PR_ATTACH_METHOD pour toutes les pièces jointes OLE est égale à 0x00000006.

L’exemple de code suivant montre comment différencier programmétiquement les pièces jointes en ligne des pièces jointes normales. La fonction isInlineAttachment prend une pièce jointe et un MapiMessage en paramètres d’entrée et renvoie true si la pièce jointe est en ligne.

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

Ce fragment de code utilise la fonction isInlineAttachment() pour évaluer les pièces jointes.

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