Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Gmail中解析邮件正文,下载和删除_C#_Email_Mailkit - Fatal编程技术网

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

C# 如何在Gmail中解析邮件正文,下载和删除,c#,email,mailkit,C#,Email,Mailkit,我正在使用Mimekit接收来自Gmail的邮件,使用C#接收物联网通知,这似乎是可行的 我想做以下工作: 登录到Gmail 搜索主题或正文中包含特定关键字的收件箱邮件 像解析C中的文本文件一样解析主体# 下载附件(test.txt) 删除 此时,我能够成功登录并检索文件夹列表和字符串匹配项 这是我的密码: using (var client = new ImapClient()) { client.Connect("imap.gmail.com", 99

我正在使用Mimekit接收来自Gmail的邮件,使用C#接收物联网通知,这似乎是可行的

我想做以下工作:

  • 登录到Gmail
  • 搜索主题或正文中包含特定关键字的收件箱邮件
  • 像解析C中的文本文件一样解析主体#
  • 下载附件(test.txt)
  • 删除
  • 此时,我能够成功登录并检索文件夹列表和字符串匹配项

    这是我的密码:

    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?

    使用(var client=new ImapClient()){
    
    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);
    }
    
    client.Connect(“imap.gmail.com”,993,SecureSocketOptions.SslOnConnect); //禁用OAuth2身份验证,除非您实际使用的是访问令牌 client.AuthenticationMechanisms.Remove(“XOAUTH2”); //1.登录Gmail 客户端身份验证(“user@gmail.com“,”密码“); //2.搜索包含主题或正文中特定关键字的收件箱邮件。 client.Inbox.Open(FolderAccess.ReadWrite); var query=SearchQuery.SubjectContains(“123”)。或(SearchQuery.BodyContains(“123”)); foreach(client.Inbox.Search(查询)中的变量uid){ //3.像解析C中的文本文件一样解析正文# //这将下载并解析完整消息: var message=client.Inbox.GetMessage(uid); //4.下载附件(test.txt) //无需下载附件,因为您已经 //使用GetMessage()下载它。 //以下是如何获取“test.txt”附件: var attachment=message.BodyParts.OfType() .FirstOrDefault(x=>x.FileName==“test.txt”); //5.删除 //这会将邮件标记为已删除,但不会将其清除 //从文件夹中。 client.Inbox.AddFlags(uid,MessageFlags.Deleted,true); } //清除已删除的邮件(如果您使用Thunderbird,这就是所谓的“压缩文件夹”) client.Inbox.Expunge(); client.Disconnect(true); }
    谢谢你,jstedfast..太棒了..它可以工作:-)