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
包括被需要,以确定是否一个身体部位是附件或不详细信息等