获取文本正文 mailkit

get text body mailkit

我正在使用 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
            }

您可以从您的客户端获取正文,以同样的方式获取摘要。

您可以获得同步或异步,但使用相同的索引。我将文档中的 link 粘贴到此处。 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;
}