【问题标题】:How to get PlainText or Html Text using AE.Net.Mail?如何使用 AE.Net.Mail 获取纯文本或 Html 文本?
【发布时间】:2012-06-07 15:04:57
【问题描述】:

我正在尝试使用 AE.Net.Mail 获取电子邮件的纯文本或 HTML 文本。 我找不到任何文档。

using (Pop3Client pop3 = new AE.Net.Mail.Pop3Client("pop3Server", "login", "pwd", 995, true))
{
   for (var i = pop3.GetMessageCount() - 1; i >= 0; i--)
   {
      MailMessage Msg = pop3.GetMessage(i);
      string HtmlText = Msg.??????
      string PlainText = Msg.??????                
   }
}

编辑:我找到了这个解决方案

   IList<Attachment> iList;
   string HtmlText = "", PlainText = "";

   for (var i = pop3.GetMessageCount() - 1; i >= 0; i--)
   {
      MailMessage msg = pop3.GetMessage(i);
      TextBody = msg.Body;
      iList = msg.AlternateViews as IList<Attachment>;
      if (iList.Count == 0) iList = msg.Attachments as IList<Attachment>;
      if (iList.Count > 0)
      {
           TextBody = iList[0].Body;
           HtmlBody = iList[1].Body;
      }
   }

【问题讨论】:

    标签: c# email pop3


    【解决方案1】:

    我用这个方法解决了这个问题:

    首先创建一个类:

        class BodyContent
      {
        public string ContentType { get; set; }
        public string Body { get; set; }
      }
    

    比:

    List<BodyContent> bodies = new List<BodyContent>();
      foreach (AE.Net.Mail.Attachment item in mail.AlternateViews)
      {
        bodies.Add(new BodyContent
        {
          ContentType = item.ContentType,
          Body = item.Body
        });
      }
    

    并且检查有“text/html”:

    string body="";
          BodyContent bodyTemp= bodies.Find(x => x.ContentType == "text/html");
          if (bodyTemp== null)
            body = mail.Body;
          else
            body = bodyTemp.Body;
    

    如果有“text/html”,则正文格式为html,否则正文格式为普通格式

    【讨论】:

      【解决方案2】:

      Body = e.Value.AlternateViews.GetHtmlView().Body,

      【讨论】:

        【解决方案3】:

        如果您的MailMessageSystem.Net.Mail 之一,您可以从Body 属性中获取消息正文。它可能是 HTML 或纯文本,具体取决于 IsBodyHtml 属性。 AlternateViews 属性还可以包含消息正文的替代形式。

        【讨论】:

        • 在 AE.Net.Mail 中,没有 IsBodyHtml 属性。对于Body 属性中的所有消息,我有消息的纯文本。对于一条消息,有 2 个 AlternateViews : [0] 和 ContentType "texte/plain" [1] 和 ContentType "texte/html" 对于其他消息,没有 AlternateViews 但 2 Attachements [0 ] with ContentType "texte/plain" [1] with ContentType "texte/html" 对于其他消息,没有AlternateViews 和Attachements。如何获取 html 文本?
        • 你可以从AlternateView获取HTML body,ContentType"text/plain";您可能需要从ContentStream 阅读它。
        • AE.Net.Mail 中没有AlternateViewContentStream 方法!
        【解决方案4】:

        您必须将 headers only 设置为 false

         foreach (var item in mm)
                    {
                        Email myM = new Email();
        
                        myM.Date = item.Date;
        
                        myM.Body = item.Body;
        

        【讨论】:

        • 我认为这不能回答问题。 Headers only 意味着它只获取电子邮件标题,例如 from、to、subject、date 等。它不下载正文内容或附件。
        猜你喜欢
        • 2021-08-21
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-25
        • 2011-10-08
        相关资源
        最近更新 更多