Diferenciar entre adjuntos en línea y adjuntos regulares
Contents
[
Hide
]
Los mensajes de correo pueden contener imágenes en línea así como adjuntos. Usando MailMessage, los adjuntos en línea pueden extraerse de la LinkedResourceCollection colección, mientras que los adjuntos regulares pueden accederse y extraerse con la colección de un mensaje AttachmentCollection colección. Sin embargo, esto es diferente cuando el mensaje se carga usando MapiMessage, ya que todas las imágenes en línea y los adjuntos regulares son accesibles al usuario en el mismo MapiAttachmentCollection. Por lo tanto, un método que pueda diferenciar entre un adjunto en línea y un adjunto regular cuando MapiMessage se usa si es necesario.
Este artículo explica cómo diferenciar los adjuntos en línea de los regulares usando MapiMessage. Al decidir, el tipo de cuerpo de MapiMessage se tiene en cuenta de la siguiente manera:
- Cuerpo de texto plano: Los mensajes de correo con cuerpo de texto plano no necesitan ser verificados, ya que todos los adjuntos en dichos mensajes son siempre adjuntos regulares.
- Cuerpo HTML: Para mensajes con cuerpo HTML, el adjunto no solo debe contener la propiedad PR_ATTACH_FLAGS (0x37140003), sino que su valor debe ser igual a 0x00000004 para adjuntos en línea. Si se cumple esta condición, entonces depende además de las etiquetas PR_ATTACH_CONTENT_LOCATION y PR_ATTACH_CONTENT_ID para determinar la naturaleza del adjunto. Sin embargo, en ausencia de la etiqueta MAPI PR_ATTACH_FLAGS, el adjunto se verifica por la propiedad PR_ATTACH_DISPOSITION (0x3716001F o 0x3716001E) para determinar el tipo de adjunto.
- Cuerpo RTF: Si el cuerpo es RTF, entonces todos los adjuntos OLE son adjuntos en línea. El valor de PR_ATTACH_METHOD para todos los adjuntos OLE es igual a 0x00000006.
El siguiente ejemplo de código demuestra cómo diferenciar programáticamente entre adjuntos en línea y regulares. La función isInlineAttachment toma un adjunto y un MapiMessage como parámetros de entrada y devuelve true si el adjunto es en línea.
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 fragmento de código usa la función isInlineAttachment() para evaluar los adjuntos.
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");
}
}