Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MailKit save Attachments

Tags:

I'm try save attachments from message

foreach(MimeKit.MimeEntity at message.Attachments)  {     at.WriteTo("nameFile"); } 

File saved, but when I open I get the error the file is corrupted or too large The size of this file is 88 kb, but size of the file should be equal to 55 kb.

I think that in all recorded message file.

How do I only record the attachment?

MailKit v1.2.0.0 MimeKit 1.2.0.0

like image 240
sergpc Avatar asked Mar 25 '16 20:03

sergpc


1 Answers

You are saving the entire MIME object (including the headers). What you need to do is save the content.

foreach (var attachment in message.Attachments) {     using (var stream = File.Create ("fileName")) {         if (attachment is MessagePart) {             var part = (MessagePart) attachment;              part.Message.WriteTo (stream);         } else {             var part = (MimePart) attachment;              part.Content.DecodeTo (stream);         }     } } 

Hope that helps.

like image 193
jstedfast Avatar answered Oct 19 '22 11:10

jstedfast