Click here to Skip to main content
11,575,124 members (68,742 online)
Click here to Skip to main content

A POP3 Client in C# .NET

, 9 Feb 2004 CPOL 2M 41.6K 322
Rate this:
Please Sign up or sign in to vote.
A POP3 client in C# .NET for reading and processing emails (including attachments).

Introduction

I was asked some time ago to develop software which involved extracting bodies and subject lines from emails. "Humm ...", I thought, "connect to mail server on 110, send POP3 commands, receive data, sorted!". Indeed, at my first attempt it was a piece of cake: reading emails - no problem. Colleagues working at my company were evangelizing about what we could do: "Yeah mate, we can automatically process emails, no sweat".

Clients would then ask more questions: "can we send it in rich text or HTML?". "Yeah, sure we can!!". "What about processing them automatically?". "Hey - you're talking to the email kings!!". "What about processing multiple attachments, WAV's MP3's JPEG's?". "Ermmm ... can I get back to you on that ...". Wasn't as easy as I'd thought ...

The reason why I found it quite difficult to code initially was mainly due to how MIME is written and how extremely ugly it can look at first glance. Here's a sample, which contains two multipart blocks (I'll explain this later):

Received: by Mailserver
        id <A href="mailto:01C3EFF7.990BBDF0@TEST">01C3EFF7.990BBDF0@TEST</A>; Tue, 11 Feb 2003 17:02:00 -0000
Message-ID: <A href="mailto:2CB86919E23ED51191840060080C3DAE02320B76@MAILSERVER">2CB86919E23ED51191840060080C3DAE02320B76@MAILSERVER</A>
From: Desmond McCarter
To: testemail
Subject: FW: my subject
Date: Tue, 11 May 2003 17:01:59 -0000
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="----_=_NextPart_000_01C3EFF7.990BD65A"
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: text/plain;
        charset="iso-8859-1"
        
        
        
-----Original Message-----
From: Lisa Cleary [mailto:lisa@cleary.com]
Sent: 11 May 2003 16:17
To: 'Desmond McCarter'
Subject: RE: Test

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: application/vnd.ms-excel;
        name="test.xls"
Content-Transfer-Encoding: base64
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAEAAAAwQEAAAAAAAAA
EAAA/v///wAAAAD+////AAAAAL0BAAC+AQAAvwEAAMABAAD/////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////8J
CBAAAAYFAP4czQfJQAAABgEAAOEAAgCwBMEAAgAAAOIAAABcAHAADQAAV0ggU21pdGggTmV3cyAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEIAAgCwBGEBAgAAAMABAAA9AQQA
AQD8AJwAAgAOABkAAgAAABIAAgAAABMAAgAAAK8BAgAAALwBAgAAAD0AEgAXB6b/WC+gIzgAAQAA
AAEAWAJAAAIAAACNAAIAAAAiAAIAAAAOAAIAAQC3AQIAAADaAAIAAAAxABoAyAAAAP9/kAEAAAAA
.

MIME (Multipurpose Internet Mail Extensions): A quick and dirty guide

Data that is transferred over the Internet is sent as a collection of bytes (i.e. a collection of 8 bits). This information includes text files, CSVs or even JPEGs or movies. "Hey" you might say "you can't send binary data as a collection of bytes!". Yes you can, with a suitable encoding scheme: using the base 64 algorithm for example (check out the System.Convert.ToBase64String method in your .NET framework). This information (we're talking in email context) also includes the subject, body and forwarded items. For the client (sending the email) and the server (reading the email) should understand each other and, in order to do that, they must conform (send and receive data) in MIME format.

In the snipped MIME example (above), you can see and easily understand the basic fields:

"From:" - who sent the email, "To:" - who is receiving the email, "Subject:" - the subject of the email and "Date:" - the date/time the email was sent.

The "Content-Type:" determines what type of content the email contains. In a simple text email (i.e. with no attachments) this is normally "text/plain". You can see however (I hope you can anyway) that this email actually contains an attachment: test.xls. Emails that contain attachments have a MIME content type of "multipart/mixed". This means that the email contains data sectioned into multiple parts: the body and attachments (or in this case "attachment") etc. The boundary (boundary="----_=_NextPart_000_01C3EFF7.990BD65A") identifies where these parts start and stop. The body in my example (and in most emails) is the first part of this multipart email. The start of the body identified at the first boundary declaration:

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: text/plain;
        charset="iso-8859-1"




-----Original Message-----
From: Lisa Cleary [mailto:lisa@cleary.com]
Sent: 11 May 2003 16:17
To: 'Desmond McCarter'
Subject: RE: Test

You can also see from the above MIME text that this part also contains its content type, i.e. the format of the body: text/plain. All parts of a multipart email have their header definitions first, then an empty line, then the actual body.

The next part of this multipart email is the attachment:

------_=_NextPart_000_01C3EFF7.990BD65A
Content-Type: application/vnd.ms-excel;
        name="test.xls"
Content-Transfer-Encoding: base64

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAEAAAAwQEAAAAAAAAA
EAAA/v///wAAAAD+////AAAAAL0BAAC+AQAAvwEAAMABAAD/////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////8J
CBAAAAYFAP4czQfJQAAABgEAAOEAAgCwBMEAAgAAAOIAAABcAHAADQAAV0ggU21pdGggTmV3cyAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEIAAgCwBGEBAgAAAMABAAA9AQQA
AQD8AJwAAgAOABkAAgAAABIAAgAAABMAAgAAAK8BAgAAALwBAgAAAD0AEgAXB6b/WC+gIzgAAQAA
AAEAWAJAAAIAAACNAAIAAAAiAAIAAAAOAAIAAQC3AQIAAADaAAIAAAAxABoAyAAAAP9/kAEAAAAA
.

Note again, the second and final "multipart part" (i.e. the attachment) start off with the boundary declaration. Also note that the content type is defined, as well as the name and encoding scheme used to convert the attachment, enabling it to be sent over the internet in byte format. You need to take note that had this email had another attachment, then the second attachment (the third "multipart part") would start off with the same boundary declaration and so on.

MIME is in fact object oriented

The first mistake I made when building a POP3 library was to develop it in a language that was unsuitable: C. It took too long to write and did indeed get very dirty. It took about 3 weeks to develop and test my library, whereas in C# it took a day and a half!! The reason for this is that MIME, you can say, is an object oriented format: each part of a multipart email (even the body of a simple text/plain mail + main headers etc.) can be thought of as being objects. This is one of the main reasons why I wrote it in C# (could have used Java or even J2EE but ...).

Code

The code I have written starts off with a class called Pop3Client. This class is used to instantiate connection to a POP3 server:

Pop3Client email = new Pop3Client("user", "password", "mail.server.com");

You then open the Inbox as follows:

email.OpenInbox();

To go to the first email, then you call the NextEmail() method, which returns true if there is a "next email" or false if no such email exists. There is also a IsMultipart singleton, which you can use to check and see whether the email has multiple parts (i.e. attachments). Here's an example of how the code might look:

try {
    Pop3Client email = new Pop3Client("user", "password", "mail.server.com");
    email.OpenInbox();

    while( email.NextEmail())

    {
        if(email.IsMultipart)
        {
            IEnumerator enumerator = email.MultipartEnumerator;
            while(enumerator.MoveNext())
            {
                Pop3Component multipart = (Pop3Component)
                enumerator.Current;
                if( multipart.IsBody )
                {
                    Console.WriteLine("Multipart body:"+
                    multipart.Body);
                }
                else
                {
                    Console.WriteLine("Attachment name="+
                    multipart.Name); // ... etc
                }
            }
        }
    }

    email.CloseConnection();

}
catch(Pop3LoginException)
{
    Console.WriteLine("You seem to have a problem logging in!");
}

I have also implemented other functionalities within this class library which includes saving attachments (currently done automatically) in their original format, a getter for the filename, extension etc.

Have a look and see what you think: I definitely found it fun to write and explore!!

License

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

Share

About the Author

Desmond McCarter
Web Developer
United Kingdom United Kingdom
Des is a Technical Architect working for a private telecoms based company in the United Kingdom. He has been involved in programming for over 14 years and has worked on many platforms including UNIX, Linux and Windows.

Language specialities are C, C++, C#.NET, Java & J2EE and shell scripting (especially on UNIX/Linux). Also enjoys writing and optimising SQL scripts.

Des is engaged to a lovely girl called Lisa!

You may also be interested in...

Comments and Discussions

 
GeneralRe: Fail when retrieving attachment Pin
Varatharajan P.7-Oct-12 20:13
memberVaratharajan P.7-Oct-12 20:13 
QuestionPop3Client.Date Pin
jon.hayward6-Sep-12 3:21
memberjon.hayward6-Sep-12 3:21 
Questionsaving the attachment Pin
Member 849115428-Jun-12 19:20
memberMember 849115428-Jun-12 19:20 
Questionwhile (email.NextEmail()) fails Pin
Member 363466811-Jun-12 9:17
memberMember 363466811-Jun-12 9:17 
QuestionIs it possible to get the source of the email? Pin
shacky19-Mar-12 17:50
membershacky19-Mar-12 17:50 
GeneralMy vote of 4 Pin
XmjX15-Feb-12 19:28
memberXmjX15-Feb-12 19:28 
GeneralRe: My vote of 4 Pin
Member 788269815-Mar-12 10:51
memberMember 788269815-Mar-12 10:51 
Suggestionthanks for all Pin
alex211b9-Feb-12 22:06
memberalex211b9-Feb-12 22:06 
Questionheader remaining empty Pin
Conrad Cassar25-Jan-12 3:04
memberConrad Cassar25-Jan-12 3:04 
QuestionNot able to read autogenerated mail coming in mail box Pin
pavan_contractor14-Dec-11 19:08
memberpavan_contractor14-Dec-11 19:08 
GeneralMy vote of 5 Pin
SamNaseri5-Dec-11 17:48
memberSamNaseri5-Dec-11 17:48 
QuestionFor those who need to save, this is how you do it Pin
cofrari18-Nov-11 5:08
membercofrari18-Nov-11 5:08 
AnswerRe: For those who need to save, this is how you do it Pin
Member 849115428-Jun-12 21:54
memberMember 849115428-Jun-12 21:54 
AnswerRe: For those who need to save, this is how you do it Pin
idShura29-Nov-12 19:33
memberidShura29-Nov-12 19:33 
QuestionGreat code, but small issue with multiline message body Pin
Dougy836-Nov-11 22:35
memberDougy836-Nov-11 22:35 
QuestionHow to download attachments from defined letters Pin
olgofur23-Aug-11 3:16
memberolgofur23-Aug-11 3:16 
QuestionbyteCount getting 0 Pin
rishabhtoshniwal24-Jul-11 22:16
memberrishabhtoshniwal24-Jul-11 22:16 
QuestionRe: byteCount getting 0 Pin
kaempf8425-Jul-11 9:33
memberkaempf8425-Jul-11 9:33 
AnswerRe: byteCount getting 0 Pin
Member 788269815-Mar-12 10:38
memberMember 788269815-Mar-12 10:38 
GeneralRe: byteCount getting 0 Pin
Geigo16-Aug-12 5:57
memberGeigo16-Aug-12 5:57 
QuestionRead Unread Mail Pin
cool_cupid_jny1-Jul-11 1:37
membercool_cupid_jny1-Jul-11 1:37 
GeneralMy vote of 5 Pin
Mark Laird24-Jun-11 13:35
memberMark Laird24-Jun-11 13:35 
GeneralExtra characters like =3D or = at the end of line Pin
Rizwan Bashir20-Apr-11 20:20
memberRizwan Bashir20-Apr-11 20:20 
Generalgetting error while using this code in asp.net Pin
Rajat_Chanana16-Apr-11 5:06
memberRajat_Chanana16-Apr-11 5:06 
GeneralMy vote of 5 Pin
sampsonlau3-Apr-11 16:35
membersampsonlau3-Apr-11 16:35 
GeneralMy vote of 5 Pin
Sandeep Mewara25-Mar-11 21:12
mvpSandeep Mewara25-Mar-11 21:12 
QuestionHow to save attachement using POP3 Client in C# .NET? Pin
Member 375346328-Dec-10 14:39
memberMember 375346328-Dec-10 14:39 
QuestionHow to filter POP3 Email from Junk Mail ? Pin
hoi20-Oct-10 16:45
memberhoi20-Oct-10 16:45 
GeneralMy vote of 5 Pin
gokujames13-Oct-10 23:27
membergokujames13-Oct-10 23:27 
GeneralNeed Help Pin
raj405727-Sep-10 8:55
memberraj405727-Sep-10 8:55 
GeneralRe: Need Help Pin
lesnikowski4-Apr-13 1:48
memberlesnikowski4-Apr-13 1:48 
Questionif (!header.Substring(0, 3).Equals("+OK")) Pin
PrajeeshTest15-Sep-10 18:39
memberPrajeeshTest15-Sep-10 18:39 
GeneralMy vote of 5 Pin
PrajeeshTest15-Sep-10 3:07
memberPrajeeshTest15-Sep-10 3:07 
GeneralRe: My vote of 5 Pin
Member 788269815-Mar-12 10:52
memberMember 788269815-Mar-12 10:52 
QuestionFilename and Name - trying to save attachments Pin
benbawden13-Sep-10 6:55
memberbenbawden13-Sep-10 6:55 
AnswerRe: Filename and Name - trying to save attachments Pin
John LAWDeed16-Mar-11 2:59
memberJohn LAWDeed16-Mar-11 2:59 
GeneralAccess to "Date" or any other unique reference Pin
David Cresswell11-Aug-10 6:54
memberDavid Cresswell11-Aug-10 6:54 
GeneralMy vote of 5 Pin
biswajitcb29-Jul-10 3:22
memberbiswajitcb29-Jul-10 3:22 
GeneralMethod email.NextEmail() doesn't work Pin
Paulo Roussenq6-May-10 9:54
memberPaulo Roussenq6-May-10 9:54 
Generalattachment is not saving if mail is comming from gmail Pin
gulam_last2-May-10 21:24
membergulam_last2-May-10 21:24 
GeneralUpdated Code Pin
chris17516-Apr-10 8:41
memberchris17516-Apr-10 8:41 
GeneralRe: Updated Code Pin
testerbddd13-May-10 11:39
membertesterbddd13-May-10 11:39 
RantThis code needs updating... Pin
chris17516-Apr-10 3:21
memberchris17516-Apr-10 3:21 
GeneralVery helpfull Pin
Ravi J Patel7-Apr-10 2:09
memberRavi J Patel7-Apr-10 2:09 
GeneralMissing multipart boundary Pin
Raios26-Mar-10 6:46
memberRaios26-Mar-10 6:46 
GeneralNot getting attachment from other mailserver [modified] Pin
NitinMakwana24-Mar-10 3:05
memberNitinMakwana24-Mar-10 3:05 
Questionhow to run this demo in visual studio 2008? Pin
negozin17-Feb-10 12:45
membernegozin17-Feb-10 12:45 
AnswerRe: how to run this demo in visual studio 2008? Pin
PrajeeshTest15-Sep-10 3:27
memberPrajeeshTest15-Sep-10 3:27 
QuestionHow to save complete email in MSG or other format ? Pin
GJR10016-Feb-10 21:47
memberGJR10016-Feb-10 21:47 
QuestionWrong encoding for german language like Fr=FChester = Frühster (=FC) Pin
TheBigPunisher22-Jan-10 2:07
memberTheBigPunisher22-Jan-10 2:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 10 Feb 2004
Article Copyright 2004 by Desmond McCarter
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid