[英]MailKit IMAP search by UID and download individual MIME parts
我已經能夠檢索到我檢查的最后一條消息之后的UID列表,因此我只能從IMAP服務器獲取新消息 :
using(var client = new ImapClient())
{
//client authentication code...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
//msgIdx contains the UID of the last message I've checked,
// so I can retrieve all the new messages after this one.
var range = new UniqueIdRange(new UniqueId((uint) msgIdx), UniqueId.MaxValue);
IList<UniqueId> uids = inbox.Search(range, SearchQuery.All);
foreach(var uid in uids)
{
//With the following instruction I download the whole message...
var message = inbox.GetMessage(uid);
LblMessageLog.Text += uid + " Subject:" + message.Subject + " []<br/>";
}
client.Disconnect(true);
}
問題是我不想下載整個郵件,而只下載特定的附件 ,據我所知,它將始終存在於所有郵件中。
MailKit網站上有一個示例,但它與我上面的搜索有沖突:
foreach (var summary in inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure)) {
if (summary.Body is BodyPartMultipart) {
var multipart = (BodyPartMultipart) summary.Body;
var attachment = multipart.BodyParts.OfType<BodyPartBasic> ().FirstOrDefault (x => x.FileName == "cert.xml");
if (attachment != null) {
// this will download *just* the attachment
var part = inbox.GetBodyPart (summary.UniqueId, attachment);
}
}
}
如何在UID上過濾數據然后僅下載一部分電子郵件的兩個過程?
這是如何混合兩者:
var range = new UniqueIdRange(new UniqueId((uint) msgIdx), UniqueId.MaxValue);
var items = inbox.Fetch(uids, MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope);
foreach(var item in items)
{
LblMessage.Text += item.UniqueId + " Subject:" + item.Envelope.Subject + "<br/>";
}
提取方法有一個重載,它接受一個IList of UniqueIDs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.