Skip to content

Commit 5b0b07c

Browse files
author
krobertson
committedOct 13, 2009
Importing the latest Papercut code
1 parent 1bad46a commit 5b0b07c

21 files changed

+3744
-0
lines changed
 

‎Mime/MailHeaders.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Net.Mail
2+
{
3+
public static class MailHeaders
4+
{
5+
public const string Bcc = "bcc";
6+
7+
public const string Cc = "cc";
8+
9+
public const string Date = "date";
10+
11+
public const string From = "from";
12+
13+
public const string Importance = "importance";
14+
15+
public const string InReplyTo = "in-reply-to";
16+
17+
public const string MessageId = "message-id";
18+
19+
public const string Received = "received";
20+
21+
public const string ReplyTo = "reply-to";
22+
23+
public const string Subject = "subject";
24+
25+
public const string To = "to";
26+
}
27+
}

‎Mime/MailMessageEx.cs

+304
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
4+
using System.Net.Mail;
5+
using System.Net.Mime;
6+
using Net.Mime;
7+
8+
namespace Net.Mail
9+
{
10+
/// <summary>
11+
/// This class adds a few internet mail headers not already exposed by the
12+
/// System.Net.MailMessage. It also provides support to encapsulate the
13+
/// nested mail attachments in the Children collection.
14+
/// </summary>
15+
public class MailMessageEx : MailMessage
16+
{
17+
public const string EmailRegexPattern = "(['\"]{1,}.+['\"]{1,}\\s+)?<?[\\w\\.\\-]+@[^\\.][\\w\\.\\-]+\\.[a-z]{2,}>?";
18+
19+
private long _octets;
20+
21+
public long Octets
22+
{
23+
get { return _octets; }
24+
set { _octets = value; }
25+
}
26+
27+
private int _messageNumber;
28+
29+
/// <summary>
30+
/// Gets or sets the message number of the MailMessage on the POP3 server.
31+
/// </summary>
32+
/// <value>The message number.</value>
33+
public int MessageNumber
34+
{
35+
get { return _messageNumber; }
36+
internal set { _messageNumber = value; }
37+
}
38+
39+
40+
private static readonly char[] AddressDelimiters = new char[] { ',', ';' };
41+
42+
private List<MailMessageEx> _children;
43+
/// <summary>
44+
/// Gets the children MailMessage attachments.
45+
/// </summary>
46+
/// <value>The children MailMessage attachments.</value>
47+
public List<MailMessageEx> Children
48+
{
49+
get
50+
{
51+
return _children;
52+
}
53+
}
54+
55+
/// <summary>
56+
/// Gets the delivery date.
57+
/// </summary>
58+
/// <value>The delivery date.</value>
59+
public DateTime DeliveryDate
60+
{
61+
get
62+
{
63+
string date = GetHeader(MailHeaders.Date);
64+
if (string.IsNullOrEmpty(date))
65+
{
66+
return DateTime.MinValue;
67+
}
68+
69+
return Convert.ToDateTime(date);
70+
}
71+
}
72+
73+
/// <summary>
74+
/// Gets the return address.
75+
/// </summary>
76+
/// <value>The return address.</value>
77+
public MailAddress ReturnAddress
78+
{
79+
get
80+
{
81+
string replyTo = GetHeader(MailHeaders.ReplyTo);
82+
if (string.IsNullOrEmpty(replyTo))
83+
{
84+
return null;
85+
}
86+
87+
return CreateMailAddress(replyTo);
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Gets the routing.
93+
/// </summary>
94+
/// <value>The routing.</value>
95+
public string Routing
96+
{
97+
get { return GetHeader(MailHeaders.Received); }
98+
}
99+
100+
/// <summary>
101+
/// Gets the message id.
102+
/// </summary>
103+
/// <value>The message id.</value>
104+
public string MessageId
105+
{
106+
get { return GetHeader(MailHeaders.MessageId); }
107+
}
108+
109+
public string ReplyToMessageId
110+
{
111+
get { return GetHeader(MailHeaders.InReplyTo, true); }
112+
}
113+
114+
/// <summary>
115+
/// Gets the MIME version.
116+
/// </summary>
117+
/// <value>The MIME version.</value>
118+
public string MimeVersion
119+
{
120+
get { return GetHeader(MimeHeaders.MimeVersion); }
121+
}
122+
123+
/// <summary>
124+
/// Gets the content id.
125+
/// </summary>
126+
/// <value>The content id.</value>
127+
public string ContentId
128+
{
129+
get { return GetHeader(MimeHeaders.ContentId); }
130+
}
131+
132+
/// <summary>
133+
/// Gets the content description.
134+
/// </summary>
135+
/// <value>The content description.</value>
136+
public string ContentDescription
137+
{
138+
get { return GetHeader(MimeHeaders.ContentDescription); }
139+
}
140+
141+
/// <summary>
142+
/// Gets the content disposition.
143+
/// </summary>
144+
/// <value>The content disposition.</value>
145+
public ContentDisposition ContentDisposition
146+
{
147+
get
148+
{
149+
string contentDisposition = GetHeader(MimeHeaders.ContentDisposition);
150+
if (string.IsNullOrEmpty(contentDisposition))
151+
{
152+
return null;
153+
}
154+
155+
return new ContentDisposition(contentDisposition);
156+
}
157+
}
158+
159+
/// <summary>
160+
/// Gets the type of the content.
161+
/// </summary>
162+
/// <value>The type of the content.</value>
163+
public ContentType ContentType
164+
{
165+
get
166+
{
167+
string contentType = GetHeader(MimeHeaders.ContentType);
168+
if (string.IsNullOrEmpty(contentType))
169+
{
170+
return null;
171+
}
172+
173+
return MimeReader.GetContentType(contentType);
174+
}
175+
}
176+
177+
/// <summary>
178+
/// Initializes a new instance of the <see cref="MailMessageEx"/> class.
179+
/// </summary>
180+
public MailMessageEx()
181+
: base()
182+
{
183+
_children = new List<MailMessageEx>();
184+
}
185+
186+
/// <summary>
187+
/// Gets the header.
188+
/// </summary>
189+
/// <param name="header">The header.</param>
190+
/// <returns></returns>
191+
private string GetHeader(string header)
192+
{
193+
return GetHeader(header, false);
194+
}
195+
196+
private string GetHeader(string header, bool stripBrackets)
197+
{
198+
if (stripBrackets)
199+
{
200+
return MimeEntity.TrimBrackets(Headers[header]);
201+
}
202+
203+
return Headers[header];
204+
}
205+
206+
/// <summary>
207+
/// Creates the mail message from entity.
208+
/// </summary>
209+
/// <param name="entity">The entity.</param>
210+
/// <returns></returns>
211+
public static MailMessageEx CreateMailMessageFromEntity(MimeEntity entity)
212+
{
213+
MailMessageEx message = new MailMessageEx();
214+
string value;
215+
foreach (string key in entity.Headers.AllKeys)
216+
{
217+
value = entity.Headers[key];
218+
if (value.Equals(string.Empty))
219+
{
220+
value = " ";
221+
}
222+
223+
message.Headers.Add(key.ToLowerInvariant(), value);
224+
225+
switch (key.ToLowerInvariant())
226+
{
227+
case MailHeaders.Bcc:
228+
MailMessageEx.PopulateAddressList(value, message.Bcc);
229+
break;
230+
case MailHeaders.Cc:
231+
MailMessageEx.PopulateAddressList(value, message.CC);
232+
break;
233+
case MailHeaders.From:
234+
message.From = MailMessageEx.CreateMailAddress(value);
235+
break;
236+
case MailHeaders.ReplyTo:
237+
message.ReplyTo = MailMessageEx.CreateMailAddress(value);
238+
break;
239+
case MailHeaders.Subject:
240+
message.Subject = value;
241+
break;
242+
case MailHeaders.To:
243+
MailMessageEx.PopulateAddressList(value, message.To);
244+
break;
245+
}
246+
}
247+
248+
return message;
249+
}
250+
251+
/// <summary>
252+
/// Creates the mail address.
253+
/// </summary>
254+
/// <param name="address">The address.</param>
255+
/// <returns></returns>
256+
public static MailAddress CreateMailAddress(string address)
257+
{
258+
try
259+
{
260+
return new MailAddress(address.Trim('\t'));
261+
}
262+
catch (FormatException e)
263+
{
264+
throw new Exception("Unable to create mail address from provided string: " + address, e);
265+
}
266+
}
267+
268+
/// <summary>
269+
/// Populates the address list.
270+
/// </summary>
271+
/// <param name="addressList">The address list.</param>
272+
/// <param name="recipients">The recipients.</param>
273+
public static void PopulateAddressList(string addressList, MailAddressCollection recipients)
274+
{
275+
foreach (MailAddress address in GetMailAddresses(addressList))
276+
{
277+
recipients.Add(address);
278+
}
279+
}
280+
281+
/// <summary>
282+
/// Gets the mail addresses.
283+
/// </summary>
284+
/// <param name="addressList">The address list.</param>
285+
/// <returns></returns>
286+
private static IEnumerable<MailAddress> GetMailAddresses(string addressList)
287+
{
288+
Regex email = new Regex(EmailRegexPattern);
289+
290+
foreach (Match match in email.Matches(addressList))
291+
{
292+
yield return CreateMailAddress(match.Value);
293+
}
294+
295+
296+
/*
297+
string[] addresses = addressList.Split(AddressDelimiters);
298+
foreach (string address in addresses)
299+
{
300+
yield return CreateMailAddress(address);
301+
}*/
302+
}
303+
}
304+
}

‎Mime/MediaTypes.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Net.Mime;
2+
3+
namespace Net.Mime
4+
{
5+
public static class MediaTypes
6+
{
7+
public static readonly string Multipart;
8+
9+
public static readonly string Mixed;
10+
11+
public static readonly string Alternative;
12+
13+
public static readonly string MultipartMixed;
14+
15+
public static readonly string MultipartAlternative;
16+
17+
public static readonly string TextPlain;
18+
19+
public static readonly string TextHtml;
20+
21+
public static readonly string TextRich;
22+
23+
public static readonly string TextXml;
24+
25+
public static readonly string Message;
26+
27+
public static readonly string Rfc822;
28+
29+
public static readonly string MessageRfc822;
30+
31+
public static readonly string Application;
32+
33+
static MediaTypes()
34+
{
35+
Multipart = "multipart";
36+
Mixed = "mixed";
37+
Alternative = "alternative";
38+
Message = "message";
39+
Rfc822 = "rfc822";
40+
41+
MultipartMixed = string.Concat(Multipart, "/", Mixed);
42+
MultipartAlternative = string.Concat(Multipart, "/", Alternative);
43+
MessageRfc822 = string.Concat(Message, "/", Rfc822);
44+
45+
TextPlain = MediaTypeNames.Text.Plain;
46+
TextHtml = MediaTypeNames.Text.Html;
47+
TextRich = MediaTypeNames.Text.RichText;
48+
TextXml = MediaTypeNames.Text.Xml;
49+
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)