当前代码未提取内容类型的电子邮件标题 [英] current code not extracting email headers for content-type

查看:91
本文介绍了当前代码未提取内容类型的电子邮件标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此链接中的代码来获取电子邮件标头外表.

I use code from this link to get email headers from outlook.

但是,它没有正确提取电子邮件正文(内容类型).一切正常.如果要进行比较,可以打开gmail,查看gmail的选项,然后点击显示原始图片",以正确显示标题.

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

输出:

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的输出(在gmail邮件选项中单击显示originl"):

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

推荐答案

PR_TRANSPORT_MESSAGE_HEADERS属性仅返回主要MIME部分的MIME标头.实际数据未存储在此处. Outlook收到邮件后,标头将解析为各种MAPI属性(例如,主题"进入PR_SUBJECT MAPI属性).纯文本主体进入PR_BODY,等等

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.

使用 OutlookSpy 查看现有消息-单击"IMessage"按钮,然后选择PR_TRANSPORT_MESSAGE_HEADERS属性以查看它的内容.

Take a look at an existing message with OutlookSpy - click IMessage button and select the PR_TRANSPORT_MESSAGE_HEADERS property to see its contents.

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

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

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

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

在您的代码中显式构造MIME消息.

Construct the MIME message explicitly in your code.

使用兑换及其

Use Redemption and its RDOMail.Save(..., olRfc822) method

这篇关于当前代码未提取内容类型的电子邮件标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆