如何在Gmail中解析邮件正文、下载和删除

How to parse the body of an email in Gmail, download and delete

我正在使用 Mimekit 接收来自 Gmail 的邮件,使用 C# 进行 IOT 通知,这似乎有效。

我想做以下事情:

  1. 登录 Gmail
  2. 搜索主题或正文中包含特定关键字的收件箱邮件。
  3. 像在 C# 中解析文本文件一样解析正文
  4. 下载一个附件(test.txt)
  5. 删除

此时我可以成功登录并检索文件夹列表并匹配字符串。

这是我的代码:

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 ");
        }

所以我的问题是:如何完成第 3、4 和 5 步?

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);
}