MailKit保存附件 [英] MailKit save Attachments

查看:284
本文介绍了MailKit保存附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存邮件中的附件

I'm try save attachments from message

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

文件已保存,但是当我打开时出现错误
文件损坏或太大
该文件的大小为88 kb,但文件大小应等于55 kb。

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.

我如何仅记录附件?

MailKit v1.2.0.0 MimeKit 1.2.0.0

MailKit v1.2.0.0 MimeKit 1.2.0.0

推荐答案

您正在保存整个MIME对象(包括标题)。您需要做的就是保存内容。

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);
        }
    }
}

希望有帮助。

这篇关于MailKit保存附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆