Click here to Skip to main content
Click here to Skip to main content

Simple POP3 Email Class

By , 13 Mar 2007
 
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:

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.

'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 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)

About the Author

Luke Niland
United Kingdom United Kingdom
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMadness Fading9hrs 30mins ago 
SuggestionThis is cool but I found more...memberscarterszoo9 Oct '12 - 17:05 
GeneralMy vote of 5memberEricoCanales11 Sep '12 - 12:17 
QuestionDo you have it in C#?membereveniza30 Jul '12 - 18:09 
Questionhey luke need help urgentlymemberakash89838 Apr '12 - 18:58 
Questioni got this errormemberzhtway8 Aug '11 - 12:56 
AnswerRe: i got this errormemberMember 794829920 Feb '12 - 8:10 
QuestionCan someone give me a sample for hotmail???memberSteef43525 Jul '11 - 0:58 
AnswerRe: Can someone give me a sample for hotmail???memberLuke Niland25 Jul '11 - 9:40 
Generalbase64 decodingmemberRamsin1 Jul '09 - 11:13 
GeneralRe: base64 decodingmemberLuke Niland9 Jul '09 - 8:06 
GeneralRe: base64 decodinggroupjseph8816 Jan '13 - 16:04 
GeneralSSL/Pop3 Support in this DemomemberLuke Niland30 Mar '09 - 9:50 
GeneralA problem occured while decoding a base64 email.memberYueming Wu22 Feb '09 - 1:44 
GeneralRe: A problem occured while decoding a base64 email.memberLuke Niland22 Feb '09 - 5:28 
GeneralRe: A problem occured while decoding a base64 email.memberYueming Wu22 Feb '09 - 15:39 
GeneralRe: A problem occured while decoding a base64 email.memberLuke Niland23 Feb '09 - 8:12 
QuestionRe: A problem occured while decoding a base64 email.groupjseph8816 Jan '13 - 16:02 
Hi,
 
Have u fix the problem that occurs decoding base 64 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.
 
Thank you.
QuestionIt's not working with GMAIL. Can anyone help me? It could be a SSL problem?memberitamar.locatelli@gmail.com28 Jan '09 - 3:19 
AnswerRe: It's not working with GMAIL. Can anyone help me? It could be a SSL problem?memberLuke Niland2 Feb '09 - 8:23 
GeneralRe: It's not working with GMAIL. Can anyone help me? It could be a SSL problem?memberw3rm24 Mar '09 - 6:08 
GeneralRe: It's not working with GMAIL. Can anyone help me? It could be a SSL problem?memberLuke Niland25 Mar '09 - 7:41 
GeneralRe: It's not working with GMAIL. Can anyone help me? It could be a SSL problem?membercstrader23227 Mar '09 - 2:54 
QuestionHow to read attachment's content from emailmemberbalamurugan kalyanasundaram1 Sep '08 - 3:40 
AnswerRe: How to read attachment's content from emailmemberLuke Niland7 Sep '08 - 1:32 
QuestionQuestion on Message bodymembertnyatsvimbo@yahoo.com10 Aug '08 - 23:19 
AnswerRe: Question on Message bodymemberLuke Niland13 Aug '08 - 11:39 
GeneralRe: Question on Message bodymembertnyatsvimbo@yahoo.com13 Aug '08 - 22:05 
GeneralRe: Question on Message bodymemberLuke Niland14 Aug '08 - 6:40 
GeneralHere is a samplemembertnyatsvimbo@yahoo.com15 Aug '08 - 1:54 
GeneralRe: Here is a samplememberLuke Niland25 Aug '08 - 12:36 
GeneralThanksmembertnyatsvimbo@yahoo.com25 Aug '08 - 20:16 
GeneralRe: ThanksmemberLuke Niland7 Sep '08 - 1:28 
GeneralFeedbackmembertnyatsvimbo@yahoo.com7 Sep '08 - 4:21 
GeneralRe: ThanksmemberFletch9978 Apr '11 - 20:36 
Generalcould u plz help me retrirve the bodymemberharsh nathani17 Jun '08 - 20:07 
GeneralRe: could u plz help me retrirve the bodymemberLuke Niland20 Jun '08 - 8:19 
GeneralUnable to write data to transport connection An Established Connection was Aborted by the software in your host machinememberoerslaafroze27 Nov '07 - 8:49 
GeneralRe: Unable to write data to transport connection An Established Connection was Aborted by the software in your host machinememberLuke Niland30 Nov '07 - 3:21 
QuestionHave You Done Anything With Attachments?membermycroft.xxx21 Nov '07 - 8:04 
AnswerRe: Have You Done Anything With Attachments?memberLuke Niland21 Nov '07 - 13:52 
QuestionPlease help me ~~tqmemberedmond100226 Sep '07 - 4:03 
AnswerRe: Please help me ~~tqmemberLuke Niland26 Sep '07 - 11:12 
GeneralThis is greatmemberSgt Mike3 Sep '07 - 10:32 
GeneralRe: This is greatmemberLuke Niland24 Sep '07 - 5:51 
GeneralRe: This is greatmemberSgt Mike25 Sep '07 - 1:14 
QuestionNeed helpmemberkanzz2 Aug '07 - 19:50 
AnswerRe: Need helpmemberLuke Niland24 Sep '07 - 7:59 
QuestionIt is not working for me. Pleae helpmemberpgsr25 Jul '07 - 1:22 
AnswerRe: It is not working for me. Pleae helpmemberLuke Niland27 Jul '07 - 3:56 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 13 Mar 2007
Article Copyright 2007 by Luke Niland
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid