简体   繁体   中英

MailKit save Attachments

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM