Fetch
方法只提取摘要有關郵件的信息(如在郵件客戶端中構建郵件列表所需的信息)。
如果要獲取消息,則需要使用GetMessage
方法。
像這樣:
using (var client = new ImapClient()) {
client.Connect ("imap.gmail.com", 993, true);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate ("username", "password");
client.Inbox.Open (FolderAccess.ReadOnly);
var uids = client.Inbox.Search (SearchQuery.All);
foreach (var uid in uids) {
var message = client.Inbox.GetMessage (uid);
var text = message.TextBody;
Console.WriteLine ("This is the text/plain content:");
Console.WriteLine ("{0}", text);
}
client.Disconnect (true);
}
現在,如果你想下載僅郵件正文,你需要使用的摘要信息,你是獲取和傳遞中作爲參數到GetBodyPart
方法是這樣的:
using (var client = new ImapClient()) {
client.Connect ("imap.gmail.com", 993, true);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate ("username", "password");
client.Inbox.Open (FolderAccess.ReadOnly);
// Note: the Full and All enum values don't mean what you think
// they mean, they are aliases that match the IMAP aliases.
// You should also note that Body and BodyStructure have
// subtle differences and that you almost always want
// BodyStructure and not Body.
var items = client.Inbox.Fetch (0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
foreach (var item in items) {
if (item.TextBody != null) {
var mime = (TextPart) client.Inbox.GetBodyPart (item.UniqueId, item.TextBody);
var text = mime.Text;
Console.WriteLine ("This is the text/plain content:");
Console.WriteLine ("{0}", text);
}
}
client.Disconnect (true);
}
你可以認爲Fetch
方法爲您的IMAP服務器上做一個SQL查詢的元數據˚F或者您的消息以及枚舉參數作爲位域,其中枚舉值可以按位或一起指定哪個IMessageSummary
要由Fetch
查詢填充。
在上面的例子中,UniqueId
和Body
bitflags指定我們要填充IMessageSummary
結果UniqueId
和Body
性能。
如果我們想獲得有關讀取/未讀狀態的信息等 - 我們會將MessageSummaryItems.Flags
添加到列表中。
注意:兩個Body
和BodyStructure
枚舉值填充IMessageSummary.Body
屬性,但BodyStructure
包括被需要,以確定是否一個身體部位是附件或不詳細信息等