问题
I use code from this link to get email headers from outlook.
But, it is not extracting email body(content-type) correctly. Everything works fine. If you want to compare, you can open gmail, see options for gmail and click 'show original' which shows headers correctly.
Providing code from above link:
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop.Outlook;
public static class MailItemExtensions
{
private const string HeaderRegex =
@"^(?<header_key>[-A-Za-z0-9]+)(?<seperator>:[ \t]*)" +
"(?<header_value>([^\r\n]|\r\n[ \t]+)*)(?<terminator>\r\n)";
private const string TransportMessageHeadersSchema =
"http://schemas.microsoft.com/mapi/proptag/0x007D001E";
public static string[] Headers(this MailItem mailItem, string name)
{
var headers = mailItem.HeaderLookup();
if (headers.Contains(name))
return headers[name].ToArray();
return new string[0];
}
public static ILookup<string, string> HeaderLookup(this MailItem mailItem)
{
var headerString = mailItem.HeaderString();
var headerMatches = Regex.Matches
(headerString, HeaderRegex, RegexOptions.Multiline).Cast<Match>();
return headerMatches.ToLookup(
h => h.Groups["header_key"].Value,
h => h.Groups["header_value"].Value);
}
public static string HeaderString(this MailItem mailItem)
{
return (string)mailItem.PropertyAccessor
.GetProperty(TransportMessageHeadersSchema);
}
}
Output:
MIME-Version: 1.0
Received: by someip with HTTP; Wed, 3 Dec 2014 10:04:00 -0800 (PST)
Date: Wed, 3 Dec 2014 23:34:00 +0530
Delivered-To: test..@gmail.com
Message-ID: <somehashhere..g@mail.gmail.com>
Subject: <subject here>
From: test name <test @gmail.com>
To: test name <test @gmail.com>
Content-Type: multipart/alternative; boundary=<somehash...>
Output from gmail(click 'show originl' in gmail message options):
MIME-Version: 1.0
Received: by someiphere with HTTP; Wed, 3 Dec 2014 10:04:00 -0800 (PST)
Date: Wed, 3 Dec 2014 23:34:00 +0530
Delivered-To: test..@gmail.com
Message-ID: <somehash__@mail.gmail.com>
Subject: subjecthere
From: test name <test..@gmail.com>
To: test name <test..@gmail.com>
Content-Type: multipart/alternative; boundary=somehash
--somehash
Content-Type: text/plain; charset=UTF-8
messagehere
--somehash
Content-Type: text/html; charset=UTF-8
<div dir="ltr">messagehere</div>
--somehash--
回答1:
PR_TRANSPORT_MESSAGE_HEADERS property only returns the MIME headers of the main MIME part. The actual data is not stored there. When a message is received by Outlook, the headers are parsed into various MAPI properties (e.g. "Subject" goes into the PR_SUBJECT MAPI property). Plain text body goes into PR_BODY, etc.
Take a look at an existing message with OutlookSpy - click IMessage button and select the PR_TRANSPORT_MESSAGE_HEADERS property to see its contents.
UPDATE: You can convert the message to the MIME format. It will not be the exact message that came in (the order of headers and MIME message parts might be different). You can either
Use IConverterSession MAPI interface (Extended MAPI, so C++ or Delphi only). You can play with that interface in OutlookSpy (click IConverterSession button on the OutlookSpy ribbon).
Construct the MIME message explicitly in your code.
Use Redemption and its RDOMail.Save(..., olRfc822) method
回答2:
I know that is very old question, but I see it in top of google search so I want to share my finding for other guys
I have used bellowing code to get Content-Type in Outlook 2013/2016
if (Globals.ThisAddIn.Application.Inspectors.Count > 0)
{
Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
if (inspector.CurrentItem is Outlook.MailItem)
{
Outlook.MailItem mail = (Outlook.MailItem)inspector.CurrentItem;
string pidNameContentType = mail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/content-type/0x0000001F");
}
}
来源:https://stackoverflow.com/questions/26956076/current-code-not-extracting-email-headers-for-content-type