如何解析Gmail中的电子邮件正文,下载和删除

时间:2015-12-19 23:59:24

标签: c# email mailkit

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

我想做以下事情:

  1. 登录Gmail
  2. 在主题或正文中搜索包含特定关键字的收件箱邮件。
  3. 像在C#
  4. 中的文本文件一样解析正文
  5. 下载附件(test.txt)
  6. 删除
  7. 此时我能够成功登录并检索文件夹列表并匹配字符串。

    这是我的代码:

    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步?

1 个答案:

答案 0 :(得分: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);
}