2014-11-16 3 views
0

나는이 코드 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); 
    } 
} 

출력 : Gmail의

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: [email protected] 

Message-ID: <[email protected]> 

Subject: <subject here> 

From: test name <test @gmail.com> 

To: test name <test @gmail.com> 

Content-Type: multipart/alternative; boundary=<somehash...> 

출력 (Gmail의 메시지 옵션에서 '쇼 originl'를 클릭) :

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: [email protected] 
Message-ID: <[email protected]> 
Subject: subjecthere 
From: test name <[email protected]> 
To: test name <[email protected]> 
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-- 
+0

당신은 당신의 코드를 단계별로 콘텐츠 유형을 얻기 위해 bellowing 코드를 사용한

다른 사람

위해 찾는? 정규 표현식이 맞습니까? 예를 들어 http://regexpad.com/#p-javascript와 같이 테스트 해 보셨습니까? 헤더에 실제로 "header_key"또는 "seperator> (sic)가 포함되어 있습니까? –

+0

mailitem.HeaderString()은 메일 본문을 제외한 전체 데이터를 반환합니다. 이는 메일 본문이 콘텐츠 유형 (gmail)으로 표시되고 몇 가지 새로운 라인, 그 코드에서 처리되지 않을 수 있습니다. 그건 그렇고 내 코드가 아니야, 어딘가에 온라인으로 봤다. 내가 regexpad에서 코드를 아직 테스트하지 않았어, 내가 시도합니다 – user2349115

+0

나는 아웃룩뿐만 아니라 Gmail에서 출력을 추가 할 수 있도록 – user2349115

답변

1

PR_TRANSPORT_MESSAGE_HEADERS 속성을 주 MIME 부분의 MIME 헤더 만 반환합니다. 실제 데이터는 거기에 저장되지 않습니다. Outlook에서 메시지를 받으면 머리글은 다양한 MAPI 속성으로 구문 분석됩니다 (예 : "제목"이 PR_SUBJECT MAPI 속성에 포함됨). 일반 텍스트 본문이 PR_BODY 등으로 이동합니다.

OutlookSpy으로 기존 메시지를보십시오. IMessage 버튼을 클릭하고 내용을 보려면 PR_TRANSPORT_MESSAGE_HEADERS 속성을 선택하십시오.

업데이트 : 메시지를 MIME 형식으로 변환 할 수 있습니다. 정확한 메시지가 아닙니다 (헤더와 MIME 메시지 부분의 순서가 다를 수 있음). 당신도

  1. 사용 IConverterSession MAPI 인터페이스 (확장 MAPI, C++ 또는 델파이 만 정도)를 할 수 있습니다. 해당 인터페이스로 OutlookSpy에서 재생할 수 있습니다 (OutlookSpy 리본의 IConverterSession 단추 클릭).

  2. 코드에 명시 적으로 MIME 메시지를 구성하십시오.

  3. 사용 RedemptionRDOMail .Save (..., olRfc822) 방법

+0

고마워 +1 for now .......... – user2349115

+0

좋아, 알았다.어쨌든, 원래 메시지의 것과 거의 같은 (거의) 생성합니까? 나는 mailitem.html 몸과 몸 (html과 plain)을 얻을 수있었습니다. 그래서 남은 것은 콘텐츠 유형뿐입니다. 그 또한 얻을 수 있습니까? 얻을 수있는 경우 수동으로 추가 할 수 있습니다. r = headerstring. r + = "\ n"+ get_text_content_type(); r + = "\ n"+ mailitem.body; r + = "\ n"+ get_html_content_type(); r + = "\ n"+ mailitem.html_body; – user2349115

+0

위의 업데이트 된 답변을 참조하십시오. –

0

나는 아주 오래된 질문입니다 알지만, 그래서 난 내를 공유 할 Google 검색의 상단에서 볼 정확히 작동하지 않습니다, 나는 아웃룩 2,016분의 2,013

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