{"id":142,"date":"2014-09-08T10:14:39","date_gmt":"2014-09-08T10:14:39","guid":{"rendered":"http:\/\/www.danieledavi.com\/blog\/?p=142"},"modified":"2014-09-05T16:30:55","modified_gmt":"2014-09-05T16:30:55","slug":"how-to-read-a-pop3-server-mail-folder-with-c","status":"publish","type":"post","link":"https:\/\/www.danieledavi.com\/blog\/2014\/09\/how-to-read-a-pop3-server-mail-folder-with-c\/","title":{"rendered":"How to read a Pop3 server mail folder with C#"},"content":{"rendered":"<p>Let&#8217;s see how to read emails from a pop3 Server.<\/p>\n<p>After setting some variables, we&#8217;ll create a connection to the Pop3 Server and we&#8217;ll try to autenticate with user and password. After we&#8217;ll take a bunch of messages and we&#8217;ll start to iterate some operation per each message.<\/p>\n<p><div id=\"attachment_140\" style=\"width: 310px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-140\" src=\"http:\/\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image.png\" alt=\"C Sharp\" width=\"300\" height=\"300\" class=\"size-full wp-image-140\" srcset=\"https:\/\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image.png 300w, https:\/\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image-150x150.png 150w, https:\/\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image-50x50.png 50w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-140\" class=\"wp-caption-text\">C#<\/p><\/div>The message can be cleaned, processed, saved or used in any way you want.<br \/>\nAfter we&#8217;ll delete the message from the server and we&#8217;ll disconnect the user.<br \/>\nTo do this we&#8217;ll use an external library called IndipendentSoft.Email so at the beginning of the file we&#8217;ll include these 2 lines:<\/p>\n<pre>using Independentsoft.Email.Pop3;\r\nusing Independentsoft.Email.Mime;<\/pre>\n<p>Now we can write code we need.<\/p>\n<pre>public int checkPop3()\r\n{\r\n\tWriteLog(\"- checkPop3 START\");\r\n\tPop3Server = rootkey.GetValue(\"POP3Server\").ToString();\r\n\tPop3User = rootkey.GetValue(\"POP3User\").ToString();\r\n\tPop3Pass = \"your-password\"\r\n\r\n\tint myResult = 0;\r\n\tint myStep = 0;\r\n\tint messageIndex = 0;\r\n\tint mymaxdelfor = 20; \/\/how many messages to read per each connection\r\n\tPop3Client client = null;\r\n\r\n\tIndependentsoft.Email.Mime.Message myMessage = null;\r\n\tString myCode = \"\";\r\n\tmyStep = 1;\r\n\tclient = new Pop3Client(Pop3Server);\r\n\ttry\r\n\t{\r\n\t\tclient.Connect();\r\n\t\tWriteLog(\"Conn POP3: OK\");\r\n\t}\r\n\tcatch (Exception myEx)\r\n\t{\r\n\t\tWriteLog(\"Conn POP3: KO\" + myEx.ToString());\r\n\t\treturn 0;\r\n\t}\r\n\ttry\r\n\t{\r\n\t\tclient.Login(Pop3User, Pop3Pass);\r\n\t\tWriteLog(\"Login POP3: OK\");\r\n\t}\r\n\tcatch (Exception myEx2)\r\n\t{\r\n\t\tWriteLog(\"Login POP3: KO\" + myEx2.ToString());\r\n\t\treturn 0;\r\n\t}\r\n\tMessageInfo[] messageInfo;\r\n\ttry\r\n\t{\r\n\t\tmessageInfo = client.List();\r\n\t\tWriteLog(\"List POP3: OK\");\r\n\t}\r\n\tcatch (Exception myEx3)\r\n\t{\r\n\t\tWriteLog(\"List POP3: KO\" + myEx3.ToString());\r\n\t\tthrow;\r\n\t}\r\n\t\r\n\tmyStep = 2;\r\n\tif (messageInfo.Length > 0)\r\n\t{\r\n\t\tWriteLog(\"MSG to read: \" + messageInfo.Length);\r\n\t\tmyStep = 3;\r\n\t\tif (messageInfo.Length < mymaxdelfor)\r\n\t\t{\r\n\t\t\tmymaxdelfor = messageInfo.Length;\r\n\t\t}\r\n\r\n\t\tfor (int i = 0; i < mymaxdelfor; i++)\r\n\t\t{\r\n\t\t\tWriteLog(\"MSG n.\" + messageIndex.ToString());\r\n\t\t\tmessageIndex = messageInfo[i].Index;\r\n\t\t\tmyMessage = client.GetMessage(messageIndex);\r\n\t\t\tmyStep = 4;\r\n\t\t\t\r\n\t\t\tDoYourThings();\r\n\t\t\t...\r\n\t\t\tmyResult = \"dependsOnYourThings\";\r\n\t\t\t...\r\n\t\t\t\r\n\t\t}\r\n\t\t\tclient.Delete(messageIndex);\r\n\t}\r\n\t\tclient.Disconnect();\r\n\t\tWriteLog(\"DISCONNECT POP3 OK\");\r\n\t\r\n\tWriteLog(\"- checkPop3 END\");\r\n\treturn myResult;\r\n}\r\n<\/pre>\n<p>The number of messages to read in a box, can be very high, even hundreds of thousands and not just because of spam but according to the application and email purpose.<br \/>\nSo in time I experienced that for high volumes and big size emails it is better not to read all the messages. So I choose a maximum (20) per each connection.<\/p>\n<p>Of course you can read the pop3 according to some events or call your function every some minutes according to a timer.<\/p>\n<p>Enjoy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s see how to read emails from a pop3 Server. After setting some variables, we&#8217;ll create a connection to the Pop3 Server and we&#8217;ll try to autenticate with user and password. After we&#8217;ll take a bunch of messages and we&#8217;ll start to iterate some operation per each message. The message can be cleaned, processed, saved [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[12],"tags":[50,49,53,51,52,48,44],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p90hsv-2i","jetpack-related-posts":[{"id":138,"url":"https:\/\/www.danieledavi.com\/blog\/2014\/09\/how-to-use-windows-registry-in-your-net-c-application\/","url_meta":{"origin":142,"position":0},"title":"How to use Windows registry in your .Net C# application","author":"Daniele Dav\u00ec","date":"September 5, 2014","format":false,"excerpt":"In this article I will show how to read and use in your .Net C# application informations stored in\u00a0the Windows registry. First of all let's see the easiest way to store some information in the server registry. You can use the \"regedit\" command to edit the register or you can\u2026","rel":"","context":"In &quot;.Net C#&quot;","block_context":{"text":".Net C#","link":"https:\/\/www.danieledavi.com\/blog\/category\/programming\/dot-net-c-sharp\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/09\/csharp-image.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":45,"url":"https:\/\/www.danieledavi.com\/blog\/2013\/10\/server-antivirus-for-free-win2008-r2\/","url_meta":{"origin":142,"position":1},"title":"Server antivirus for free win2008 r2","author":"Daniele Dav\u00ec","date":"October 10, 2013","format":false,"excerpt":"I saw around a lot of unsolved forum's posts asking basically the same question: \"Free antovirus are not for free for commercial use or aren't working on servers. Can anyone find me someone to install?\" Here the solution I tried for my win2008 r2 server: Microsoft Security Essentials (here the\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.danieledavi.com\/blog\/category\/programming\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2013\/10\/Windows-Server-2008-300x225.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":308,"url":"https:\/\/www.danieledavi.com\/blog\/2017\/08\/android-toast-equivalent-in-ios\/","url_meta":{"origin":142,"position":2},"title":"Android Toast equivalent in iOS","author":"Daniele Dav\u00ec","date":"August 5, 2017","format":false,"excerpt":"If you wonder what the Java Toast equivalent of this iOS Objective C event would be than you are in the right place. Below is a sample of what I have written for iOS and Xamarin.iOS. This behave pretty much the\u00a0same as Alert in Java using a Toast in place\u2026","rel":"","context":"In &quot;.Net C#&quot;","block_context":{"text":".Net C#","link":"https:\/\/www.danieledavi.com\/blog\/category\/programming\/dot-net-c-sharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":131,"url":"https:\/\/www.danieledavi.com\/blog\/2014\/08\/html-parser-in-classic-asp\/","url_meta":{"origin":142,"position":3},"title":"HTML Parser in Classic ASP","author":"Daniele Dav\u00ec","date":"August 28, 2014","format":false,"excerpt":"Today I'll show you a script written in classic ASP that can use some server vulnerability to provide traffic from an unaware website to another one through another unaware server. It's quite simple, so I'm not going to explain it in details. This malicious code was founded on a server,\u2026","rel":"","context":"In &quot;Classic ASP&quot;","block_context":{"text":"Classic ASP","link":"https:\/\/www.danieledavi.com\/blog\/category\/programming\/classic-asp\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2014\/08\/AspClassic.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":767,"url":"https:\/\/www.danieledavi.com\/blog\/2021\/03\/protect-your-work-from-home-space-from-toxicity\/","url_meta":{"origin":142,"position":4},"title":"Protect your work from home space from toxicity","author":"Daniele Dav\u00ec","date":"March 7, 2021","format":false,"excerpt":"A year ago, many countries took the first measures to tackle the emerging pandemic due to COVID-19. Lockdown, working from home, social distancing...Experts aren't certain if we'll be back to normality by 2022 or we will just prolong current habits and lifestyle to establish a new norm.\u00a0Will companies let employees\u2026","rel":"","context":"In &quot;Business&quot;","block_context":{"text":"Business","link":"https:\/\/www.danieledavi.com\/blog\/category\/business\/"},"img":{"alt_text":"Toxic Waste","src":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2021\/03\/Toxic_waste.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2021\/03\/Toxic_waste.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2021\/03\/Toxic_waste.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":175,"url":"https:\/\/www.danieledavi.com\/blog\/2017\/05\/spam-how-everything-started\/","url_meta":{"origin":142,"position":5},"title":"SPAM: how everything started","author":"Daniele Dav\u00ec","date":"May 24, 2017","format":false,"excerpt":"\u00a0 The year was 1994. The Internet was young and there were a lot of clever people trying to figure out what to do with this new medium and exploring the possibilities that it opened up. In particular, there was a pair of attorneys in Arizona, Laurence Canter and Martha\u2026","rel":"","context":"In &quot;Courses Notes&quot;","block_context":{"text":"Courses Notes","link":"https:\/\/www.danieledavi.com\/blog\/category\/courses-notes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.danieledavi.com\/blog\/wp-content\/uploads\/2017\/05\/6a00d83452d6c969e200e54f71aa398833-800wi-264x300.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/posts\/142"}],"collection":[{"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danieledavi.com\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}