Spire.Email 教程:如何在C#,VB.NET中提取消息内容

本教程介绍了如何使用Spire.Email从地址、发送地址、主题、日期和消息的正文获取消息内容。

关联工具:Spire.Email for .NET


代码片段:

Step 1:加载邮件。

MailMessage mail = MailMessage.Load("Sample.msg");

Step 2:创建一个新的StringBuilder实例。

StringBuilder sb = new StringBuilder();

Step 3:获取我们想要的消息内容。

//get the From addresssb.AppendLine("From:");sb.AppendLine(mail.From.Address);//get the To addresssb.AppendLine("To:");foreach (MailAddress toAddress in mail.To){    sb.AppendLine(toAddress.Address);}//get the datesb.AppendLine("Date:");sb.AppendLine(mail.Date.ToString());//get the subjectsb.AppendLine("Subject:");sb.AppendLine(mail.Subject);//get the BodyTextsb.AppendLine("Message contents");sb.AppendLine(mail.BodyText);//get the BodyHtmlsb.AppendLine("BodyHtml");sb.AppendLine(mail.BodyHtml);

Step 4:写入.txt中的所有内容

File.WriteAllText("ExtractMessageContents.txt", sb.ToString());

提取的.text文件格式的消息内容如下:

图片1

完整代码:

[C#]

{    MailMessage mail = MailMessage.Load("Sample.msg");    StringBuilder sb = new StringBuilder();    sb.AppendLine("From:");    sb.AppendLine(mail.From.Address);    sb.AppendLine("To:");    foreach (MailAddress toAddress in mail.To)    {        sb.AppendLine(toAddress.Address);    }    sb.AppendLine("Date:");    sb.AppendLine(mail.Date.ToString());    sb.AppendLine("Subject:");    sb.AppendLine(mail.Subject);    sb.AppendLine("Message contents");    sb.AppendLine(mail.BodyText);    sb.AppendLine("BodyHtml");    sb.AppendLine(mail.BodyHtml);    File.WriteAllText("ExtractMessageContents.txt", sb.ToString());}

[VB.NET]

Dim mail As MailMessage = MailMessage.Load("Sample.msg")Dim sb As New StringBuilder()sb.AppendLine("From:")sb.AppendLine(mail.From.Address)sb.AppendLine("To:")For Each toAddress As MailAddress In mail.[To]    sb.AppendLine(toAddress.Address)Nextsb.AppendLine("Date:")sb.AppendLine(mail.[Date].ToString())sb.AppendLine("Subject:")sb.AppendLine(mail.Subject)sb.AppendLine("Message contents")sb.AppendLine(mail.BodyText)sb.AppendLine("BodyHtml")sb.AppendLine(mail.BodyHtml)File.WriteAllText("ExtractMessageContents.txt", sb.ToString())

控件网

热门格式转换工具在线订购特惠中点击查看>>>>


图片1
标签:电子邮件

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年6月13日
下一篇 2017年6月13日

相关推荐

发表回复

登录后才能评论