Diferenciar entre anexos embutidos e regulares
Contents
[
Hide
]
Mensagens de e‑mail podem conter imagens embutidas assim como anexos. Usando MailMessage, os anexos embutidos podem ser extraídos do LinkedResourceCollection coleção, enquanto os anexos regulares podem ser acessados e extraídos com a AttachmentCollection coleção. Contudo, isso difere quando a mensagem é carregada usando MapiMessage, já que todas as imagens embutidas e anexos regulares são acessíveis ao usuário no mesmo MapiAttachmentCollection. Portanto, um método que possa diferenciar entre um anexo embutido e um anexo regular quando MapiMessage é usado se necessário.
Este artigo explica como diferenciar anexos embutidos de regulares usando MapiMessage. Ao decidir, o tipo de corpo de MapiMessage é considerado da seguinte forma:
- Corpo em texto simples: Mensagens de e‑mail com corpo em texto simples não precisam ser verificadas, pois todos os anexos nessas mensagens são sempre anexos regulares.
- Corpo HTML: Para mensagens com corpo HTML, o anexo deve não apenas conter a propriedade PR_ATTACH_FLAGS (0x37140003), mas seu valor deve ser igual a 0x00000004 para anexos embutidos. Se essa condição for atendida, então depende ainda das tags PR_ATTACH_CONTENT_LOCATION e PR_ATTACH_CONTENT_ID para determinar a natureza do anexo. Contudo, na ausência da tag MAPI PR_ATTACH_FLAGS, o anexo é verificado quanto à propriedade PR_ATTACH_DISPOSITION (0x3716001F ou 0x3716001E) para determinar o tipo de anexo.
- Corpo RTF: Se o corpo for RTF, então todos os anexos OLE são anexos embutidos. O valor de PR_ATTACH_METHOD para todos os anexos OLE é igual a 0x00000006.
O exemplo de código a seguir demonstra como diferenciar programaticamente entre anexos embutidos e regulares. A função isInlineAttachment recebe um anexo e um MapiMessage como parâmetros de entrada e retorna true se o anexo for embutido.
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();
}
}
Este trecho de código usa a função isInlineAttachment() para avaliar anexos.
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");
}
}