Saturday, July 11, 2009

C# POP3 Library with SSL for Gmail

So I ended up searching for hours trying to find a free C# library to do POP3 access to a SSL server like gmail. In our company we use Google Apps and we like gmail's mail server, it would be nice to be able to have programmatical access to these mail accounts. How does this not exist?

So this solution takes OpenPop.net Open source library and makes some very small tweaks so it has Ssl support.

Here is the important simple code snippet.
Stream actualstream = clientSocket.GetStream();

if (useSSL) {
System.Net.Security.SslStream sslstream = new SslStream(clientSocket.GetStream());
sslstream.AuthenticateAsClient(strHost);
actualstream = sslstream;
}


Attached here is the re zip of the source code with the change to the library and also a tweak to the sample app with a checkbox to include SSL support.

OpenPOP with SSL Source Download

OpenPOP with SSL Binaries Download

Enjoy!

9 comments:

Carlos Jacobs said...

You links for re zip don't work

Justin Beck said...

Thanks for letting me know about the broken links, they should be fixed now. Happy Emailing!

Unknown said...

Thanks alot from Germany

sriram said...

Thanks its working for gmail . How to connect to my companies exchange server . i am getting

"The handshake failed due to an unexpected packet format."

Justin Beck said...

Hey Sriram,

Sorry, I don't know exactly what causes that error: Here is an article I found, if you find out what the fix is if you could post it here that would be great and I might even be able to change my library to include it

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23770445.html

http://social.msdn.microsoft.com/Forums/en/ncl/thread/e8807c4c-72b6-4254-ae64-45c2743b181e

Unknown said...

Hey!

Nice work!I've managed to access my gmail account with it. There is one problem which i can not solve.
I can only check the unread messages. How can i access the read ones? Or the sent ones?
Cheers,
ati

Justin Beck said...

Hey Sdf,

As far as reading the read mail. This is how gmail implemented the pop3 interface to their mail server.

I did find there is a setting in gmail for whether or not accessing it. I don't know if it actually helps though.

http://www.justinkbeck.com/random_images/pop_gmail.png

Justin

Unknown said...

Well its not about that setting, because it was already enabled.

Your program works well but once an email is retrieved it will not be retrieved anymore.

That's how i meant that i can only access the unread emails but not the read or sent etc ones.

Unknown said...

sdf - that is how the GMail POP works... they automatically "remove" the messages from server for you... when I say "remove" I mean they are not accessible via POP after that... I noticed this behavior a few years ago when I was using POP to get my GMail via e-mail clients on different computers. IMAP is probably the "easiest" way to do what you want, but don't know if there is a free/open source class out there that will do IMAP.