{"id":389,"date":"2010-01-27T20:31:22","date_gmt":"2010-01-27T18:31:22","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=389"},"modified":"2014-04-28T09:30:13","modified_gmt":"2014-04-28T07:30:13","slug":"mark-emails-as-read-with-imap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/mark-emails-as-read-with-imap","title":{"rendered":"Mark emails as read using IMAP"},"content":{"rendered":"<p>In this article we&#8217;ll show how to mark emails as seen (read) using Mail.dll <a href=\"\/mail\">.NET IMAP library<\/a> and IMAP protocol.<\/p>\n<p>The following code connects to an IMAP server, <strong>finds<\/strong> unique ids of all <strong>unseen email messages<\/strong> and <strong>marks the first one as seen<\/strong>. Internally IMAP server uses IMAP flag \\UNSEEN (<em>Flag.Unseen<\/em>) to remember which messages were already read by user.<\/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\timap.Connect(&quot;imap.example.com&quot;);    \/\/ or ConnectSSL for SSL\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\r\n\r\n\timap.SelectInbox();\r\n\tList&lt;long&gt; uids = client.Search(Flag.Unseen);\r\n\tif (uids.Count &gt; 0)\r\n\t\tclient.MarkMessageSeenByUID(uids&#x5B;0]);\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.Connect(&quot;imap.example.com&quot;)    ' or ConnectSSL for SSL\r\n\timap.UseBestLogin(&quot;user&quot;, &quot;password&quot;)\r\n\r\n\timap.SelectInbox()\r\n\tDim uids As List(Of Long) = client.Search(Flag.Unseen)\r\n\tIf uids.Count &gt; 0 Then\r\n\t\tclient.MarkMessageSeenByUID(uids(0))\r\n\tEnd If\r\n\timap.Close()\r\nEnd Using\r\n<\/pre>\n<p>If you are using <em>GetMessageByUID<\/em> to download email message or <em>GetHeadersByUID <\/em>to get only message headers, most IMAP servers will automatically mark such messages as seen. If it&#8217;s not expected behavior in your scenario, you can use <em>Peek* <\/em>methods: <em>PeekHeadersByUID<\/em>, <em>PeekMessageByUID<\/em>. Those methods in contrast to their <em>Get*<\/em> equivalents: <em>GetMessageByUID <\/em>and <em>GetHeadersByUID <\/em>do not set the Seen flag &#8211; <strong>emails are not marked as seen automatically<\/strong>.<\/p>\n<p>You can also easily <strong>mark message as unseen<\/strong> by using <em>Imap.MarkMessageUnseenByUID<\/em> method.<\/p>\n<p>Please note that it is not possible to mark messages as read using POP3 protocol. You can read more about <a href=\"\/blog\/pop3-vs-imap\">differences between IMAP and POP3<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;ll show how to mark emails as seen (read) using Mail.dll .NET IMAP library and IMAP protocol. The following code connects to an IMAP server, finds unique ids of all unseen email messages and marks the first one as seen. Internally IMAP server uses IMAP flag \\UNSEEN (Flag.Unseen) to remember which messages [&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],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-imap","tag-imap-component","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/389"}],"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=389"}],"version-history":[{"count":9,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":4586,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/389\/revisions\/4586"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}