Categories
PHP
Javascript
MySQL
C#
VB
VB.NET
ASP.NET
Regex
Packaging & compression
General Web Tech
Tech Speak


Google


This website looks best on firefox.
 
Resource Center : C# : <Accessing mail with Pop - Using C# and OpenPop>

Accessing mail with Pop - Using C# and OpenPop

Posted by: Floresense Team

Scenario: To read, download and parse mail from a pop3 server, with or without password, with and without a secure connection.

How To:

OpenPop is an open source C#.NET code bundle that implements mail fetching and parsing. As of this writing, it only uses Microsoft .NET framework libraries to do the required. But for accessing secure pop servers, openPop can be extended by using some SSL library.

For basic usage, on any pop server, without SSL, with or without authentication, you can just download OpenPop windows binaries (dlls), and follow their sample code.

For accessing SSL based pop, you have to connect to the pop server through a socket and SSL layer, rather than just a socket.. and since OpenPop's version (as of this writing) doesn't have implementation to do this, you have to do the following.

1. Download Open Pop's free sources.
2. Download Mentali's free SSL library or source files.
3. Extend Open Pop's PopClient class functionality to support usage of a SSL layer which will use the Org.mentalis namespace and classes from the Mentalis SSL library.

How I applied it:

I googled, and referrenced at a few websites.. all linked at the end of this post.

I first downloaded Open Pop's library and tried using it. It only works for non-ssl based simple pop servers. I wanted to access gmail.com pop which is a ssl pop and doesn't respond to any normal non-ssl requests.

Then, I downloaded mentalis's free ssl library. but the mentalis site clearly said it was meant only for .Net framework 1.1. I was using .NET framework 2.0. Though it should be backward compatible, I downloaded the sources for mentalis's library and compiled it on my machine.

There was only one problem at compilation.. a Class HMac had an ambiguity because of two conflicting declarations from two referrenced namespaces. I did a find-replace to make all HMac referrences point to the org.mentalis namespace's implementation of the class.

Found a modified OpenPop's PopClient.cs file here which implements changes required to allow ssl pop access through mentali's libraries. I replaced the original PopClient.cs class with the one on the linked page, opened OpenPop's Visual studio solution, to the OpenPop project I added a referrence to the new mentalis library that I had ready from a previous compilation. Compiled OpenPop sources now..

Now I have to just pick the required libraries and use it.

Libraries required:

1. OpenPop.dll (the new compiled one)
2. Org.Mentalis.Security.dll (the new compiled one)
3. MIMEParser.dll (from OpenPop downloads made.. no changes need to be made on this. This library implements email parsing, and load it to a class structure).

If you want, here is my compilation of the above libraries for .NET 2.0

I copy the dlls to my project's space, and use the below code to access gmail's pop successfully.

POPClient poppy = new POPClient();
poppy.Connect("pop.gmail.com", 995, true);
poppy.Authenticate(username@gmail.com, "password");
int Count = poppy.GetMessageCount();
if (Count > 0)
{
   for (int i = Count; i >= 1; i -= 1)
   {
     OpenPOP.MIMEParser.Message m = poppy.GetMessage(i, false);
     //use the parsed mail in variable 'm'
   }
}

Related Articles/Links :
1. openPop link
2. Why SSL is not implemented in openPOP - a forum message
3. Mentalis opensource ssl lib
4. pop3 successful try with openpop and mentalis ssl lib

If you want to try implement POP with your own code, you can learn stuff from here.

Socket tutorial for pop
Sample telnet call to access gmail pop
MSDN forum message on ssl http
SSLstream class of .net2


Advertisement

2005 - 2008 © Floresense.com