3

i'm using Mailkit to get the subject of emails , it's working for me but i need to get the text body to , any one could help me any one could help me thanks

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
            }

1 Answers1

2

You can get the body from your client, in the same way, you are getting the summary.

You can get sync or async, but using the same index. I paste here the link in the documentation. 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;
}
SirMartin
  • 148
  • 1
  • 7
  • You need to know the UID(s) and/or indexes of the message(s) that you want to download. That's all `uids` represents in this example. `baseDirectory` represents the local directory on your hard disk where you want to save these message bodies. It's just an example. – jstedfast Nov 25 '19 at 20:54
  • A more complete example can be found here: http://www.mimekit.net/docs/html/M_MailKit_IMailFolder_GetBodyPart.htm – jstedfast Nov 25 '19 at 20:55
  • I just paste an example, you need to check it with your own data/code. Sorry if was confusing. – SirMartin Nov 26 '19 at 08:05