Using MAPI or ExMAPI to get SMTP header

Add-in Express™ Support Service
That's what is more important than anything else

Using MAPI or ExMAPI to get SMTP header
need help getting Outlook MailItem SMTP HEADER 
allen razdow




Posts: 1
Joined: 2007-02-01
Trolling the forums, I see examples of accessing SMTP message headers using a MAPI namespace....for example:

MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_EMAIL_ADDRESS, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure
(pPropValue, typeof(MAPI.SPropValue));

...etc. How do I find and access that namespace? Do I need to add something to my prject? Do I need ADX Premium?

I am using VS 2005 c# and need to get at the actual email headers in a mailitem.
Posted 01 Feb, 2007 17:40:22 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Allen.

Please use the code below:

private class MAPI
{
public const uint PR_TRANSPORT_MESSAGE_HEADERS = 0x7D001E;
public const uint PR_BODY = 0x1000001E;
public const uint PR_BODY_HTML = 0x1013001E;
public const uint PR_HTML = 0x10130102;

public struct SPropValue
{
public uint ulPropTag;
public uint dwAlignPad;
public long Value;
}

[DllImport("MAPI32.DLL", CharSet=CharSet.Ansi, EntryPoint="HrGetOneProp@12")]
public static extern void HrGetOneProp(IntPtr pmp, uint ulPropTag, out IntPtr ppProp);

[DllImport("MAPI32.DLL", CharSet=CharSet.Ansi, EntryPoint="MAPIFreeBuffer@4")]
public static extern void MAPIFreeBuffer(IntPtr lpBuffer);
}

private void btnShowIH_Click(object sender)
{
object mapiObject = null;
Outlook._Explorer explorer = OutlookHost().ActiveExplorer();
if (explorer == null) return;
Outlook.Selection selObject = explorer.Selection;
if (selObject == null) return;
object item = selObject.Item(1);
if (item is Outlook._MailItem)
{
mapiObject = (item as Outlook._MailItem).MAPIOBJECT;
}
else if (item is Outlook._RemoteItem)
{
mapiObject = (item as Outlook._RemoteItem).MAPIOBJECT;
}
if (mapiObject != null)
{
string iheader = String.Empty;
IntPtr pPropValue = IntPtr.Zero;
IntPtr unk = IntPtr.Zero;
try
{
IntPtr unkObj = Marshal.GetIUnknownForObject(mapiObject);
Guid iMapiProp = new Guid("00020303-0000-0000-C000-000000000046");
Marshal.QueryInterface(unkObj, ref iMapiProp, out unk);
Marshal.Release(unkObj);
if (unk != IntPtr.Zero)
{
MAPI.HrGetOneProp(unk, MAPI.PR_TRANSPORT_MESSAGE_HEADERS, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
propValue.Value &= 0xFFFFFFFF;
iheader = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
}
}
catch
{
}
if (pPropValue != IntPtr.Zero)
MAPI.MAPIFreeBuffer(pPropValue);
if (unk != IntPtr.Zero)
Marshal.Release(unk);
Marshal.ReleaseComObject(mapiObject);
if (iheader != String.Empty)
{
ContentForm ihForm = new ContentForm();
ihForm.Text = "Internet Header";
ihForm.HeaderText.AppendText(iheader);
switch(ihForm.ShowDialog())
{
case DialogResult.OK:
item.GetType().InvokeMember("Display", BindingFlags.InvokeMethod, null, item, new object[]{false});
break;
case DialogResult.Yes:
item.GetType().InvokeMember("Delete", BindingFlags.InvokeMethod, null, item, null);
break;
}
ihForm.Dispose();
}
}
if (item != null) Marshal.ReleaseComObject(item);
Marshal.ReleaseComObject(selObject);
Marshal.ReleaseComObject(explorer);
}


P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 02 Feb, 2007 11:16:28 Top