当前代码不提取内容类型的电子邮件标头

时间:2014-11-16 10:44:47

标签: c# outlook add-in outlook-addin office-addins

我使用此link中的代码从outlook获取电子邮件标头。

但是,它没有正确提取电子邮件正文(内容类型)。一切正常。如果你想比较,可以打开gmail,查看Gmail的选项,然后点击“显示原始数据”。它正确地显示了标题。

从上面的链接提供代码:

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);
    }
}

输出:

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...>

来自gmail的输出(点击&#39;在gmail消息选项中显示来源&#39;):

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

2 个答案:

答案 0 :(得分:1)

PR_TRANSPORT_MESSAGE_HEADERS属性仅返回主MIME部分的MIME标头。实际数据不存储在那里。当Outlook收到邮件时,标头会被解析为各种MAPI属性(例如“Subject”进入PR_SUBJECT MAPI属性)。纯文本正文进入PR_BODY等。

使用OutlookSpy查看现有消息 - 单击IMessage按钮并选择PR_TRANSPORT_MESSAGE_HEADERS属性以查看其内容。

<强>更新: 您可以将邮件转换为MIME格式。它不会是确切的消息(标题和MIME消息部分的顺序可能不同)。你可以

  1. 使用IConverterSession MAPI接口(扩展MAPI,仅限C ++或Delphi)。您可以在OutlookSpy中使用该界面进行播放(单击OutlookSpy功能区上的IConverterSession按钮)。

  2. 在代码中明确构造MIME消息。

  3. 使用Redemption及其RDOMail。保存(...,olRfc822)方法

答案 1 :(得分:0)

我知道这是一个非常古老的问题,但我在谷歌搜索的顶部看到它,所以我想与其他人分享我的发现

我使用了以下代码来获取Outlook 2013/2016中的Content-Type

n