我正在尝试使用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.??????
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我找到了这个解决方案
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我用这个方法解决了这个问题:
Firt创建一个类:
class BodyContent
{
public string ContentType { get; set; }
public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
比:
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
});
}
Run Code Online (Sandbox Code Playgroud)
并检查有"text/html":
string body="";
BodyContent bodyTemp= bodies.Find(x => x.ContentType == "text/html");
if (bodyTemp== null)
body = mail.Body;
else
body = bodyTemp.Body;
Run Code Online (Sandbox Code Playgroud)
如果有"text/html",正文的格式为html else body的格式为plain
| 归档时间: |
|
| 查看次数: |
5270 次 |
| 最近记录: |