Aspose.Email - 处理MIME Email文件

通过Aspose.Email包,可以方便地对邮件文件进行处理,如:读取邮件信息、获取内嵌HTML等

Aspose.Email - 处理MIME Email文件

1 example

以Microsoft Outlook 2013邮件文件格式(*.msg)为例,邮件Body是一个内嵌的HTML页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using Aspose.Email;
using System.IO;

namespace EmailTest
{
class EmailTest
{
static void Main(string[] args)
{
// Create MailMessage instance by loading an Eml file
MailMessage message = MailMessage.Load(@"C:\Dev\Mail\Mail.msg");

// Print From, To(es), CC(s)
Console.WriteLine("From: " + message.From.DisplayName + "<" + message.From.Address + ">");
foreach(var toAddr in message.To)
{
Console.WriteLine("To: " + toAddr.DisplayName + "<" + toAddr.Address + ">");
}
foreach (var ccAddr in message.CC)
{
Console.WriteLine("CC: " + ccAddr.DisplayName + "<" + ccAddr.Address + ">");
}
// Print Subject and Date(Time)
Console.WriteLine("Subject: " + message.Subject);
Console.WriteLine("DateTime: " + message.Date.ToString());

// pause
Console.ReadKey();

// Print message body
Console.WriteLine("Body:");
if (message.IsBodyHtml)
{
Console.WriteLine(message.HtmlBody);

// Save HTML message body as a page
FileStream outFile = File.OpenWrite(@"C:\Dev\Mail\MailBody.html");
StreamWriter writer = new StreamWriter(outFile);
writer.Write(message.HtmlBody);
writer.Close();
}
else
{
Console.WriteLine(message.Body);
}

// Print and save mail's attachments
Console.WriteLine("Attachments:");
foreach (var attach in message.Attachments)
{
Console.WriteLine(attach.Name + attach.ContentType);
attach.Save(@"C:\Dev\Mail\" + attach.Name);
}
}
}
}