简体   繁体   中英

Body or Message Contains method using MailKit

I'm using MailKit for received messages via Imap.

Now I need a search-logic for the software and I got a some troubles.

For ex, I using:

var query = SearchQuery.BodyContains("SomeTextfromEmail");
( or var query = SearchQuery.MessageContains("SomeTextfromEmail"); )

foreach (UniqueId uid in imapfolder.Search(query)){
  //some logic
}

So, I can't get messages with text contains on it with Search filter. I'm downloaded message ( message.WriteTo(string.Format(@"C:{0}.eml", uid)); ) and see the message content. Text of message in a base-64 format.

How do it correctly? I need decode message text from base64?

Problem are here:

var query = SearchQuery.BodyContains("Iphone").And(SearchQuery.All);  //this construction find messages in folder
foreach (var uid in imapfolder.Search(query))
{
    Console.WriteLine(message.From + "  :  " + uid.ToString());
}


var query = SearchQuery.BodyContains("Received from Iphone").And(SearchQuery.All); //this construction can't find message in folder
foreach (var uid in imapfolder.Search(query))
{
    Console.WriteLine(message.From + "  :  " + uid.ToString());
}

Your question is a jumbled mess of confusion (your question starts out saying you are trying to figure out how to search and then you end with asking how to base64 decode the raw message that you saved to a file) so I have no idea exactly what you want to know.

If all you want is the decoded text body, you can do this:

var message = imapfolder.GetMessage (uid);
var text = message.TextBody;

This will be the decoded text string (either base64 decoded or quoted-printable decoded if it needs to be).

The MimeKit README and FAQ are both full of useful information on the basics of how to use them. I would highly recommend reading them as they may be helpful.

Update:

Based on the comment added to my answer, it sounds like what you want to do is this?

var matched = new UniqueIdSet ();
foreach (var uid in folder.Search (SearchQuery.BodyContains ("iPhone"))) {
    var message = folder.GetMessage (uid);
    var body = message.TextBody;

    if (body != null && body.Contains ("Received from iPhone"))
        matched.Add (uid);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM