Importing CDO.dll and still can't get headers from mail items using C#

  • Thread starter Thread starter Andy Baldwin
  • Start date Start date
A

Andy Baldwin

So, I have the following function defined in C#

public string InternetHeaders(Outlook.MailItem mi) {
object missingVal = System.Reflection.Missing.Value;
MAPI.Session session = new MAPI.SessionClass();
session.Logon("", missingVal, missingVal,
missingVal, missingVal,
missingVal, missingVal);
MAPI.Message m = (MAPI.Message)session.GetMessage(mi.EntryID,"");
System.Console.WriteLine(m.Fields);
MAPI.Fields fields = (MAPI.Fields)m.Fields;
string headers = fields["&H7D001E"];
return null;
}

MAPI is my CDO.dll reference, and Outlook is the Outlook API
reference. The problem lies in the second to last line where I am
attempting to get the message headers. Slipstick refers me to

http://support.microsoft.com/?kbid=194870

which gives a VB example in which it shows using the fields collection
like a c# indexer (which is what this code is based on). Problem is
that using either square brackets or parenthesis both give me build
errors ("Cannot apply indexing with [] to an expression of type
'MAPI.Fields'" or "'fields' denotes a 'variable' where a 'method' was
expected"). My autocomplete/tooltip is inactive when I attempt to use
the [] or () as well. I then additionally tried the get_item method
like so:

string headers = (string)fields.get_Item((object)0,(object)"&H7D001E");

assuming that the second argument called propID is the same everywhere
and got

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: [Collaboration Data Objects -
[MAPI_E_NOT_FOUND(8004010F)]]

I looked up that MAPI_E_NOT_FOUND error with no real luck. I have read
somwhere (although I can't find it again) that there were problems
using CDO under .Net. Is this true? Can anyone else suggest a quick
way to get the full message headers in .Net from an Outlook.MailItem?
I wanna parse this info and keep it to analyze for spammer trends.

Thanks in advance!!

Andy Baldwin
 
Hi Andy,
string headers = (string)fields.get_Item((object)0,(object)"&H7D001E");

What if you call it as listed below, does it help?
string headers = (string)fields.get_Item("&H7D001E", null);

string headers = (string)fields.get_Item("&H7D001E",
System.Reflection.Missing.Value);


...
Regards,
Vadim.
 
What if you call it as listed below, does it help?
string headers = (string)fields.get_Item("&H7D001E", null);

Gives a type 'System.Runtime.InteropServices.COMException'
Additional information: Parameter not found.
string headers = (string)fields.get_Item("&H7D001E",
System.Reflection.Missing.Value);

gives same exception about the MAPI_E_NOT_FOUND

The parameter list of get_Item is "object index, object PropsetID". I
was under the assumption that "&H7D001E" was a PropsetID so I tried
reversing the order of the parameters in your two calls, and
subsequently got another parameter not found exception and a type
mismatch exception respectivly (to the order above).

I have also in my spare time here watched the new MAPI.Message object
and verified that although I don't have a StoreID in my call to get
the message

MAPI.Message m = (MAPI.Message)session.GetMessage((Outlook.MailItem)mi.EntryID,"");

I am indeed getting the correct MAPI.Message. So that shouldn't be the
problem, right?

I had dreams about this last night too. Why isn't this as easy as it
should be? There should just be a string property named "headers" on
the message object (like the sender and other properties??).

Any more suggestions or hints are greatly appreciated!

Andy
 
Hi Andy,

OK, here is idl definition for MAPI::Fields::Item

<idl>
[id(0x00000015), propget, helpstring("The Item property returns a single
Field object from the Fields collection.")]

VARIANT Item(

[in] VARIANT Index,

[in, optional] VARIANT PropsetID);

</idl>


And documentation snip

<doc>
objFieldsColl.Item(index)
objFieldsColl.Item(proptag)
objFieldsColl.Item(name [, propsetID] )
objFieldsColl

Required. Specifies the Fields collection object.

index
Integer. Value must be greater than 0 and less than or equal to 65,535
(&HFFFF). Specifies the index within the Fields collection.

proptag
Long. Value must be greater than or equal to 65,536 (&H10000). Specifies the
property tag value for the MAPI property to be retrieved.

name
String. Contains the custom name of the user-defined property, or a string
representation of the of its property tag.

propsetID
Optional. String....

</doc>


Perhaps first paramter should be a number, not a string?:

Int32 index = 0xH7D001E;
MAPI.Field headers = fields.get_Item(index, System.Reflection.Missing.Value)
as MAPI.Field;

...
Cheers,
Vadim.
 
WOO HOO, I did some further searching and found a post

http://www.google.com/groups?hl=en&[email protected]#link7

that mentions that he was able to get something that looked like the
following to work:

string headers = ((MAPI.Field)fields.get_Item(MAPI.CdoPropTags.CdoPR_TRANSPORT_MESSAGE_HEADERS,
System.Reflection.Missing.Value)).Value.ToString();

MAPI.CdoPropTags is a enumeration that gives out all those funky hex
notations. This worked! Can't really tell why, It has something to do
with the cast to a Field object, and using that enumeration instead.
Really wierd problem, but thanks for all of your help!!

AB
 
Back
Top