c# program to read Internet headers from public folder emails

J

john bailo

I am attempting to create a c# program to iterate through the messages in an
Outlook/Exchange
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?

PROBLEMS

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
not?

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
Outlook.ApplicationClass();

public static Outlook.Folders objFolders =
objOutlook.GetNamespace("MAPI").Folders;

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 =
objOutlook.Explorers.Add(objFolder.GetDefaultFolder(OlDefaultFolders.olFolde
rDrafts), OlFolderDisplayMode.olFolderDisplayNormal);


objExplorer.Activate();


Outlook.MailItem objMail = (Outlook.MailItem)
objOutlook.CreateItem(OlItemType.olMailItem);

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

//objMail.Subject = "sample email";

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

//objMail.Save();



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

{

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

{

aolAbuse = objFolders.Item(i);

Console.WriteLine(aolAbuse.Name);


}

}


//try

//{

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

Console.WriteLine(aolAbuse.Name);


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

Console.WriteLine(aolAbuse.Name);


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

Console.WriteLine(aolAbuse.Name);


aolAbuse = returnAOL(aolAbuse, "AOLAbuse");

Console.WriteLine(aolAbuse.Name);

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

//aolAbuse.Folders.Count

Outlook.Explorer objExplorerAOL = objOutlook.Explorers.Add(aolAbuse,
OlFolderDisplayMode.olFolderDisplayNormal);

objExplorerAOL.Activate();

//objExplorerAOL.CurrentFolder.c



//CDONTS.Message aolAbuseMail1;

//aolAbuseMail1.

//MSMAPI.

//CDONTS.Message

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

{

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

Console.WriteLine(aolAbuseMail.Subject.ToString());

Console.WriteLine(aolAbuseMail.Body);

//aolAbuseMail.fi

}

//aolAbuse.


//}

//catch(Exception e)

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

//finally

//{

objOutlook.Quit();

//}



}

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;

MSMAPI.MAPISession objCDO;

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

objCDO.Logoff;

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

Top