{"id":978,"date":"2010-07-06T17:00:42","date_gmt":"2010-07-06T15:00:42","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=978"},"modified":"2023-11-07T09:18:08","modified_gmt":"2023-11-07T07:18:08","slug":"peek-message-on-imap-server","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/peek-message-on-imap-server","title":{"rendered":"Peek message on IMAP server"},"content":{"rendered":"\n<p>If you are using <em>GetMessageByUID<\/em> to download email message or <em>GetHeadersByUID<\/em> to get email headers only, most IMAP servers will automatically mark such messages as seen. <\/p>\n\n\n\n<p>In other words IMAP server adds <strong>\\SEEN<\/strong> flag to the messages you have just downloaded.<\/p>\n\n\n\n<p>If it&#8217;s not expected behavior in your scenario, you can use one of the <em>Peek* <\/em>methods, such as: <em>PeekHeadersByUID<\/em>, <em>PeekMessageByUID<\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using Peek methods<\/h2>\n\n\n\n<p>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\n\n\n<p>The sample below connects to IMAP server and finds all unseen messages, then it downloads them, but does not change their unseen status. Messages are left unseen.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nusing(Imap imap = new Imap())\n{\n    imap.ConnectSSL(&quot;imap.example.com&quot;);\n    imap.UseBestLogin(&quot;user&quot;, &quot;password&quot;);\n\n    imap.SelectInbox();\n    List&lt;long&gt; uidList = imap.Search(Flag.Unseen);\n    foreach (long uid in uidList)\n    {\n        IMail email = new MailBuilder()\n            .CreateFromEml(imap.PeekMessageByUID(uid));\n\n        string subject = email.Subject;\n        string text = email.Text;\n    }\n    imap.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 imap As New Imap\n    imap.ConnectSSL(\"imap.example.com\")\n    imap.UseBestLogin(\"user\", \"password\")\n\n    imap.SelectInbox()\n    Dim uidList As List(Of Long) = imap.Search(Flag.Unseen)\n\n    For Each uid As Long In uidList\n        Dim email As IMail = New MailBuilder() _\n            .CreateFromEml(imap.PeekMessageByUID(uid))\n\n        Dim subject As String = email.Subject\n        Dim text As String = email.Text\n    Next\n    imap.Close()\nEnd Using\n<\/pre><\/div>\n\n\n<p>There are <em>Peek<\/em> methods for downloading headers only (<em>PeekHeadersByUID<\/em>) and even <a href=\"https:\/\/www.limilabs.com\/blog\/download-parts-of-email-message\" title=\"Download parts of email message\">parts of the email message<\/a> (<em>PeekTextByUID<\/em>\u00a0, <em>PeekDataByUID<\/em>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examine instead of Select<\/h2>\n\n\n\n<p>The second approach is to use <em>Examine <\/em>method instead of <em>Select<\/em>. Examine puts the folder into read-only state, so no \\SEEN flag is added to any message. <\/p>\n\n\n\n<p>The drawback is that you can not delete or upload any message.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/ C#\n\nusing(Imap imap = new Imap())\n{\n    imap.ConnectSSL(\"imap.example.com\");\n    imap.UseBestLogin(\"user\", \"password\");\n\n    imap.ExamineInbox(); \/\/ -or- imap.Examine(\"Inbox\");\n\n    \/\/ ...\n\n    imap.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 imap As New Imap\n    imap.ConnectSSL(\"imap.example.com\")\n    imap.UseBestLogin\"user\", \"password\")\n\n    imap.ExamineInbox() ' -or- imap.Examine(\"Inbox\")\n\n    ' ...\n\n    imap.Close()\nEnd Using\n<\/pre><\/div>\n\n\n<p>You can also easily <a href=\"\/blog\/mark-emails-as-read-with-imap\">mark message as seen<\/a> and unseen by using <em>Imap.MarkMessageUnseenByUID<\/em> and <em>Imap.MarkMessageSeenByUID<\/em> methods.<\/p>\n\n\n\n<p>You can download <a href=\"\/mail\">Mail.dll IMAP library for .NET here<\/a>.<\/p>\n\n\n\n<br \/>\n<a class=\"btn btn-primary btn-largest btn-action\" href=\"\/mail\/download\">Get Mail.dll<\/a>\n<br \/>\n","protected":false},"excerpt":{"rendered":"<p>If you are using GetMessageByUID to download email message or GetHeadersByUID to get email headers only, most IMAP servers will automatically mark such messages as seen. In other words IMAP server adds \\SEEN flag to the messages you have just downloaded. If it&#8217;s not expected behavior in your scenario, you can use one of the [&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-978","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\/978"}],"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=978"}],"version-history":[{"count":6,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/978\/revisions"}],"predecessor-version":[{"id":6593,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/978\/revisions\/6593"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}