使用MailKit检查附件尺寸



我正在下载带有以下代码的给定邮件的附件:

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标头具有大小参数时才设置,例如:

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

相关内容

  • 没有找到相关文章

最新更新