forked from smiley22/S22.Mail
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtensions.cs
More file actions
119 lines (114 loc) · 3.63 KB
/
Extensions.cs
File metadata and controls
119 lines (114 loc) · 3.63 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using System.Net.Mail;
using System.Text;
using S22.Imap;
namespace S22.Mail
{
/// <summary>
/// Contains extension methods for the MailMessage class of the System.Net.Mail
/// namespace.
/// </summary>
public static class MailExtension
{
/// <summary>
/// Saves the MailMessage instance to the specified stream.
/// </summary>
/// <param name="m">Extension method for MailMessage class.</param>
/// <param name="stream">
/// The stream the MailMessage data will be
/// written to.
/// </param>
/// <remarks>
/// The mail message is saved as plain text in MIME
/// format (known as .eml in many email clients)
/// </remarks>
public static void Save(this MailMessage m, Stream stream)
{
var bytes = Encoding.ASCII.GetBytes(m.ToMIME822());
stream.Write(bytes, 0, bytes.Length);
}
/// <summary>
/// Saves the MailMessage instance to the specified file.
/// </summary>
/// <param name="m">Extension method for MailMessage class</param>
/// <param name="name">
/// A relative or absolute path for the file the
/// MailMessage will be saved to.
/// </param>
/// <remarks>
/// The mail message is saved as plain text in MIME
/// format (known as .eml in many email clients)
/// </remarks>
public static void Save(this MailMessage m, string name)
{
using (var s = new FileStream(name, FileMode.Create))
m.Save(s);
}
/// <summary>
/// Creates a MailMessage instance from the specified stream.
/// </summary>
/// <param name="stream">
/// The stream the MailMessage will be constructed
/// from.
/// </param>
/// <returns>An initialized MailMessage object</returns>
public static MailMessage Load(Stream stream)
{
using (var r = new StreamReader(stream))
{
return MessageBuilder.FromMIME822(r.ReadToEnd());
}
}
/// <summary>
/// Creates a MailMessage instance from the specified file (*.eml).
/// </summary>
/// <param name="name">
/// A relative or absolute path for the file the
/// MailMessage will be saved to.
/// </param>
/// <returns>An initialized MailMessage object</returns>
public static MailMessage Load(string name)
{
using (var s = new FileStream(name, FileMode.Open))
return Load(s);
}
/// <summary>
/// Saves the contents of the attachment to the specified file.
/// </summary>
/// <param name="attachment">Extension method for Attachment class</param>
/// <param name="name">The file to save the attachment to.</param>
/// <exception cref="IOException">
/// Thrown when an I/O error occurs during
/// the save operation. Use the InnerException property to obtain the exception that
/// led to the current exception.
/// </exception>
/// <remarks>
/// If the file does not exist, it will be created. If the file already
/// exists, it will be truncated and overwritten.
/// </remarks>
public static void SaveAs(this Attachment attachment, string name)
{
int count;
var buffer = new byte[4096];
var stream = attachment.ContentStream;
try
{
using (var fs = new FileStream(name, FileMode.Create))
{
while ((count = stream.Read(buffer, 0, buffer.Length)) != 0)
fs.Write(buffer, 0, count);
}
}
catch (Exception e)
{
throw new IOException(e.Message, e);
}
finally
{
if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
}
}
}
}