Home > Enterprise >  MimeKit dosen't see attachments by html
MimeKit dosen't see attachments by html

Time:04-06

I'm using MimeKit to download .PDF attachment from mails. Today I got a mail with two attachments, first "part_1.html" and second "sample.pdf". This is first time when I got .HTML attachment (this a mail body but also in attachment, idk) and my code return null in attachments. I can't understand why in code, mail dosen't have attachments. If I send this mail again but without this html somethink everythink works properly.

Code is looking in attachemnts for .PDF, and set true on "IsAnyPdf", next my code download attachment from list but now list is null. Code:

var Email = client.Inbox.GetMessage(MailId);
            ListOfAttachments = Email.Attachments.Where(x => x.IsAttachment).ToList();
            IsAnyPDF = ListOfAttachments.Any(x => x.ContentDisposition.FileName?.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase) ?? x.ContentType.Name?.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase) ?? false);

Is it possible to somehow avoid this attachment in MailKit.Attachments?

CodePudding user response:

This is unnecessary:

ListOfAttachments = Email.Attachments.Where(x => x.IsAttachment).ToList();

MimeMessage.Attachments already only includes parts where IsAttachment is true, so it's a waste.

As far as why your code isn't finding the pdf "attachment" goes, the problem is 100% guaranteed to be that the pdf "attachment" isn't an "attachment", but rather added to the message as "inline".

So... do this:

var Email = client.Inbox.GetMessage(MailId);
IsAnyPDF = Email.BodyParts.OfType<MimePart>().Any(x => x.ContentDisposition.FileName?.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase) ?? x.ContentType.Name?.EndsWith(".pdf", StringComparison.CurrentCultureIgnoreCase) ?? false);
  • Related