2016-03-08 100 views
1

我们使用OpenPop获取电子邮件。我必须阅读该消息的主题并将其展示给用户。当用户点击消息时,我想在占位符中显示消息的内容。
这里是我的代码来显示内容,但它不工作得非常好:如何使用OpenPop以HTML格式阅读电子邮件?

plhMessage.Controls.Add(new LiteralControl(Encoding.UTF8.GetString(client.GetMessage(0).RawMessage))); 

它头部返回这样的文字:

Return-Path: [email protected] 
Received: from adspackages.com (Unknown [185.81.96.156]) by ip-30.afaghhost.com ; Mon, 7 Mar 2016 07:13:06 +0330 
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=adspackages.com; 
    s=default; h=Content-Transfer-Encoding:Content-Type: 
    MIME-Version:Message-ID:Subject:Reply-To:From:To:Date; 
    bh=MBNWLlH5FJIZMrbe+SG0izGjV9d9fR6eupDkfvgklSw=; 
    b=DLYlNuMd7ZAPCpQTQvGCi7yBiX aRhjlqQ8zGLPWcmDDX159frMVPiTh652Os3xwxWZ/iS4BQeOA5cFXdZSwcCxIO9hEaGr7ogZXVM5k blpgoO3htf9GAPJSDOxxVWIjEYgN3+m7UE1N7azmtvUPrrZkl9H8JwCIXLbI0SRpmHQ2ebA7QMgQM /Jl8u+5dLmWo4eYLxFgyTjZwTPPmLKwfSADjET3bb9ZfBTcK/KHsAjfx7Miy7zgP5vSE1n+p .... 

,并显示该消息没有图片。

如何获取电子邮件的HTML以在占位符中显示它? 另外,如何显示电子邮件中包含的图片?

+0

这可能会帮助你:http://stackoverflow.com/questions/10601913/openpop-net-get-actual-message-text – ManoDestra

+0

该问题的答案可以用来获取纯文本或HTML消息体,但是如果图像嵌入到消息中,则渲染返回的html将不包含图像(它只会具有损坏的图像)。 – jstedfast

回答

1

在开始之前,我应该注意,你不能在client.GetMessage(0).RawMessage上可靠地使用Encoding.UTF8.GetBytes(),因为(错误地)假定原始消息数据是UTF-8,并且不能保证是这种情况。西欧的大多数电子邮件将采用拉丁编码之一,俄文电子邮件通常采用iso-8859-5,windows-1251或koi8-r。中文,日文和韩文电子邮件几乎总是处于其语言环境字符集之一(通常分别是gbk/big5,iso-2022-jp和euc-kr)。

现在已经解决了......让我们继续讨论您的主要问题。

你想要做什么是很坦率地说了很多比它更容易使用OpenPOP使用MailKit做的,所以我会告诉你如何使用MailKit代替做到这一点:

得到的消息,MailKit作品非常喜欢OpenPOP作用:

using System; 

using MailKit.Net.Pop3; 
using MailKit; 
using MimeKit; 

namespace TestClient { 
    class Program 
    { 
     public static void Main (string[] args) 
     { 
      using (var client = new Pop3Client()) { 
       client.Connect ("pop.gmail.com", 995, true); 

       // Note: since we don't have an OAuth2 token, disable 
       // the XOAUTH2 authentication mechanism. 
       client.AuthenticationMechanisms.Remove ("XOAUTH2"); 

       client.Authenticate ("[email protected]", "password"); 

       for (int i = 0; i < client.Count; i++) { 
        var message = client.GetMessage (i); 
        // TODO: render the message 
       } 

       client.Disconnect (true); 
      } 
     } 
    } 
} 

要使用MailKit呈现一个消息,最简单的方法是写自己的MimeVisitor这样的:

/// <summary> 
/// Visits a MimeMessage and generates HTML suitable to be rendered by a browser control. 
/// </summary> 
class HtmlPreviewVisitor : MimeVisitor 
{ 
    List<MultipartRelated> stack = new List<MultipartRelated>(); 
    List<MimeEntity> attachments = new List<MimeEntity>(); 
    string body; 

    /// <summary> 
    /// Creates a new HtmlPreviewVisitor. 
    /// </summary> 
    public HtmlPreviewVisitor() 
    { 
    } 

    /// <summary> 
    /// The list of attachments that were in the MimeMessage. 
    /// </summary> 
    public IList<MimeEntity> Attachments { 
     get { return attachments; } 
    } 

    /// <summary> 
    /// The HTML string that can be set on the BrowserControl. 
    /// </summary> 
    public string HtmlBody { 
     get { return body ?? string.Empty; } 
    } 

    protected override void VisitMultipartAlternative (MultipartAlternative alternative) 
    { 
     // walk the multipart/alternative children backwards from greatest level of faithfulness to the least faithful 
     for (int i = alternative.Count - 1; i >= 0 && body == null; i--) 
      alternative[i].Accept (this); 
    } 

    protected override void VisitMultipartRelated (MultipartRelated related) 
    { 
     var root = related.Root; 

     // push this multipart/related onto our stack 
     stack.Add (related); 

     // visit the root document 
     root.Accept (this); 

     // pop this multipart/related off our stack 
     stack.RemoveAt (stack.Count - 1); 
    } 

    // look up the image based on the img src url within our multipart/related stack 
    bool TryGetImage (string url, out MimePart image) 
    { 
     UriKind kind; 
     int index; 
     Uri uri; 

     if (Uri.IsWellFormedUriString (url, UriKind.Absolute)) 
      kind = UriKind.Absolute; 
     else if (Uri.IsWellFormedUriString (url, UriKind.Relative)) 
      kind = UriKind.Relative; 
     else 
      kind = UriKind.RelativeOrAbsolute; 

     try { 
      uri = new Uri (url, kind); 
     } catch { 
      image = null; 
      return false; 
     } 

     for (int i = stack.Count - 1; i >= 0; i--) { 
      if ((index = stack[i].IndexOf (uri)) == -1) 
       continue; 

      image = stack[i][index] as MimePart; 
      return image != null; 
     } 

     image = null; 

     return false; 
    } 

    // Save the image to our temp directory and return a "data:" url suitable for 
    // the browser control to load. 
    string GetDataImageSrc (MimePart image) 
    { 
     using (var output = new MemoryStream()) { 
      image.ContentObject.DecodeTo (output); 
      return string.Format ("data:{0};base64,{1}", image.ContentType.MimeType, Convert.ToBase64String (output.GetBuffer(), 0, (int) output.Length)); 
     } 
    } 

    // Replaces <img src=...> urls that refer to images embedded within the message with 
    // "data:" urls that the browser control will actually be able to load. 
    void HtmlTagCallback (HtmlTagContext ctx, HtmlWriter htmlWriter) 
    { 
     if (ctx.TagId == HtmlTagId.Image && !ctx.IsEndTag && stack.Count > 0) { 
      ctx.WriteTag (htmlWriter, false); 

      // replace the src attribute with a file:// URL 
      foreach (var attribute in ctx.Attributes) { 
       if (attribute.Id == HtmlAttributeId.Src) { 
        MimePart image; 
        string url; 

        if (!TryGetImage (attribute.Value, out image)) { 
         htmlWriter.WriteAttribute (attribute); 
         continue; 
        } 

        url = GetDataImageSrc (image); 

        htmlWriter.WriteAttributeName (attribute.Name); 
        htmlWriter.WriteAttributeValue (url); 
       } else { 
        htmlWriter.WriteAttribute (attribute); 
       } 
      } 
     } else if (ctx.TagId == HtmlTagId.Body && !ctx.IsEndTag) { 
      ctx.WriteTag (htmlWriter, false); 

      // add and/or replace oncontextmenu="return false;" 
      foreach (var attribute in ctx.Attributes) { 
       if (attribute.Name.ToLowerInvariant() == "oncontextmenu") 
        continue; 

       htmlWriter.WriteAttribute (attribute); 
      } 

      htmlWriter.WriteAttribute ("oncontextmenu", "return false;"); 
     } else { 
      // pass the tag through to the output 
      ctx.WriteTag (htmlWriter, true); 
     } 
    } 

    protected override void VisitTextPart (TextPart entity) 
    { 
     TextConverter converter; 

     if (body != null) { 
      // since we've already found the body, treat this as an attachment 
      attachments.Add (entity); 
      return; 
     } 

     if (entity.IsHtml) { 
      converter = new HtmlToHtml { 
       HtmlTagCallback = HtmlTagCallback 
      }; 
     } else if (entity.IsFlowed) { 
      var flowed = new FlowedToHtml(); 
      string delsp; 

      if (entity.ContentType.Parameters.TryGetValue ("delsp", out delsp)) 
       flowed.DeleteSpace = delsp.ToLowerInvariant() == "yes"; 

      converter = flowed; 
     } else { 
      converter = new TextToHtml(); 
     } 

     body = converter.Convert (entity.Text); 
    } 

    protected override void VisitTnefPart (TnefPart entity) 
    { 
     // extract any attachments in the MS-TNEF part 
     attachments.AddRange (entity.ExtractAttachments()); 
    } 

    protected override void VisitMessagePart (MessagePart entity) 
    { 
     // treat message/rfc822 parts as attachments 
     attachments.Add (entity); 
    } 

    protected override void VisitMimePart (MimePart entity) 
    { 
     // realistically, if we've gotten this far, then we can treat this as an attachment 
     // even if the IsAttachment property is false. 
     attachments.Add (entity); 
    } 
} 

使用此HtmlPreviewVisitor的方法是这样的:

void Render (MimeMessage message) 
{ 
    var visitor = new HtmlPreviewVisitor(); 

    message.Accept (visitor); 

    plhMessage.Controls.Add (new LiteralControl (visitor.HtmlBody)); 
} 

注:如果报文中的HTML您打算渲染所有参考 网页的URL为自己的形象,你可以逃脱只是渲染了字符串 message.HtmlBody。即使图像嵌入在消息本身内,HtmlPreviewVisitor解决方案,但是, 仍然可以工作。

+0

非常thanks.it工作。 – shahroz

+0

如何保存附件邮件? – shahroz

+0

在上面的代码片段中,'HtmlPreviewVisitor.Attachments'是附件列表。有两种类型的'MimeEntity'子类需要担心:'MimePart'(最常见)和'MessagePart'(其中包含message/rfc822部分)。上面的GetDataImageSrc方法显示了如何保存附件的内容(只需使用'FileStream'而不是'MemoryStream')。要保存'MessagePart'的内容,只需执行'messagePart.Message。WriteTo(流);' – jstedfast