forked from andyedinborough/aenetmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attachment.cs
57 lines (46 loc) · 2.04 KB
/
Attachment.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
using System;
using System.ComponentModel;
namespace AE.Net.Mail {
public class Attachment : ObjectWHeaders {
public string Filename {
get { return Headers["Content-Disposition"]["filename"]; }
}
[Obsolete("Use ContentTransferEncoding instead"), EditorBrowsable(EditorBrowsableState.Never)]
public string ContentEncoding { get { return ContentTransferEncoding; } set { ContentTransferEncoding = value; } }
[Obsolete("Use Body instead"), EditorBrowsable(EditorBrowsableState.Never)]
public string Content { get { return Body; } set { SetBody(value); } }
[Obsolete("Use GetData instead"), EditorBrowsable(EditorBrowsableState.Never)]
public byte[] GetContent() { return GetData(); }
private string _ContentDisposition;
private string ContentDisposition {
get { return _ContentDisposition ?? (_ContentDisposition = Headers["Content-Disposition"].Value.ToLower()); }
}
public bool OnServer { get; internal set; }
public bool IsAttachment {
get {
return ContentDisposition == "attachment" || ContentDisposition == "inline";
}
}
public void Save(string filename) {
using (var file = new System.IO.FileStream(filename, System.IO.FileMode.Create))
Save(file);
}
public void Save(System.IO.Stream stream) {
var data = GetData();
stream.Write(data, 0, data.Length);
}
public byte[] GetData() {
byte[] data;
if (ContentTransferEncoding.Is("base64") && Utilities.IsValidBase64String(Body)) {
try {
data = Convert.FromBase64String(Body);
} catch (Exception) {
data = System.Text.Encoding.UTF8.GetBytes(Body);
}
} else {
data = System.Text.Encoding.UTF8.GetBytes(Body);
}
return data;
}
}
}