Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AttachmentBase.cs
Go to the documentation of this file.
2using System.IO;
4using System.Text;
5
6namespace System.Net.Mail;
7
8public abstract class AttachmentBase : IDisposable
9{
10 internal bool disposed;
11
12 private readonly MimePart _part = new MimePart();
13
14 private static readonly char[] s_contentCIDInvalidChars = new char[2] { '<', '>' };
15
17 {
18 get
19 {
20 if (disposed)
21 {
22 throw new ObjectDisposedException(GetType().FullName);
23 }
24 return _part.Stream;
25 }
26 }
27
28 public string ContentId
29 {
30 get
31 {
32 string contentID = _part.ContentID;
33 if (string.IsNullOrEmpty(contentID))
34 {
35 return ContentId = Guid.NewGuid().ToString();
36 }
37 if (contentID.Length >= 2 && contentID[0] == '<' && contentID[contentID.Length - 1] == '>')
38 {
39 return contentID.Substring(1, contentID.Length - 2);
40 }
41 return contentID;
42 }
43 [param: AllowNull]
44 set
45 {
46 if (string.IsNullOrEmpty(value))
47 {
48 _part.ContentID = null;
49 return;
50 }
51 if (value.IndexOfAny(s_contentCIDInvalidChars) != -1)
52 {
54 }
55 _part.ContentID = "<" + value + ">";
56 }
57 }
58
60 {
61 get
62 {
63 return _part.ContentType;
64 }
65 set
66 {
67 _part.ContentType = value;
68 }
69 }
70
72 {
73 get
74 {
76 }
77 set
78 {
79 _part.TransferEncoding = value;
80 }
81 }
82
84 {
85 get
86 {
87 if (!Uri.TryCreate(_part.ContentLocation, UriKind.RelativeOrAbsolute, out Uri result))
88 {
89 return null;
90 }
91 return result;
92 }
93 set
94 {
95 _part.ContentLocation = ((value == null) ? null : (value.IsAbsoluteUri ? value.AbsoluteUri : value.OriginalString));
96 }
97 }
98
99 internal MimePart MimePart => _part;
100
101 internal AttachmentBase()
102 {
103 }
104
105 protected AttachmentBase(string fileName)
106 {
107 SetContentFromFile(fileName, string.Empty);
108 }
109
110 protected AttachmentBase(string fileName, string? mediaType)
111 {
112 SetContentFromFile(fileName, mediaType);
113 }
114
115 protected AttachmentBase(string fileName, ContentType? contentType)
116 {
117 SetContentFromFile(fileName, contentType);
118 }
119
120 protected AttachmentBase(Stream contentStream)
121 {
122 _part.SetContent(contentStream);
123 }
124
125 protected AttachmentBase(Stream contentStream, string? mediaType)
126 {
127 _part.SetContent(contentStream, null, mediaType);
128 }
129
130 internal AttachmentBase(Stream contentStream, string name, string mediaType)
131 {
132 _part.SetContent(contentStream, name, mediaType);
133 }
134
135 protected AttachmentBase(Stream contentStream, ContentType? contentType)
136 {
137 _part.SetContent(contentStream, contentType);
138 }
139
140 public void Dispose()
141 {
142 Dispose(disposing: true);
143 }
144
145 protected virtual void Dispose(bool disposing)
146 {
147 if (disposing && !disposed)
148 {
149 disposed = true;
150 _part.Dispose();
151 }
152 }
153
154 internal void SetContentFromFile(string fileName, ContentType contentType)
155 {
156 if (fileName == null)
157 {
158 throw new ArgumentNullException("fileName");
159 }
160 if (fileName.Length == 0)
161 {
162 throw new ArgumentException(System.SR.Format(System.SR.net_emptystringcall, "fileName"), "fileName");
163 }
164 Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
165 _part.SetContent(stream, contentType);
166 }
167
168 internal void SetContentFromFile(string fileName, string mediaType)
169 {
170 if (fileName == null)
171 {
172 throw new ArgumentNullException("fileName");
173 }
174 if (fileName.Length == 0)
175 {
176 throw new ArgumentException(System.SR.Format(System.SR.net_emptystringcall, "fileName"), "fileName");
177 }
178 Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
179 _part.SetContent(stream, null, mediaType);
180 }
181
182 internal void SetContentFromString(string content, ContentType contentType)
183 {
184 if (content == null)
185 {
186 throw new ArgumentNullException("content");
187 }
188 if (_part.Stream != null)
189 {
191 }
192 Encoding encoding = ((contentType != null && contentType.CharSet != null) ? Encoding.GetEncoding(contentType.CharSet) : ((!MimeBasePart.IsAscii(content, permitCROrLF: false)) ? Encoding.GetEncoding("utf-8") : Encoding.ASCII));
193 byte[] bytes = encoding.GetBytes(content);
194 _part.SetContent(new MemoryStream(bytes), contentType);
196 {
197 _part.TransferEncoding = TransferEncoding.Base64;
198 }
199 else
200 {
201 _part.TransferEncoding = TransferEncoding.QuotedPrintable;
202 }
203 }
204
205 internal void SetContentFromString(string content, Encoding encoding, string mediaType)
206 {
207 if (content == null)
208 {
209 throw new ArgumentNullException("content");
210 }
211 if (_part.Stream != null)
212 {
214 }
215 if (string.IsNullOrEmpty(mediaType))
216 {
217 mediaType = "text/plain";
218 }
219 int offset = 0;
220 try
221 {
222 string text = MailBnfHelper.ReadToken(mediaType, ref offset, null);
223 if (text.Length == 0 || offset >= mediaType.Length || mediaType[offset++] != '/')
224 {
225 throw new ArgumentException(System.SR.MediaTypeInvalid, "mediaType");
226 }
227 text = MailBnfHelper.ReadToken(mediaType, ref offset, null);
228 if (text.Length == 0 || offset < mediaType.Length)
229 {
230 throw new ArgumentException(System.SR.MediaTypeInvalid, "mediaType");
231 }
232 }
233 catch (FormatException)
234 {
235 throw new ArgumentException(System.SR.MediaTypeInvalid, "mediaType");
236 }
237 ContentType contentType = new ContentType(mediaType);
238 if (encoding == null)
239 {
240 encoding = ((!MimeBasePart.IsAscii(content, permitCROrLF: false)) ? Encoding.GetEncoding("utf-8") : Encoding.ASCII);
241 }
242 contentType.CharSet = encoding.BodyName;
243 byte[] bytes = encoding.GetBytes(content);
244 _part.SetContent(new MemoryStream(bytes), contentType);
246 {
247 _part.TransferEncoding = TransferEncoding.Base64;
248 }
249 else
250 {
251 _part.TransferEncoding = TransferEncoding.QuotedPrintable;
252 }
253 }
254
255 internal virtual void PrepareForSending(bool allowUnicode)
256 {
258 }
259}
virtual void Close()
Definition Stream.cs:644
void SetContentFromString(string content, ContentType contentType)
AttachmentBase(string fileName, string? mediaType)
AttachmentBase(Stream contentStream, string? mediaType)
AttachmentBase(Stream contentStream, ContentType? contentType)
virtual void Dispose(bool disposing)
void SetContentFromFile(string fileName, ContentType contentType)
virtual void PrepareForSending(bool allowUnicode)
static readonly char[] s_contentCIDInvalidChars
AttachmentBase(Stream contentStream, string name, string mediaType)
void SetContentFromString(string content, Encoding encoding, string mediaType)
AttachmentBase(string fileName, ContentType? contentType)
void SetContentFromFile(string fileName, string mediaType)
AttachmentBase(Stream contentStream)
static string ReadToken(string data, ref int offset, StringBuilder builder)
static bool IsAscii(string value, bool permitCROrLF)
static bool ShouldUseBase64Encoding(Encoding encoding)
TransferEncoding TransferEncoding
Definition MimePart.cs:66
void SetContent(Stream stream)
Definition MimePart.cs:119
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string MailHeaderInvalidCID
Definition SR.cs:60
static string MediaTypeInvalid
Definition SR.cs:56
static string net_emptystringcall
Definition SR.cs:14
Definition SR.cs:7
static Encoding GetEncoding(int codepage)
Definition Encoding.cs:593
static Encoding ASCII
Definition Encoding.cs:511
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
virtual string BodyName
Definition Encoding.cs:350
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
UriKind
Definition UriKind.cs:4
static Guid NewGuid()
Definition Guid.cs:1283