Click here to Skip to main content
15,920,896 members
Articles / Programming Languages / Visual Basic
Article

Simple POP3 Email Class

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
13 Mar 2007CPOL2 min read 247K   14.2K   84   67
A simple POP3 class to download emails from a mail server.
Screenshot - ScreenShot.jpg

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.

You declare the variables and create the objects, like so:

VB
Dim popConn As SamplePop3Class.POP3
Dim mailMess As SamplePop3Class.EmailMessage

'create the objects

popConn = New SamplePop3Class.POP3
mailMess = New SamplePop3Class.EmailMessage

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

VB
'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.

VB
'if we returned some messages, loop through each one and get the details

For i = 1 To 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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
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

Comments and Discussions

 
Generalcould u plz help me retrirve the body Pin
harsh nathani17-Jun-08 20:07
harsh nathani17-Jun-08 20:07 
GeneralRe: could u plz help me retrirve the body Pin
Luke Niland20-Jun-08 8:19
Luke Niland20-Jun-08 8:19 
GeneralUnable to write data to transport connection An Established Connection was Aborted by the software in your host machine Pin
oerslaafroze27-Nov-07 8:49
oerslaafroze27-Nov-07 8:49 
GeneralRe: Unable to write data to transport connection An Established Connection was Aborted by the software in your host machine Pin
Luke Niland30-Nov-07 3:21
Luke Niland30-Nov-07 3:21 
QuestionHave You Done Anything With Attachments? Pin
mycroft.xxx21-Nov-07 8:04
mycroft.xxx21-Nov-07 8:04 
AnswerRe: Have You Done Anything With Attachments? Pin
Luke Niland21-Nov-07 13:52
Luke Niland21-Nov-07 13:52 
QuestionPlease help me ~~tq Pin
edmond100226-Sep-07 4:03
edmond100226-Sep-07 4:03 
AnswerRe: Please help me ~~tq Pin
Luke Niland26-Sep-07 11:12
Luke Niland26-Sep-07 11:12 
Hi,

I'll try and answer your questions in here and not my blog as more people will probably see it!

1) In the form code I have purposely left the part that would delete the mail of the server commented out so if you just run the code it wont delete any mail of the server. To delete any mail you will have to tick the box on the form AND uncomment the popConn.MarkForDelete(i) line.
Also note that the mail is only marked for deletion at this point, it is actually deleted when you issue the QUIT command to the server, this is done by the popConn.CloseConn line right at the end.

When the program runs and gets mail of your server, it puts the entire content of the message into the strMailContent variable. This includes all the headers, mime types, too/from headers etc. The function called ParseBody then tries to extract only the body text out of the email, and stores it in the strBody variable. If you uncomment out the message box just after where the message gets added to the list you will see all the sections of the email.

2)This could prove tricky! The part of my class that attempts to get the body text out isn't brilliant, but at the time i was only dealing with simple messages. I dont really know that much about how you will get the Chinese chars out of the email within vb.net. You will probably have to do something clever regarding what character set you read the email as. You can send me an example email if you like and I can have a look but its not something I have ever dealt with before.



GeneralThis is great Pin
Sgt Mike3-Sep-07 10:32
Sgt Mike3-Sep-07 10:32 
GeneralRe: This is great Pin
Luke Niland24-Sep-07 5:51
Luke Niland24-Sep-07 5:51 
GeneralRe: This is great Pin
Sgt Mike25-Sep-07 1:14
Sgt Mike25-Sep-07 1:14 
QuestionNeed help Pin
kanzz2-Aug-07 19:50
kanzz2-Aug-07 19:50 
AnswerRe: Need help Pin
Luke Niland24-Sep-07 7:59
Luke Niland24-Sep-07 7:59 
QuestionIt is not working for me. Pleae help Pin
pgsr25-Jul-07 1:22
pgsr25-Jul-07 1:22 
AnswerRe: It is not working for me. Pleae help Pin
Luke Niland27-Jul-07 3:56
Luke Niland27-Jul-07 3:56 
Generalsome error found Pin
vankha24-Jun-07 22:28
vankha24-Jun-07 22:28 
GeneralRe: some error found Pin
Luke Niland25-Jun-07 11:54
Luke Niland25-Jun-07 11:54 
GeneralComplete POP3 / MIME solution on CodeProject Pin
Peter Huber SG21-Mar-07 22:57
mvaPeter Huber SG21-Mar-07 22:57 
GeneralRe: Complete POP3 / MIME solution on CodeProject Pin
Luke Niland22-Mar-07 9:18
Luke Niland22-Mar-07 9:18 
GeneralRe: Complete POP3 / MIME solution on CodeProject Pin
Peter Huber SG22-Mar-07 15:06
mvaPeter Huber SG22-Mar-07 15:06 
GeneralNice, but some errors Pin
pjreid19-Mar-07 5:32
pjreid19-Mar-07 5:32 
GeneralRe: Nice, but some errors Pin
Luke Niland20-Mar-07 4:13
Luke Niland20-Mar-07 4:13 
GeneralRe: Nice, but some errors Pin
DonBL8-Nov-07 11:55
DonBL8-Nov-07 11:55 
GeneralRe: Nice, but some errors Pin
Luke Niland11-Nov-07 8:22
Luke Niland11-Nov-07 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.