简体   繁体   English

如何检查邮件是否有附件(MailKit)

[英]How to check if message has attachments (MailKit)

I'm using IMAP4 client called: MailKit. 我正在使用名为:MailKit的IMAP4客户端。

It works great, but I have a problem knowing whether the message has attachments or not. 它工作得很好,但我知道消息是否有附件有问题。

I've tried: 我试过了:

var summary = inbox.Fetch(MessageId,MessageSummaryItems.Body,cancel.Token).FirstOrDefault();  
var bodyMultiPart = summary.Body as BodyPartMultipart;
if (bodyMultiPart != null)
{
   foreach (var bodyPart in bodyMultiPart.BodyParts.Where(x => x is BodyPartBasic))
   {
        BodyPartBasic basicPart = bodyPart as BodyPartBasic;
        if (basicPart.ContentDisposition != null && basicPart.ContentDisposition.IsAttachment)
        {
           //add basic part as attachment
        }               
   }    
}

But for some messages (for example: message that has PDF as attachment) the content disposition is null . 但是对于某些消息(例如:将PDF作为附件的消息),内容处置为

Try using MessageSummaryItems.BodyStructure instead of MessageSummaryItems.Body. 尝试使用MessageSummaryItems.BodyStructure而不是MessageSummaryItems.Body。

BODYSTRUCTURE retrieves more details for each body part than BODY does. BODYSTRUCTURE检索每个身体部位的更多细节,而不是BODY。 I think that the Content-Disposition header is one of the extra things that BODYSTRUCTURE retrieves over plain BODY. 我认为Content-Disposition标头是BODYSTRUCTURE在普通BODY上检索的额外内容之一。

Hope that helps. 希望有所帮助。

When the content disposition is null, you can use the content type as a hint: text/* is seldom an attachment, multipart/* is not a hint, other types generally hint at attachments. 当内容处置为空时,您可以使用内容类型作为提示:text / *很少是附件,multipart / *不是提示,其他类型通常暗示附件。

PDF is application/pdf, so that's an attachment according to this heuristic. PDF是application / pdf,所以这是根据这种启发式的附件。

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

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