jstedfast/MimeKit

Attachment is not seen by MimeKit

kchristman54 opened this issue · 4 comments

Describe the bug

We have some emails that have attachments on them, but our code which uses MimeKit doesn't see or download them. Upon closer inspection, the Attachments property on MimeMessage is an empty list.

We know the attachment exists because if we use a webmail client we can see and download them manually that way. We can also see the filename of the attachment when we put a debugger in our code (see 2nd image). If necessary, I'd be happy to directly send you the email (just rather not post it here on a public Github issue).

We've been using MimeKit here at Energy System Lab (at Texas A&M) for over a year to download millions of email attachments and it's been great. Thank you!

image

image

Platform (please complete the following information):

  • OS: Windows
  • .NET Runtime: CoreCLR
  • .NET Framework: .NET (Core) 7
  • MimeKit Version: 4.3.0

Hi,

So the reason that the excel file doesn't appear in the MimeMessage.Attachments property (and the reason that the IsAttachment property is false) is because the Content-Disposition header is "inline". In order to be considered an attachment, it would need to have a Content-Disposition header with a value of "attachment".

You'll be able to find this part in the MimeMessage.BodyParts list (actually, all parts are in this list whether they are marked as attachments or inline).

This "inline" vs "attachment" thing is a constant source of confusion, I think because in common parlance, people think of attachments as any and all files that get added to an email whereas the technical definition depends on the Content-Disposition value where these file attachments are distinctly "attachment" vs "inline".

Thanks for the explanation. Using MimeKit, is there a good way for me to get the byte array of the "inline" data? I tried using MimeEntity.WriteTo (where you pass in a Stream), but from that it resulted in this file
image

What you probably want is MimePart.Content.DecodeTo(Stream)

(The MimePart.Content property is a MimeContent object)

This will write the decoded content of the MIME part (aka "attachment") to the stream.

Thanks!