2012-06-02 85 views
1

我想使用AE.Net.Mail获取纯文本或HTML文本的电子邮件。 我找不到任何文档。如何使用AE.Net.Mail获取PlainText或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; 
     } 
    } 

回答

6

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

Firt创建一个类:

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别人身体的格式的纯

0

如果您MailMessageSystem.Net.Mail一个,你可以从Body财产得到的消息体。它可以是HTML或纯文本,具体取决于IsBodyHtml属性。 AlternateViews属性也可能包含邮件正文的其他形式。

+0

在AE.Net.Mail中,没有'IsBodyHtml'属性。对于'Body'属性中的所有消息,我都有纯文本的消息。 对于一个消息,存在2'AlternateViews': [0]与'ContentType' “texte /纯” [1]与'ContentType' “texte/HTML” 对于其他消息没有'AlternateViews'但2'Attachements' 对于其他消息,没有'AlternateView's并且没有'Attachements'。'''''''''''''''''''' 我如何获取html文本? – LeMoussel

+0

您可以从其“ContentType”为“text/plain”''AlternateView'的HTML主体获取;你可能需要从'ContentStream'中读取它。 – Douglas

+0

AE.Net.Mail中的'AlternateView'没有'ContentStream'方法! – LeMoussel

0

必须设置头只

Mail Sample

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

       myM.Date = item.Date; 

       myM.Body = item.Body; 
+0

我不认为这回答了这个问题。标题仅意味着它只能提取电子邮件标题,例如从,到,主题,日期等。它不会下载正文内容或附件。 – angularsen

2

体= E .Value.AlternateViews.GetHtmlView()。Body,