使用 mailkit 检查附件大小

Check attachment size with 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 都是空的。是否有关于附件大小的字段?

ContentDisposition.Size 属性 仅在 Content-Disposition header 具有大小参数时设置,如下所示:

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