0

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

4

1 回答 1

0
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);
}
于 2016-01-06T23:22:49.300 回答