{"id":1857,"date":"2011-06-12T15:02:22","date_gmt":"2011-06-12T13:02:22","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1857"},"modified":"2014-06-04T09:15:18","modified_gmt":"2014-06-04T07:15:18","slug":"get-gmail-labels-for-specified-messages","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/get-gmail-labels-for-specified-messages","title":{"rendered":"Get Gmail labels for specified messages"},"content":{"rendered":"<p>Gmail treats labels as folders for the purposes of IMAP.<\/p>\n<p>As such, labels can be modified using the standard IMAP commands, <a href=\"\/blog\/folder-management-using-imap-create-delete-rename\">CreateFolder, RenameFolder, and DeleteFolder<\/a>, that act on folders.<\/p>\n<p>System labels, which are labels created by Gmail, are reserved and prefixed by &#8220;[Gmail]&#8221; or &#8220;[GoogleMail]&#8221; in the list of labels.<\/p>\n<p>Use the <a href=\"\/blog\/list-all-gmail-labels\">XLIST command to get the entire list of labels for a mailbox<\/a>.<\/p>\n<p>The labels for a given message may be retrieved by using the X-GM-LABELS attribute with the FETCH command.<\/p>\n<p>For list of messages<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.ConnectSSL(&quot;imap.gmail.com&quot;);\r\n    imap.UseBestLogin(&quot;pat@gmail.com&quot;, &quot;password&quot;);\r\n\r\n    imap.SelectInbox();\r\n\r\n    List&lt;long&gt; uids = imap.GetAll();\r\n    List&lt;messageInfo&gt; infos = imap.GetMessageInfoByUID(uids);\r\n\r\n    foreach (MessageInfo info in infos)\r\n    {\r\n        List&lt;string&gt; labels = info.GmailLabels.Labels;\r\n\r\n        Console.WriteLine(&quot;&#x5B;{0}] {1}&quot;,\r\n            string.Join(&quot; &quot;, labels.ToArray()),\r\n            info.Envelope.Subject);\r\n    }\r\n\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nUsing imap As New Imap()\r\n\timap.ConnectSSL(&quot;imap.gmail.com&quot;)\r\n\timap.UseBestLogin(&quot;pat@gmail.com&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\r\n\tDim uids As List(Of Long) = imap.GetAll()\r\n\tDim infos As List(Of MessageInfo) = imap.GetMessageInfoByUID(uids)\r\n\r\n\tFor Each info As MessageInfo In infos\r\n            Dim labels As List(Of String) = info.GmailLabels.Labels\r\n\r\n\t    Console.WriteLine(&quot;&#x5B;{0}] {1}&quot;,  _\r\n                String.Join(&quot; &quot;, labels.ToArray()),  _\r\n                info.Envelope.Subject)\r\n\tNext\r\n\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<p>For single message:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nusing (Imap imap = new Imap())\r\n{\r\n    imap.ConnectSSL(&quot;imap.gmail.com&quot;);\r\n    imap.Login(&quot;pat@gmail.com&quot;, &quot;password&quot;);\r\n\r\n    imap.SelectInbox();\r\n\r\n    long uid = imap.GetAll().Last();\r\n    List&lt;string&gt; labels = imap.GmailGetLabelsByUID(uid);\r\n\r\n    labels.ForEach(Console.WriteLine);\r\n\r\n    imap.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nUsing imap As New Imap()\r\n\timap.ConnectSSL(&quot;imap.gmail.com&quot;)\r\n\timap.Login(&quot;pat@gmail.com&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\r\n\tDim uid As Long = imap.GetAll().Last()\r\n\tDim labels As List(Of String) = imap.GmailGetLabelsByUID(uid)\r\n\r\n\tlabels.ForEach(Console.WriteLine)\r\n\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<p>You can learn more about this Gmail IMAP extension here:<br \/>\n<a href=\"http:\/\/code.google.com\/apis\/gmail\/imap\/#x-gm-labels\">http:\/\/code.google.com\/apis\/gmail\/imap\/#x-gm-labels<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gmail treats labels as folders for the purposes of IMAP. As such, labels can be modified using the standard IMAP commands, CreateFolder, RenameFolder, and DeleteFolder, that act on folders. System labels, which are labels created by Gmail, are reserved and prefixed by &#8220;[Gmail]&#8221; or &#8220;[GoogleMail]&#8221; in the list of labels. Use the XLIST command to [&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,25,28,77,57,63],"class_list":["post-1857","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-gmail","tag-imap","tag-imap-component","tag-vb-net","tag-x-gm-labels"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1857"}],"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=1857"}],"version-history":[{"count":5,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"predecessor-version":[{"id":4616,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions\/4616"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}