How to get PlainText or Html Text using AE.Net.Mai

2019-07-26 00:17发布

问题:

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;


标签: c# email pop3