【问题标题】:get text body mailkit获取正文邮件包
【发布时间】:2019-11-25 18:48:36
【问题描述】:

我正在使用 Mailkit 来获取电子邮件的主题, 它对我有用,但我需要将文本正文 任何人都可以帮助我 任何人都可以帮助我 谢谢

async Task FetchMessageSummariesAsync(bool print)
            {
                IList<IMessageSummary> fetched = null;

                do
                {
                    try
                    {
                        // fetch summary information for messages that we don't already have
                        startIndex = startIndex + messages.Count;

                        fetched = client.Inbox.Fetch(startIndex, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId, cancel.Token);
                        break;
                    }
                    catch (ImapProtocolException)
                    {
                        // protocol exceptions often result in the client getting disconnected
                        await ReconnectAsync();
                    }
                    catch (IOException)
                    {
                        // I/O exceptions always result in the client getting disconnected
                        await ReconnectAsync();
                    }
                } while (true);

                messages.Clear();

                foreach (var message in fetched)
                {
                    if (print)
                   Console.WriteLine("new message: {0}", message.Envelope.Subject);

                    messages.Add(message);
                }
                // ---- Insert Data in Database
            }

【问题讨论】:

    标签: c# mailkit


    【解决方案1】:

    你可以从你的客户那里得到正文,就像你得到摘要一样。

    您可以获取同步或异步,但使用相同的索引。我在此处粘贴文档中的链接。 GetBodyPart

    var items = client.Inbox.Fetch (uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);
    
    foreach (var item in items) {
         // determine a directory to save stuff in
         var directory = Path.Combine (baseDirectory, item.UniqueId.ToString ());
    
         // create the directory
         Directory.CreateDirectory (directory);
    
         // IMessageSummary.TextBody is a convenience property that finds the 'text/plain' body part for us
         var bodyPart = item.TextBody;
    
         // download the 'text/plain' body part
         var body = (TextPart) client.Inbox.GetBodyPart (item.UniqueId, bodyPart);
    
         // TextPart.Text is a convenience property that decodes the content and converts the result to
         // a string for us
         var text = body.Text;
    }
    

    【讨论】:

    • 您需要知道要下载的消息的 UID 和/或索引。这就是本例中uids 所代表的全部内容。 baseDirectory 代表硬盘上您要保存这些邮件正文的本地目录。这只是一个例子。
    • 一个更完整的例子可以在这里找到:mimekit.net/docs/html/M_MailKit_IMailFolder_GetBodyPart.htm
    • 我只是粘贴一个例子,你需要用你自己的数据/代码检查它。抱歉,如果造成混淆。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2016-07-04
    • 2016-12-12
    • 1970-01-01
    相关资源
    最近更新 更多