【教程】如何在C#,VB.NET中提取消息内容

关联工具:Spire.Email for .NET


代码片段:

Step 1:加载邮件。

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

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

StringBuilder sb = new StringBuilder();

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

//get the From address
sb.AppendLine("From:");
sb.AppendLine(mail.From.Address);
//get the To address
sb.AppendLine("To:");
foreach (MailAddress toAddress in mail.To)
{
    sb.AppendLine(toAddress.Address);
}
//get the date
sb.AppendLine("Date:");
sb.AppendLine(mail.Date.ToString());
//get the subject
sb.AppendLine("Subject:");
sb.AppendLine(mail.Subject);
//get the BodyText
sb.AppendLine("Message contents");
sb.AppendLine(mail.BodyText);
//get the BodyHtml
sb.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)
Next
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())

慧都控件网




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值