Click here to Skip to main content
15,867,568 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 243.8K   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

 
PraisePOP3 Reader Pin
Member 136075124-Jan-18 15:00
Member 136075124-Jan-18 15:00 
QuestionQuery Pin
Member 824170531-May-17 3:55
Member 824170531-May-17 3:55 
QuestionGetClientSocket() Pin
Member 900910024-Nov-16 1:07
Member 900910024-Nov-16 1:07 
QuestionCan we get unread messages only Pin
kasunthilina6-Jul-15 0:50
kasunthilina6-Jul-15 0:50 
Questionattachments? Pin
mhalonso7410-Mar-15 23:12
mhalonso7410-Mar-15 23:12 
AnswerRe: attachments? Pin
kasunthilina6-Jul-15 0:54
kasunthilina6-Jul-15 0:54 
Questiondoes this work in zimbra? Pin
Member 104358523-Jun-14 16:20
Member 104358523-Jun-14 16:20 
QuestionGood News!! Visual Studio now has support for POP3, SMTP, IMAP, etc. Pin
Tino Fourie21-Feb-14 10:00
Tino Fourie21-Feb-14 10:00 
Questionunread emails Pin
Member 104943835-Jan-14 0:51
Member 104943835-Jan-14 0:51 
GeneralMy vote of 5 Pin
Madness Fading21-May-13 17:40
Madness Fading21-May-13 17:40 
SuggestionThis is cool but I found more... Pin
scarterszoo9-Oct-12 17:05
scarterszoo9-Oct-12 17:05 
GeneralMy vote of 5 Pin
EricoCanales11-Sep-12 12:17
EricoCanales11-Sep-12 12:17 
QuestionDo you have it in C#? Pin
eveniza30-Jul-12 18:09
eveniza30-Jul-12 18:09 
Questionhey luke need help urgently Pin
akash89838-Apr-12 18:58
akash89838-Apr-12 18:58 
Questioni got this error Pin
zhtway8-Aug-11 12:56
zhtway8-Aug-11 12:56 
QuestionCan someone give me a sample for hotmail??? Pin
Steef43525-Jul-11 0:58
Steef43525-Jul-11 0:58 
Can somebody tell me what I must do if I want to get mail from the hotmail server. I already set the port to 995, but when I try to connect it says:

Unable to read data from the transport connection: [here comes a translated message, my os is dutch] The external host disconnected.

How do I get this working??? Or are the mails encrypted, so the program just can't read it?

Thanks!

Steef
AnswerRe: Can someone give me a sample for hotmail??? Pin
Luke Niland25-Jul-11 9:40
Luke Niland25-Jul-11 9:40 
Generalbase64 decoding Pin
Ramsin1-Jul-09 11:13
Ramsin1-Jul-09 11:13 
GeneralRe: base64 decoding Pin
Luke Niland9-Jul-09 8:06
Luke Niland9-Jul-09 8:06 
GeneralRe: base64 decoding Pin
jseph8816-Jan-13 16:04
jseph8816-Jan-13 16:04 
GeneralSSL/Pop3 Support in this Demo Pin
Luke Niland30-Mar-09 9:50
Luke Niland30-Mar-09 9:50 
GeneralA problem occured while decoding a base64 email. Pin
Yueming Wu22-Feb-09 1:44
Yueming Wu22-Feb-09 1:44 
GeneralRe: A problem occured while decoding a base64 email. Pin
Luke Niland22-Feb-09 5:28
Luke Niland22-Feb-09 5:28 
GeneralRe: A problem occured while decoding a base64 email. Pin
Yueming Wu22-Feb-09 15:39
Yueming Wu22-Feb-09 15:39 
GeneralRe: A problem occured while decoding a base64 email. Pin
Luke Niland23-Feb-09 8:12
Luke Niland23-Feb-09 8:12 

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.