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

标签: c# email pop3
4条回答
Animai°情兽
2楼-- · 2019-07-26 01:07

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

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-26 01:12

you must set headers only to false

Mail Sample

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

                myM.Date = item.Date;

                myM.Body = item.Body;
查看更多
地球回转人心会变
4楼-- · 2019-07-26 01:15

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

查看更多
爷的心禁止访问
5楼-- · 2019-07-26 01:16

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.

查看更多
登录 后发表回答