简体   繁体   English

MailKit保存附件

[英]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. 文件已保存,但是当我打开时我得到错误文件已损坏或太大此文件的大小为88 kb,但文件大小应等于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 MailKit v1.2.0.0 MimeKit 1.2.0.0

You are saving the entire MIME object (including the headers). 您正在保存整个MIME对象(包括标题)。 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. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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