Getting headers of a MailItem?

  • Thread starter Thread starter 280Z28
  • Start date Start date
2

280Z28

Using the new Office 12 SDK.

I'm trying to get the headers of a MailItem programmatically. Therse are the
same things you would see if you right click in Outlook and go to Message
Options.

I can get the body of the message easily, with the MailItem.Body property.
The place where I need the headers is in an event handler for the
Folder.Items.ItemAdd event. In particular, I'm trying to do something when
items are added to the folder and have certain SpamAssassin information in
the header:

X-Spam-Flag: YES
X-Spam-Checker-Version: SpamAssassin 3.1.0 (2005-09-13) on
box36.bluehost.com
X-Spam-Level: *******************************
X-Spam-Status: Yes, score=31.5 required=5.0 tests=BAYES_95,
DATE_IN_FUTURE_96_XX,DATE_SPAMWARE_Y2K,FORGED_AOL_TAGS,
FORGED_MUA_AOL_FROM,FROM_ILLEGAL_CHARS,HEAD_ILLEGAL_CHARS,
HTML_MESSAGE,HTML_MIME_NO_HTML_TAG,MIME_BOUND_DD_DIGITS,
MIME_HTML_ONLY,MIME_HTML_ONLY_MULTI,MISSING_MIMEOLE,MSGID_SPAM_CAPS,
REPTO_QUOTE_AOL,SUBJ_ILLEGAL_CHARS,UNPARSEABLE_RELAY autolearn=no
version=3.1.0
 
Assuming you have a mail item instantiated:

Dim oPropAccessor As Outlook.PropertyAccessor

Const PR_MAIL_HEADER_TAG = _
"http://schemas.microsoft.com/mapi/proptag/0x007D001E"

'only works if Application.IsTrusted is True
Set oPropAccessor = oItem.PropertyAccessor

strHeaders = oPropAccessor(PR_MAIL_HEADER_TAG)

From there the headers are in the string variable strHeaders and can be
parsed out.
 
I left out a method that must be called on the PropertyAccessor object; the
code line should read as follows:

strHeaders = oPropAccessor.GetProperty(PR_MAIL_HEADER_TAG)

Sorry about that.
 
Thank you. I got it working. I'm using this to automatically mark certain
messages that go into the junk folder as read so I don't have to review them.

const string PR_MAIL_HEADER_TAG =
"http://schemas.microsoft.com/mapi/proptag/0x007D001E";
Outlook.PropertyAccessor oPropAccessor = mailItem.PropertyAccessor;
string headers = (string)oPropAccessor.GetProperty( PR_MAIL_HEADER_TAG );
bool spam = headers.Contains( "X-Spam-Flag: YES" );
 
Don't wait for it to happen. You now have access to that property and any
others through the PropertyAccessor, just use that.
 
That's what I'm doing. You can see how I'm using this in my solution to the
problem named in this topic in Outlook General:

Outlook should let me prioritize rules to run before junk filters

I believe my solution could also be done by anyone through Outlook macros in
VB, but it's easier for me to create an add-in since I have VS2005 and know
C# but not VB.

Sam
 
Back
Top