forked from jstedfast/MimeKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachmentExamples.cs
76 lines (64 loc) · 1.91 KB
/
AttachmentExamples.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using MimeKit;
namespace MimeKit.Examples
{
public static class AttachmentExamples
{
public static void SaveMimePart (MimePart attachment, string fileName)
{
#region SaveMimePart
using (var stream = File.Create (fileName))
attachment.Content.DecodeTo (stream);
#endregion SaveMimePart
}
public static void SaveMimePart (MessagePart attachment, string fileName)
{
#region SaveMessagePart
using (var stream = File.Create (fileName))
attachment.Message.WriteTo (stream);
#endregion SaveMessagePart
}
public static void SaveAttachments (MimeMessage message)
{
#region SaveAttachments
foreach (var attachment in message.Attachments) {
if (attachment is MessagePart) {
var fileName = attachment.ContentDisposition?.FileName;
var rfc822 = (MessagePart) attachment;
if (string.IsNullOrEmoty (fileName))
fileName = "attached-message.eml";
using (var stream = File.Create (fileName))
rfc822.Message.WriteTo (stream);
} else {
var part = (MimePart) attachment;
var fileName = part.FileName;
using (var stream = File.Create (fileName))
part.Content.DecodeTo (stream);
}
}
#endregion SaveAttachments
}
public static void SaveAttachments (MimeMessage message)
{
#region SaveBodyParts
foreach (var bodyPart in message.BodyParts) {
if (!bodyPart.IsAttachment)
continue;
if (bodyPart is MessagePart) {
var fileName = attachment.ContentDisposition?.FileName;
var rfc822 = (MessagePart) attachment;
if (string.IsNullOrEmoty (fileName))
fileName = "attached-message.eml";
using (var stream = File.Create (fileName))
rfc822.Message.WriteTo (stream);
} else {
var part = (MimePart) attachment;
var fileName = part.FileName;
using (var stream = File.Create (fileName))
part.Content.DecodeTo (stream);
}
}
#endregion SaveBodyParts
}
}
}