Email Attachment Info when Downloading from IMAP without Attachments

If the Imap.AutoDownloadAttachments property is set to false, or if headers-only are downloaded, then emails will be downloaded without attachments.  However, attachment information, such as the count, and the size and filename of each attachment is still available.

When an email is downloaded from an IMAP server without the attachment data, the attachment information is added as headers to the email:

  • ckx-imap-numattach: contains the number of attachments.
  • ckx-imap-attach-nm-{N}: contains the name of the Nth attachment, where N begins at 1 for the 1st attachment.  For example “ckx-imap-attach-nm-1”
  • ckx-imap-attach-sz-{N}: contains the size in bytes of the Nth attachment.

The IMAP API provides methods for getting this information:

  • GetMailNumAttach
  • GetMailAttachSize
  • GetMailAttachFilename

Each of these methods begins with the string “GetMail”, and the 1st argument is the email object from which the information is to be retrieved.  If the “ckx-imap-*” header is not found, then it returns the information provided by the email object itself:

  • GetMailNumAttach:  If the ckx-imap-numattach header is not present, it returns the value of the emailObject.NumAttachments property.   (where emailObject is the email object passed to GetMailNumAttach)
  • GetMailAttachSize: If the ckx-imap-attach-sz-* header is not present, it calls emailObject.GetAttachmentSize and returns its value.
  • GetMailAttachFilename: If the ckx-imap-attach-nm-* header is not present, it calls emailObject.GetAttachmentFilename and returns its value.

The Chilkat.Email object (CkEmail, CkoEmail, etc.) also has properties and methods for getting attachment information.  However, these apply to attachments that are already downloaded and present within the email object.  For example:

  • Email.NumAttachments:  This property returns the number of attachments that are fully present within the email object.
  • Email.GetAttachmentFilename:  This method returns the filename of the Nth attachment that is already fully present in the email object.
  • Email.GetAttachmentSize:  Again, it returns the size in bytes of the Nth attachment that is fully present in the email object.

Email Attachment Not Found?

The following is a list of the most common mistakes made when expecting to find an attachment in an email object:

  1. You downloaded headers-only.  If you call a method to download only the headers of an email, then the attachments will not be included.  With IMAP you will still get information about the attachments, but with POP3 you will not get them at all.  See this blog post about POP3 email headers and attachment info.
  2. What you are expecting to see as an attachment is actually a “related item”.  A related item is an image, style sheet, etc. that is part of the HTML presentation.  It is an embedded item that is referenced by the HTML body of an email and is not a formal attachment.  You’ll find a complete set of properties and methods for related items:  NumRelatedItems, GetRelatedData, SaveRelatedItem, etc.
  3. It is an “attached message”.  Similar to related items, a complete email that is attached to another email is considered separately by Chilkat.  The property is NumAttachedMessages.  To get the Nth attached message, call GetAttachedMessage(index).  this returns a new email object that is the Nth attached message.
  4. It is an attachment found within an attached message.  You would first need to get the attached message and then you’ll see the attachments within it.