{"id":4387,"date":"2013-11-01T15:44:36","date_gmt":"2013-11-01T13:44:36","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=4387"},"modified":"2022-03-31T16:47:46","modified_gmt":"2022-03-31T14:47:46","slug":"search-unanswered-emails-in-gmail","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/search-unanswered-emails-in-gmail","title":{"rendered":"Search unanswered emails in Gmail"},"content":{"rendered":"<p>IMAP protocol in <a href=\"\/mail\/rfc\/3501\">RFC3501<\/a> introduced \\Answered flag. \\Answered flag should mark messages that have been answered. This flag can by set by client applications or even by SMTP server, which can be examining In-Reply-To email headers.<\/p>\n<p>Unfortunately if the message is answered through Gmail&#8217;s web interface, the \\Answered flag will not be set. It will only be set if the messages were answered using an email program that sets this flag.<\/p>\n<p>This means that \\Answered flag can&#8217;t be used to find not-answered emails.<\/p>\n<p>One option to resolve this problem is to use Gmail IMAP extensions, X-GM-THRID in particular.<\/p>\n<p>We&#8217;ll connect to Gmail using IMAP &#8211; remember to use SSL and be sure to <a href=\"\/blog\/enable-imap-in-gmail\">enable IMAP protocol for Gmail<\/a>.<\/p>\n<p>As we plan to search all emails, including sent and received ones, we&#8217;ll use <em>CommonFolders<\/em> class to get &#8216;All Mail&#8217; folder.<\/p>\n<p>Then we&#8217;ll get <em>Envelope<\/em> for each message. <em>Envelope <\/em>contains <em>GmailThreadId<\/em> property. It contains <strong>thread id<\/strong> to which this email was assigned to by Gmail.<\/p>\n<p>Finally <strong> we&#8217;ll find threads that contain only one message<\/strong> &#8211; those are messages that were not answered.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n \nusing(Imap client = new Imap())\n{\n    client.ConnectSSL(&quot;imap.gmail.com&quot;);\n    client.Login(&quot;pat@gmail.com&quot;, &quot;app-password&quot;);\n \n    \/\/ Select 'All Mail' folder.\n    List&lt;FolderInfo&gt; folders = client.GetFolders();\n    CommonFolders common = new CommonFolders(folders);\n    client.Select(common.AllMail);\n \n    \/\/ Get envelopes for all emails.\n    List&lt;long&gt; uids = client.GetAll();\n    List&lt;Envelope&gt; envelopes = client.GetEnvelopeByUID(uids);\n \n    \/\/ Group messages by thread id.\n    var threads = new Dictionary&lt;decimal, List&lt;Envelope&gt;&gt;();\n    foreach (Envelope envelope in envelopes)\n    {\n        decimal threadId = (decimal) envelope.GmailThreadId;\n        if (threads.ContainsKey(threadId) == false)\n            threads&#x5B;threadId] = new List&lt;Envelope&gt;();\n         \n        threads&#x5B;threadId].Add(envelope);\n    }\n \n    \/\/ Find threads containing single message.\n    foreach (KeyValuePair&lt;decimal, List&lt;Envelope&gt;&gt; pair in threads)\n    {\n        if (pair.Value.Count == 1)\n        {\n            Envelope envelope = pair.Value&#x5B;0];\n            Console.WriteLine(&quot;Gmail message id: {0}; subject: {1}&quot;, \n                envelope.GmailMessageId , \n                envelope.Subject);\n \n        }\n    }\n    client.Close();\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\n' VB.NET\n \nUsing client As New Imap()\n    client.ConnectSSL(\"imap.gmail.com\")\n    client.Login(\"pat@gmail.com\", \"app-password\")\n \n    ' Select 'All Mail' folder.\n    Dim folders As List(Of FolderInfo) = client.GetFolders()\n    Dim common As New CommonFolders(folders)\n    client.&#x5B;Select](common.AllMail)\n \n    ' Get envelopes for all emails.\n    Dim uids As List(Of Long) = client.GetAll()\n    Dim envelopes As List(Of Envelope) = client.GetEnvelopeByUID(uids)\n \n    ' Group messages by thread id.\n    Dim threads = New Dictionary(Of Decimal, List(Of Envelope))()\n    For Each envelope As Envelope In envelopes\n        Dim threadId As Decimal = CDec(envelope.GmailThreadId)\n        If threads.ContainsKey(threadId) = False Then\n            threads(threadId) = New List(Of Envelope)()\n        End If\n \n        threads(threadId).Add(envelope)\n    Next\n \n    ' Find threads containing single message.\n    For Each pair As KeyValuePair(Of Decimal, List(Of Envelope)) In threads\n        If pair.Value.Count = 1 Then\n            Dim envelope As Envelope = pair.Value(0)\n \n            Console.WriteLine(\"Gmail message id: {0}; subject: {1}\", _\n                envelope.GmailMessageId, envelope.Subject)\n        End If\n    Next\n    client.Close()\nEnd Using\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>IMAP protocol in RFC3501 introduced \\Answered flag. \\Answered flag should mark messages that have been answered. This flag can by set by client applications or even by SMTP server, which can be examining In-Reply-To email headers. Unfortunately if the message is answered through Gmail&#8217;s web interface, the \\Answered flag will not be set. It will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,28,77,57,66],"class_list":["post-4387","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-vb-net","tag-x-gm-thrid"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4387"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=4387"}],"version-history":[{"count":15,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4387\/revisions"}],"predecessor-version":[{"id":6098,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/4387\/revisions\/6098"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=4387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=4387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=4387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}