Differenziare tra allegati inline e regolari

Contents
[ ]

Questo articolo spiega come differenziare gli allegati inline da quelli regolari usando MapiMessage. Quando si decide, il tipo di corpo di MapiMessage viene considerato come segue:

  • Corpo di testo semplice: I messaggi email con corpo di tipo testo semplice non necessitano di verifica, poiché tutti gli allegati in tali messaggi sono sempre allegati regolari.
  • Corpo HTML: Per i messaggi con corpo HTML, l’allegato non deve solo contenere la proprietà PR_ATTACH_FLAGS (0x37140003), ma il suo valore deve essere pari a 0x00000004 per gli allegati inline. Se questa condizione è soddisfatta, dipende ulteriormente dai tag PR_ATTACH_CONTENT_LOCATION e PR_ATTACH_CONTENT_ID per determinare la natura dell’allegato. Tuttavia, in assenza del tag MAPI PR_ATTACH_FLAGS, l’allegato è verificato per la proprietà PR_ATTACH_DISPOSITION (0x3716001F o 0x3716001E) per determinare il tipo di allegato.
  • Corpo RTF: Se il corpo è RTF, allora tutti gli allegati OLE sono allegati inline. Il valore di PR_ATTACH_METHOD per tutti gli allegati OLE è pari a 0x00000006.

Il seguente esempio di codice dimostra come differenziare programmaticamente tra allegati inline e regolari. La funzione isInlineAttachment accetta un allegato e MapiMessage come parametri di input e restituisce true se l’allegato è inline.

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

Questo frammento di codice utilizza la funzione isInlineAttachment() per valutare gli allegati.

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