Introduction

This is a simple class written in VB.NET that allows you to download email from a POP3 mail server. It includes functions for extracting who the mail is for, who it is from, the subject, and the email body. As it stands, there is no support for handling attachments.

Background

When I started to work with the .NET framework, one of the first things I noticed was its lack of support for downloading emails from a mail server. There is very good support for sending mail via the SMTP protocol, but nothing for receiving. The class came about when I needed a way of retrieving log files that were sent as emails and acting on the information contained in the mail.

Using the code

There are two main classes you can use. The first one is called POP3, and is used to connect to the POP3 server and deal with all commands you have to issue in order to retrieve mail. The second class is called EmailMessage, and is used to extract all the different sections out of messages.

Once you have created the objects, connect to the mail server and find out how many messages (if any) are on the server.

'if we have got to this point, try and connect to the server
popConn.POPConnect(strMailServeor, strUsername, strPassword)
'now we have a connection, see if there are any mails on the server
intMessCnt = popConn.GetMailStat()

Now, the variable intMessCnt will contain how many messages are on the server. If it is greater then 0, loop through each of the messages and extract the sections of the email.

'if we returned some messages, loop through each one and get the detailsFor i = 1To intMessCnt
'load the entire content of the mail into a string
strMailContent = popConn.GetMailMessage(i)
'call the functions to get the various parts out of the email
strFrom = mailMess.ParseEmail(strMailContent, "From:")
strSubject = mailMess.ParseEmail(strMailContent, "Subject:")
strToo = mailMess.ParseEmail(strMailContent, "To:")
strBody = mailMess.ParseBody()
next i

Now, you should have all the sections of the email held in your variables. From here, you could save the email to an external file, insert the details into a database, or do whatever you want with the information.

Points of interest

This was the first time I have ever tried to write an application that involved communicating over TCP/IP. I was very surprised how easy and powerful it is, the hardest part was making sure I handled any errors coming back from the POP3 server correctly, but as everything coming back into an IOStream, it was very easy to read.

The only problem with this code as it stands is its lack of support for attachments. I have looked into it and it does seam very complex, but I am going to have a go at it and will update this code once I have it working.

Please note this is my first posting, so if anyone has any pointers on how to improve my article, let me know!

Share

About the Author

I currently work at for a newspaper group in Lancashire(U.K) in the IT dept. I deal with a quite a few different IT system on a day to day basis. My programming experience includes vb6, asp.net, sql and some C++. Check my blog out at beakersoft.co.uk.

I have also recently started writing software with a couple of guys i work with, check out our website at www.we3soft.com

As one of many developers I was looking for a way to retrieve mail via POP3 (with or without SSL) and I came across a few solutions here on Code Project.

However, one was purely SSL and badly documented while other only addressed connection to a POP3 server. Unfortunately most solutions written in VB could not address attachments and no further development occured after a few promises was made.

It seems that MS did address the lack for a wide variety of Mail functionality, not only SMTP. I only downloaded the expansion pack now and haven't tried any of it yet. Hopefully this will help many of us in the near future.

i got an error
A connection attept failed because the connected party did not properly respond after a period of time, or established connection failed bcoz connected has failed to respond 173.194.79.108:110
plz hhelp me soon

I'm interested in your code so I was reviewing but then I ran into one issue and will appreciate it if you give me some help on it. Most of the time I get an error as "A Problem occurred while decoding a base64 email".

I got that error, when an email has attachment(s). And this is the error:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.

Anyway, can you let me know how to retrieve attachment(s) using that code.

Quite a few people have asked if this demo supports pop3 over ssl, in its current state the answer to this question is no, so if you are trying to download of gmail or anything over port 995 (the normal secure pop3 port) its not going to work.

I've had a quick look around and found an open project at http://www.mentalis.org/soft/projects/ssocket/

Look like it should be possible to mod this demo (or create a new one) using this library to add ssl support. If anyone does it let me know and ill post a link here to it, or when i get round to it i'll have a go

I checked it more after I asked that question. It does not the trouble with the character set. The error occuried when there is a picture embeded in the email. I believe you are finding the way to fix this now. If you still want me to send you that email, please let us know. The problem is that I can not find your email address.

I got that error, when an email has attachment(s). And this is the error:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.

Anyway, can you let me know how to retrieve attachment(s) using that code.