How to get PlainText or Html Text using AE.Net.Mail?

北城余情 提交于 2019-12-24 09:15:52

问题


I'm trying to get Plain Text or HTML Text of email using AE.Net.Mail. I cannot find any documentation.

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

Edit : I found this solution

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

回答1:


I solved this problem with this method:

Firt create a class:

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

Than:

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

And check has "text/html":

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

And if has "text/html", body's format html else body's format plain




回答2:


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




回答3:


If your MailMessage is the System.Net.Mail one, you can get the message body from the Body property. It may be either HTML or plain-text depending on the IsBodyHtml property. The AlternateViews property may also contain alternate forms of the message body.




回答4:


you must set headers only to false

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

                myM.Date = item.Date;

                myM.Body = item.Body;


来源:https://stackoverflow.com/questions/10866096/how-to-get-plaintext-or-html-text-using-ae-net-mail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!