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

關聯工具:Spire.Email for .NEThtml

代碼片斷:工具

Step 1:加載郵件。ui

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

Step 2:建立一個新的StringBuilder實例。htm

StringBuilder sb = new StringBuilder();

Step 3:獲取咱們想要的消息內容。blog

//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文件格式的消息內容以下:get

圖片1

完整代碼:it

[C#]date

{
    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]foreach

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())

慧都控件網

相關文章
相關標籤/搜索