0

I am using Mimekit to receive mail from Gmail using C# for IOT notifications, which seems to be working.

I would like to do the following:

  1. Log in to Gmail
  2. Search inbox mail containing a specific keyword in subject or body.
  3. Parse the body like you would a text file in C#
  4. Download a attachment (test.txt)
  5. Delete

At this point I am able to login successfully and retrieve a list of folders and matches for a string.

Here is my code:

using (var client = new ImapClient())
        {
            client.Connect("imap.gmail.com", 993,    SecureSocketOptions.SslOnConnect);

            // disable OAuth2 authentication unless you are actually using an access_token
            client.AuthenticationMechanisms.Remove("XOAUTH2");

            client.Authenticate("user@gmail.com", "password");
            MessageBox.Show("we're connected");

            // The Inbox folder is always available on all IMAP servers...
            var inbox = client.Inbox;
            inbox.Open(FolderAccess.ReadOnly);

            //1. search for all messages containing the string test123
            var query = SearchQuery.FromContains("test123");

            foreach (var uid in inbox.Search(query))
            {
                var message = inbox.GetMessage(uid);

                System.Diagnostics.Debug.WriteLine("[match] {0}: {1}", uid, message.Subject);

                //2. Show all folders in Personal
                var personal = client.GetFolder(client.PersonalNamespaces[0]);
                foreach (var folder in personal.GetSubfolders(false))
                    System.Diagnostics.Debug.WriteLine("[folder] {0}", folder.Name);
            }
            client.Disconnect(true);
            MessageBox.Show("disconnected ");
        }

So my question is: How do I accomplish steps 3 , 4 and 5?

BSMP
  • 4,596
  • 8
  • 33
  • 44
user3407537
  • 51
  • 1
  • 13

1 Answers1

1
using (var client = new ImapClient ()) {
    client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);

    // disable OAuth2 authentication unless you are actually using an access_token
    client.AuthenticationMechanisms.Remove ("XOAUTH2");

    // 1. Log in to Gmail
    client.Authenticate ("user@gmail.com", "password");

    // 2. Search inbox mail containing a specific keyword in subject or body.
    client.Inbox.Open (FolderAccess.ReadWrite);

    var query = SearchQuery.SubjectContains ("123").Or (SearchQuery.BodyContains ("123"));

    foreach (var uid in client.Inbox.Search (query)) {
        // 3. Parse the body like you would a text file in C#

        // This downloads and parses the full message:
        var message = client.Inbox.GetMessage (uid);

        // 4. Download a attachment (test.txt)

        // No need to download an attachment because you already
        // downloaded it with GetMessage().

        // Here's how you could get the "test.txt" attachment:
        var attachment = message.BodyParts.OfType<MimePart> ()
            .FirstOrDefault (x => x.FileName == "test.txt");

        // 5. Delete

        // This marks the message as deleted, but does not purge it
        // from the folder.
        client.Inbox.AddFlags (uid, MessageFlags.Deleted, true);
    }

    // Purge the deleted messages (if you use Thunderbird, this is aka "Compact Folders")
    client.Inbox.Expunge ();

    client.Disconnect (true);
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110