使用mailkit检查附件大小

时间:2017-02-23 14:49:56

标签: c# size imap attachment mailkit

我正在使用以下代码下载给定mailid的附件:

HeaderSearchQuery searchCondition = SearchQuery.HeaderContains("Message-Id", ssMailItemId);

                var folder = client.GetFolder(ssFolderName);
                folder.Open(FolderAccess.ReadOnly);

                IList<UniqueId> ids = folder.Search(searchCondition);

                foreach (UniqueId uniqueId in ids)
                {
                    MimeMessage message = folder.GetMessage(uniqueId);

                    foreach (MimeEntity attachment in message.Attachments)
                    {
                        ssAttachmentsDetail.Append(fillAttachmentDetailRecord(attachment, uniqueId.Id.ToString()));
                    }

但是MimeEntity.ContentDisposition.Size和MimePart.ContentDuration都是null。是否有关于附件大小的字段?

1 个答案:

答案 0 :(得分:0)

ContentDisposition.Size属性仅在Content-Disposition标头具有size参数时设置,如下所示:

Content-Disposition: attachment; size=15462

但是,无论如何,这个价值可能都不值得信任......

如果你想要一个附件的大小,唯一准确的方法是做这样的事情:

// specify that we want to fill the IMessageSummary.Body and IMessageSummary.UniqueId fields...
var items = folder.Fetch (ids, MessageSummaryItems.BodyStructure | MessageSummaryItems.UniqueId);
foreach (var item in items) {
    foreach (var attachment in item.Attachments) {
        // 'octets' is just a fancy word for "number of bytes"
        var size = attachment.Octets;

        // download the individual attachment
        var entity = folder.GetBodyPart (item.UniqueId, attachment);
    }
}