How to POP3 in C#

Here’s an excellent how-to on using POP3 in your C# apps.  I’ve used it successfully.

<font face="Verdana" color="#000000" size="2">The Pop3 class derives from the System.Net namespace:</font>
<font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Pop3</span> : System.Net.Sockets.<span style="color: rgb(43,145,175)">TcpClient</span></font>
<font size="2"><span style="color: rgb(43,145,175)"></span></font>&nbsp;
<font size="2"><font face="Courier New"><span style="color: rgb(43,145,175)"><font face="Verdana" color="#000000" size="2">And the class consists of the following fields and methods:</font></span></font></font>
<span style="color: rgb(0,0,255)"><span style="color: rgb(0,0,255)"><font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">ArrayList</span> List()</font></span></span>
<span style="color: rgb(0,0,255)"><font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">class</span> <span style="color: rgb(43,145,175)">Pop3Message</span></font></span>
<font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">void</span> Connect(<span style="color: rgb(0,0,255)">string</span> server, <span style="color: rgb(0,0,255)">string</span> username, <span style="color: rgb(0,0,255)">string</span> password)</font>
<font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">void</span> Disconnect()</font>
<font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(43,145,175)">Pop3Message</span> Retrieve(<span style="color: rgb(43,145,175)">Pop3Message</span> rhs)</font>
<font size="2"><span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">void</span> Delete(<span style="color: rgb(43,145,175)">Pop3Message</span> rhs)</font>
<font size="2"><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">void</span> Write(<span style="color: rgb(0,0,255)">string</span> message)</font>
<font size="2"><span style="color: rgb(0,0,255)">private</span> <span style="color: rgb(0,0,255)">string</span> Response()</font>
<font size="2"></font>&nbsp;
<font face="Verdana" color="#000000" size="2">Finally, below is a code snippet on how to use the Pop3 class:</font>
<font size="2"><span style="color: rgb(0,0,255)">static</span> <span style="color: rgb(0,0,255)">void</span> Main(<span style="color: rgb(0,0,255)">string</span>[] args)
{
    </font><font size="2"><span style="color: rgb(0,0,255)">try
</span>    {
        Pop3 obj = <span style="color: rgb(0,0,255)">new</span> Pop3();
        obj.Connect(<span style="color: rgb(163,21,21)">"mail.xxx.com"</span>, <span style="color: rgb(163,21,21)">"yyy"</span>, <span style="color: rgb(163,21,21)">"zzz"</span>);
        <span style="color: rgb(43,145,175)">ArrayList</span> list = obj.List();
        <span style="color: rgb(0,0,255)">foreach</span> (Pop3Message msg <span style="color: rgb(0,0,255)">in</span> list)
        {
            Pop3Message msg2 = obj.Retrieve(msg);
            System.<span style="color: rgb(43,145,175)">Console</span>.WriteLine(<span style="color: rgb(163,21,21)">"Message {0}: {1}"</span>,
                msg2.number, msg2.message);
        }
        obj.Disconnect();
    }
    <span style="color: rgb(0,0,255)">catch</span> (Pop3Exception e)
    {
        System.<span style="color: rgb(43,145,175)">Console</span>.WriteLine(e.ToString());
    }
    <span style="color: rgb(0,0,255)">catch</span> (System.<span style="color: rgb(43,145,175)">Exception</span> e)
    {
        System.<span style="color: rgb(43,145,175)">Console</span>.WriteLine(e.ToString());
    }
}</font>

Nifty ey?  So check out the how-to article.  Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *