c# program to read Internet headers from public folder emails


john bailo

I am attempting to create a c# program to iterate through the messages in an
public folder and extract the headers. My apologies to the VB/VBA groups,
but you seem to
have more information for Office automation than the c# groups.

I am having some problems manipulating the various object models. I say
various because based
on what source code I could find, I am trying to use the Outlook, CDO and
MAPI type libraries
( COM+ objects ). Looking back at source code, either .NET is not
importing all the properties
that are available, or the available properties have changed. The key
seems to be access to the MAPI
properties. I see some of these in the Outlook type library, some in CDO
and almost none at
all in the MSMAPI (!)

Should I use all three? Or do I need to add some other library?


Specifically, I am able to traverse the folder structure and cast the
contents of a public folder
as an Outlook.MailItem -- that however is not enough to gain access to the
Internet headers, there
is no simple property to expose them.

(Problem 1) Also, I am puzzled because although I type my public folder as
Outlook.MAPIFolder it does not support a
count property so I cannot gather how many items are in the folder. Why

I have found some VBA code to extract the headers and am working on turning
it into a c# method. But, again,
there are properties that do not appear, for example:

objItem = objOutlook.ActiveInspector.CurrentItem;

(Problem 2) ActiveInspector exists, but the CurrentItem property does not

My code appears below.

using System;

using System.Reflection;

using Outlook;

using CDONTS;

using MSMAPI;

namespace Thresher


/// <summary>

/// Summary description for Class1.

/// </summary>

class Rotors


public static Outlook.Application objOutlook = new

public static Outlook.Folders objFolders =

public static Outlook.NameSpace objFolder = objOutlook.GetNamespace("MAPI");

public static Outlook.MAPIFolder aolAbuse;

public static Outlook.MailItem aolAbuseMail;

static void Main(string[] args)


Outlook.Application objOutlook = new Outlook.ApplicationClass();

Outlook.NameSpace objFolder = objOutlook.GetNamespace("MAPI");

Outlook.Explorer objExplorer =
rDrafts), OlFolderDisplayMode.olFolderDisplayNormal);


Outlook.MailItem objMail = (Outlook.MailItem)

//objMail.To = "(e-mail address removed)";

//objMail.Subject = "sample email";

//objMail.Body = "Hi,\nI'm your sample email.";


for (int i=1; i <= objFolders.Count; i++)


if (objFolders.Item(i).Name == "Public Folders")


aolAbuse = objFolders.Item(i);






aolAbuse = returnAOL(aolAbuse, "All Public Folders");


aolAbuse = returnAOL(aolAbuse, "Vestcom North West");


aolAbuse = returnAOL(aolAbuse, "VNW - General Email Folders");


aolAbuse = returnAOL(aolAbuse, "AOLAbuse");


//Outlook.Folders aolPublicFolder = (Outlook._Items) aolAbuse;


Outlook.Explorer objExplorerAOL = objOutlook.Explorers.Add(aolAbuse,



//CDONTS.Message aolAbuseMail1;




for (int i=1; i< 10 ; i++ )


aolAbuseMail = (Outlook.MailItem) aolAbuse.Items.Item(i);







//catch(Exception e)

//{string q = e.ToString(); }






public static Outlook.MAPIFolder returnAOL(Outlook.MAPIFolder inFolder,
string folderName)


for (int i=1; i <= inFolder.Folders.Count; i++)

if (inFolder.Folders.Item(i).Name == folderName)

return inFolder.Folders.Item(i);

return null;


public static string InternetHeaders()


Outlook.Application objOutlook;

Outlook.MailItem objItem;


Outlook.NameSpace objCDO = objOutlook.GetNamespace("MAPI");

// Instantiate an Outlook Application object.

//objOutlook = CreateObject("Outlook.Application");

// Find the current email item and get its EntryID

objItem = objOutlook.ActiveInspector.CurrentItem;

strID = objItem.EntryID;

// Then set up a CDO Session using a piggy-back login

objCDO.Logon("", "", False, False);

// Now get the item as a CDO Message

objMessage = objCDO.GetMessage(strID);

// Now get the headers from the message

Set objFields = objMessage.Fields;

InternetHeaders = objFields.Item(CdoPR_TRANSPORT_MESSAGE_HEADERS).Value;

// Now that the headers are captured in a string you can do whatever you
want with them


objFields = null;

objMessage = null;

objCDO = null;

objItem = null;

objOutlook = null;




Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question