Format Your Brain

OpenPop.NET Helpful Tips

I have just worked on a project where I needed to download e-mails from a POP3 server. I research some of the libraries out there and settled on OpenPop.NET. OpenPop.NET has a great set of examples of connecting to a POP3 server, but what I want to show you below are some examples that OpenPop.NET doesn’t go over or elaborate on.

E-mail Addresses

There are four potential e-mail addresses that you can grab.

  1. To – who the message is to (multiple entries)
  2. From – who sent the message
  3. CC – who was carbon copied on the message (multiple entries)
  4. BCC – who was blind carbon copied on the message (multiple entries)

The ‘To’, ‘CC’, ‘BCC’ are similar in retrieving the data. The From is separate since it provides more information that the other fields and also that there can’t be more than one sender on the message.

From E-mail Address

First we need to gain access to the header section of the e-mail we are importing.

To gain access to each of these e-mails you have to access them via the RfcMailAddress type. This is easily done as follows.

   1: Pop3Client connection = new Pop3Client();                

   2:  

   3: connection.Connect(serverURL, port, ssl);                

   4: connection.Authenticate(userName, password);

   5:  

   6: Message emailMessage = connect.GetMessage(i); // number of the message you are getting

   7: MessageHeader emailHeader = emailMessage.Headers; // grab the headers

 

After you have the connection and the headers stored, you need to gain access to the From object. It’s a part of the RfcMailAddress type.

   1: RfcMailAddress emailFrom = emailHeader.From;

The From e-mail address is simple since we know the e-mail can only be sent from one individual e-mail address.  Each of the members of the object are straight forward.

   1: string fromAddress = emailFrom.Address;

   2: string fromName = emailFrom.DisplayName;

   3: string fromComplete = emailHeader.From.ToString();

You will notice the last line of code. This is an extremely quick way to get both the name and the address in one string variable. If you use that method you will need to split up the data yourself. I prefer the other two calls, that way I can access the data without having to parse any of it.

To, CC, BCC E-mail Addresses

The To, CC, BCC e-mail address are all similar, you won’t be able to use the “From” example when retrieving the data. What works for one of these types will work for the other. For this instance we will say the e-mail that you are retrieving has multiple “To” recipients. This will requiring looping through the data to get each of the e-mail addresses. Before we even loop we need to store the e-mails into a List. I have put all the code together.

   1: List<RfcMailAddress> emailTo = emailHeader.To;

   2:                             

   3: // Loop through the list and concatenate the e-mail addresses

   4: string strAllEmails = "";

   5:  

   6: foreach(RfcMailAddress tempTo in emailTo)

   7: {

   8:     if(tempTo.HasValidMailAddress)

   9:     {

  10:         strAllEmails += tempTo.MailAddress + ",";

  11:     }

  12: }

To accomplish this for the CC and the BCC e-mail addresses you just need to change the variable you are grabbing to be the .Cc or .Bcc.

E-mail Body

Grabbing the body of the e-mail message is a little bit trying. You need to actually grab the MessagePart (in the e-mail address we used the MessageHeader). You will make that call as follows:

   1: MessagePart emailMessage = connection.GetMessage(intMessageNumber).MessagePart;      

Once you have the MessagePart you can then access the following methods .FindFirstPlainTextVersion() and .FindFirstHtmlVersion(), plain text and HTML respectively. But once you have these set up you need to be able to actually see the Body as text (whether it is HTML text or plain text). Below is the final call on how to get the desired body text from the e-mail message.

   1: Message emailMessage = connection.GetMessage(intMessageNumber); // get the full message

   2:  

   3: // Then get the type of body text you desire.

   4: string plainBody = emailMessage.FindFirstPlainTextVersion().GetBodyAsText();

   5: string htmlBody = emailMessage.FindFirstHtmlVersion().GetBodyAsText();

Even though we call another method .GetBodyAsText() it still will return the needed value even if the body is HTML (tags and all).

Attachments

We can’t forget about the attachments. What’s a good e-mail without the ability to attach a file?

First you will need to test whether the e-mail has an attachment. If it does we need to set up a situation where we can loop through all the attachments, in case there is more than one. Below is a bit of code that will save the attachments to a folder on the local drive

   1: foreach (MessagePart emailAttachment in emailMessageMain.FindAllAttachments())

   2: {                                    

   3:     //-- Set variables

   4:     string OriginalFileName = emailAttachment.FileName;

   5:     string ContentID = emailAttachment.ContentId; // If this is set then the attachment is inline.

   6:     string ContentType = emailAttachment.ContentType.MediaType; // type of attachment pdf, jpg, png, etc.

   7:   

   8:     //-- write the attachment to the disk

   9:     File.WriteAllBytes(path + OriginalFileName, emailAttachment.Body); //overwrites MessagePart.Body with attachment   

  10: }

The documentation on the OpenPop.NET site is pretty straight forward and can help with anything I didn’t cover here.

Warnings

  • GMail can be accessed by OpenPOP.NET if you set the right settings in your account setup. The problem is Gmail does things a little different that a normal POP3 server. I would suggest that instead of trying to access gmail as a POP3 server that you access it as an IMAP server (which is has the ability to do). This will save you a lot of grief down the road.
  • OpenPop.NET does not handle IMAP servers

,

Comments are currently closed.