【问题标题】:Not able to retrieve an email's body with TextBody无法使用 TextBody 检索电子邮件的正文
【发布时间】:2019-08-06 20:12:47
【问题描述】:

以前使用 OpenPop 来测试电子邮件通知。今天切换到 IMAP 并开始研究 MailKit。我目前在从 Gmail 中检索电子邮件正文的纯字符串文本时遇到问题。

到目前为止我的代码的 sn-p:

using (var client = new ImapClient())
{
    var credentials = new NetworkCredential("username", "password");
    var uri = new Uri("imaps://imap.gmail.com");

    client.Connect(uri);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    client.Authenticate(credentials);
    client.Inbox.Open(FolderAccess.ReadOnly);

    var inboxMessages = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full).ToList();

    foreach (var message in inboxMessages)
    {
        var messageBody = message.TextBody.ToString();
        ...
    }

    ...
}

据我目前对文档的了解,TextBody 可以以纯文本形式检索消息的正文(如果存在)。但是,在 Visual Studio 中调试时,我看到这是 TextBody 的值。

{("TEXT" "PLAIN" ("CHARSET" "utf-8" "FORMAT" "flowed") NIL NIL "7BIT" 6363 NIL NIL NIL NIL 119)}

我在某处缺少步骤吗?这是否意味着从 MailKit 的角度来看缺少正文?我也看到了 HtmlBody 的类似值。

【问题讨论】:

    标签: c# imap mailkit


    【解决方案1】:

    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 查询以获取消息的元数据,并将 MessageSummaryItems 枚举参数视为一个位域,其中枚举值可以按位或组合在一起指定您希望通过Fetch 查询填充哪些IMessageSummary 属性。

    在上面的示例中,UniqueIdBody 位标志指定我们要填充 IMessageSummary 结果的 UniqueIdBody 属性。

    如果我们想获取有关已读/未读状态等信息 - 我们会将MessageSummaryItems.Flags 添加到列表中。

    注意:BodyBodyStructure 枚举值都填充了IMessageSummary.Body 属性,但BodyStructure 包含确定正文部分是否为附件等所需的更多信息。

    【讨论】:

      猜你喜欢
      • 2018-12-27
      • 1970-01-01
      • 2011-04-05
      • 2018-08-25
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多