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!

Try Ruby online

Ruby has been getting a lot of praise for a while now, so I decided to give it a try. I was surprised to find so many resources already out there, and one I think worth mentioning is this website.

Ruby is a programming language from Japan (available at ruby-lang.org) which is revolutionizing the web. The beauty of Ruby is found in its balance between simplicity and power.

What’s cool about the Try Ruby! website is that it’s an interactive web-based Ruby shell. And on top of that, it also offers a quick 15-minute tutorial as you go along.

Here’s a screenshot:

Pretty cool, ey? Lastly, here’s an excellent help-and-documentation website, specifically for Ruby if you want to delve and read more on it.

Catch you later. =0)

Get a finalized spec as much as possible

If you develop software or web apps, you’ve most likely experienced working off of an incomplete spec.  I have, and it’s tremendously frustrating when all the work you’ve developed so far needs to be changed all of sudden because you followed the spec!

So here are some points to keep in my mind and push for before you start any kind of development:

  1. Make it clear to your client, whether it be an internal department or actual client, that you won’t be able to work on the project unless a finalized spec is delivered to you.  If they can’t give you one, wear the project manager hat and obtain it yourself by way of interviewing, document gathering, etc.
  2. If you have a spec and see any TBDs in the spec, write down questions that will assist your client help you finalize that portion of the spec.
  3. If the TBDs in the spec can’t be finalized or answered, i.e., the client doesn’t know what he/she wants, build a mock up or shell to show him/her, as it might help the client envision what he/she needs and most likely give you the answer you need.
  4. Give and allocate yourself enough development time for you to complete the project.  I personally add at least a week, on top of the time I believe I can complete it.  This way I have time to resolve bugs or unforseen issues, and…if I complete it earlier, then I look good.  =0)
  5. Most importantly, set the expectations of your customer – especially with regard to item #4.  Let them know what the end product will look like, functions it will provide, etc.  This is why building a mock up or showing the client a reference early in the development process is important.

There will be times where some – or maybe all – of these points may not apply, but try to push for them anyway.

Thanks to a good friend of mine, Dave Mercer, for making this weigh heavily in my mind!

Take care.

Add text highlighting in ASP.NET 2.0 GridView

I built an ASP.NET 2.0 web application [for the company I work for] that streamlined the deployment process to our production environment. It processed, tracked, archived, and notified automatically on each step of the deployment process.

So with that in mind, I’ll be showing you how to add text highlighting in a GridView; for example, a Date & Time column (i.e., red=overdue, green=still OK), which, at work, presented to the the technical group if a deployment was late or not. =0)

Here’s a screenshot:

Fig. 1.1 – Text highlighting in ASP.NET 2.0 GridView

Text highlighting in ASP.NET GridView

Adding this feature is quite easy. The first thing you’ll need to do is add a RowDataBound event in the GridView code:

<font size="2"><span style="color: #0000ff">&lt;</span><span style="color: #a31515">asp</span><span style="color: #0000ff">:</span><span style="color: #a31515">GridView</span> <span style="color: #ff0000">OnRowDataBound</span><span style="color: #0000ff">="GridView_RowDataBound"&gt;</span></font>

The second thing is add an ASP label within an ASP template field, like so:
<font size="2"><span style="color: #0000ff">&lt;</span><span style="color: #a31515">asp</span><span style="color: #0000ff">:</span><span style="color: #a31515">TemplateField</span> <span style="color: #ff0000">HeaderText</span><span style="color: #0000ff">="Date &amp; Time"</span>

            <span style="color: #ff0000">SortExpression</span></font><font size="2"><span style="color: #0000ff">="TargetDateTime"&gt;

</span>    <span style="color: #0000ff">&lt;</span><span style="color: #a31515">ItemTemplate</span></font><font size="2"><span style="color: #0000ff">&gt;

</span>        <span style="color: #0000ff">&lt;</span><span style="color: #a31515">asp</span><span style="color: #0000ff">:</span><span style="color: #a31515">Label</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">="lblTargetDateTime"</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">="server"</span>

                <span style="color: #ff0000">Text</span><span style="color: #0000ff">='</span><span style="background: #ffee62 none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">&lt;%</span># Eval("TargetDateTime") <span style="background: #ffee62 none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">%&gt;<span style="color: #0000ff"></span>'</span> </font><font size="2"><span style="color: #0000ff">/&gt;

</span>    <span style="color: #0000ff">&lt;/</span><span style="color: #a31515">ItemTemplate</span></font><font size="2"><span style="color: #0000ff">&gt;

</span><span style="color: #0000ff">&lt;/</span><span style="color: #a31515">asp</span><span style="color: #0000ff">:</span><span style="color: #a31515">TemplateField</span><span style="color: #0000ff">&gt;</span></font>

Pretty simple so far, ‘ey?  Now, to the last bread-and-butter step, the code-behind file:

<font size="2"><span style="color: #0000ff">using</span> System.Drawing;</font><font size="2">
</font><span style="color: #0000ff"><font size="2">private</font></span><font size="2"> <span style="color: #0000ff">void</span> GridView_RowDataBound(<span style="color: #0000ff">object</span> sender,

        <span style="color: #2b91af">GridViewRowEventArgs</span> e)

{

    </font><font size="2"><span style="color: #008000">// Find control to add text highlighting

</span>    <span style="color: #0000ff">if</span> (e.Row.RowType == <span style="color: #2b91af">DataControlRowType</span>.DataRow)

    {

        </font><font size="2"><span style="color: #008000">// Create Label datatype then cast and assign control

</span>        <span style="color: #2b91af">Label</span> dateTimeType =

            (<span style="color: #2b91af">Label</span>)e.Row.FindControl(<span style="color: #a31515">"lblTargetDateTime"</span>);</font>

<font size="2">
</font><font size="2">        </font><font size="2"><span style="color: #008000">// Instantiate new DateTime object

</span>        <span style="color: #2b91af">DateTime</span> dateTime = <span style="color: #0000ff">new</span> <span style="color: #2b91af">DateTime</span>();

        dateTime = <span style="color: #2b91af">Convert</span>.ToDateTime(dateTimeType.Text);</font>

<font size="2">
</font><font size="2">        </font><font size="2"><span style="color: #008000">// Set coloring based on date and time

</span>        <span style="color: #0000ff">if</span> (<span style="color: #2b91af">DateTime</span>.Compare(dateTime, <span style="color: #2b91af">DateTime</span>.Today) &gt;= 0)

            dateTimeType.BackColor = Color.LawnGreen;

        </font><font size="2"><span style="color: #0000ff">else

</span>            dateTimeType.BackColor = Color.Red;

    }

}</font>

That’s it! Using text highlighting in your GridView not only makes it look “pretty,” but also emphasizes a record’s importance based on colors.