Mimekit,IMapClient无需下载完整消息即可获取附件信息 [英] Mimekit, IMapClient get attachment information without downloading whole message

查看:272
本文介绍了Mimekit,IMapClient无需下载完整消息即可获取附件信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来获取主题信息.

I am using the following code to obtain subject information.

是否可以在不下载整个邮件的情况下知道电子邮件是否包含附件,尤其是excel电子表格(xls/xlsx)?

Is it possible to know if the email contains attachments, and perhaps more specifically excel spreadsheets (xls/xlsx) without downloading the entire message?

client.Connect("imap.gmail.com", 993);
client.Authenticate("spyperson", "secret-word");
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);

Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);

var uids = inbox.Search(SearchQuery.NotSeen);
foreach (var summary in inbox.Fetch(uids, MessageSummaryItems.Full | MessageSummaryItems.UniqueId | MessageSummaryItems.Flags))
{
    Console.WriteLine("[summary] {0:D2}: {1}:{2}", summary.Index, summary.Envelope.Subject, summary.Flags);
}

推荐答案

是的,这是可能的.为此,您需要将MessageSummaryItems.BodyStructure标志传递给Fetch()方法.

Yes, this is possible. In order to do this, however, you'll need to pass the MessageSummaryItems.BodyStructure flag to the Fetch() method.

这将填充summary.Body属性.

如果填充了Body属性,则可以将BodyParts属性用作快速&遍历消息中扁平的身体部位层次结构的肮脏方式,检查它们是否为附件,如下所示:

If the Body property is populated, you can use the BodyParts property as a quick & dirty way of iterating over a flattened hierarchy of body parts in the message, checking if any of them are attachments like this:

var hasAttachments = summary.BodyParts.Any (x => x.IsAttachment);

检查xls/xlsx附件的一种方法可能是:

One way to check for xls/xlsx attachments might be the following:

var hasAttachments = summary.BodyParts.Any (x => x.IsAttachment &&
    x.FileName != null && (x.FileName.EndsWith (".xls") ||
    x.FileName.EndsWith (".xslsx")));

这些是非常简单的检查,但是很可能您对附件是或不是的解释将与IsAttachment属性告诉您的内容相冲突,因此,我建议您使用Visitor模式遍历MIME层次结构,或者使用递归并使用您自己的逻辑通过您自己的自定义定义来确定零件是否为附件(每个人对于电子邮件中的附件"似乎都有自己的独特解释).

These are very simplistic checks, however, and most likely your interpretation of what is or isn't an attachment will conflict with what the IsAttachment property tells you, so I'd probably recommend using either the Visitor pattern for traversing the MIME hierarchy or else using recursion and using your own logic to determine if a part is an attachment by your own custom definition (everyone seems to have their own unique interpretation of what constitutes an "attachment" when it comes to email).

我在以下位置有关于常见MIME层次结构的文档:

I've got docs on common MIME hierarchies in the following locations:

  1. http://www.mimekit.net/docs/html/WorkingWithMessages. htm#MessageStructure
  2. http://www.mimekit.net/docs/html/FrequentlyAskedQuestions. htm#MessageBody
  1. http://www.mimekit.net/docs/html/WorkingWithMessages.htm#MessageStructure
  2. http://www.mimekit.net/docs/html/FrequentlyAskedQuestions.htm#MessageBody

...还有其他地方.

...and probably other places.

这篇关于Mimekit,IMapClient无需下载完整消息即可获取附件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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