-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Email.cs
416 lines (368 loc) · 18.3 KB
/
Email.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//
// Email.cs
//
// Author: Kees van Spelde <sicos2002@hotmail.com>
//
// Copyright (c) 2015-2017 Magic-Sessions. (www.magic-sessions.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using MsgKit.Enums;
using MsgKit.Helpers;
using MsgKit.Streams;
using OpenMcdf;
using Stream = System.IO.Stream;
namespace MsgKit
{
/// <summary>
/// A class used to make a new Outlook E-mail MSG file
/// </summary>
/// <remarks>
/// See https://msdn.microsoft.com/en-us/library/office/cc979231.aspx
/// </remarks>
public class Email : Message
{
#region Fields
/// <summary>
/// The E-mail <see cref="Recipients" />
/// </summary>
private Recipients _recipients;
/// <summary>
/// The E-mail <see cref="Attachments" />
/// </summary>
private Attachments _attachments;
/// <summary>
/// The subject of the E-mail
/// </summary>
private string _subject;
#endregion
#region Properties
/// <summary>
/// Returns the sender of the E-mail from the <see cref="Recipients" />
/// </summary>
public Sender Sender { get; private set; }
/// <summary>
/// Contains the e-mail address for the messaging user represented by the <see cref="Sender"/>.
/// </summary>
/// <remarks>
/// These properties are examples of the address properties for the messaging user who is being represented by the
/// <see cref="Receiving" /> user. They must be set by the incoming transport provider, which is also responsible for
/// authorization or
/// verification of the delegate. If no messaging user is being represented, these properties should be set to the
/// e-mail address contained in the PR_RECEIVED_BY_EMAIL_ADDRESS (PidTagReceivedByEmailAddress) property.
/// </remarks>
public Representing Representing { get; private set; }
/// <summary>
/// Returns the E-mail <see cref="Recipients" />
/// </summary>
public Recipients Recipients
{
get { return _recipients ?? (_recipients = new Recipients()); }
}
/// <summary>
/// Contains the e-mail address for the messaging user who receives the message.
/// </summary>
/// <remarks>
/// These properties are examples of the address properties for the messaging user who receives the message. They must
/// be set by the incoming transport provider.
/// </remarks>
public Receiving Receiving { get; set; }
/// <summary>
/// Contains the e-mail address for the messaging user who is represented by the <see cref="Receiving"/> user.
/// </summary>
/// <remarks>
/// These properties are examples of the address properties for the messaging user who is being represented by the
/// <see cref="Receiving" /> user. They must be set by the incoming transport provider, which is also responsible for authorization or
/// verification of the delegate. If no messaging user is being represented, these properties should be set to the
/// e-mail address contained in the PR_RECEIVED_BY_EMAIL_ADDRESS (PidTagReceivedByEmailAddress) property.
/// </remarks>
public ReceivingRepresenting ReceivingRepresenting { get; internal set; }
/// <summary>
/// Returns the subject prefix of the E-mail
/// </summary>
public string SubjectPrefix { get; private set; }
/// <summary>
/// Returns or sets the subject of the E-mail
/// </summary>
public string Subject
{
get { return _subject; }
set
{
_subject = value;
SetSubject();
}
}
/// <summary>
/// Returns the normalized subject of the E-mail
/// </summary>
public string SubjectNormalized { get; private set; }
/// <summary>
/// Returns or sets the <see cref="MessagePriority"/>
/// </summary>
public MessagePriority Priority { get; set; }
/// <summary>
/// Returns or sets the <see cref="MessageImportance"/>
/// </summary>
public MessageImportance Importance { get; set; }
/// <summary>
/// Returns or sets the text body of the E-mail
/// </summary>
public string BodyText { get; set; }
/// <summary>
/// Returns or sets the html body of the E-mail
/// </summary>
public string BodyHtml { get; set; }
/// <summary>
/// The E-mail <see cref="Attachments" />
/// </summary>
public Attachments Attachments
{
get { return _attachments ?? (_attachments = new Attachments()); }
}
/// <summary>
/// Returns or sets the UTC date and time the <see cref="Sender"/> has submitted the
/// <see cref="Message"/>
/// </summary>
public DateTime? SentOn { get; set; }
/// <summary>
/// Returns the UTC date and time when the <see cref="Message"/> was received by
/// </summary>
public DateTime? ReceivedOn { get; private set; }
/// <summary>
/// Returns or sets the transport message headers. These are only present when
/// the message has been sent outside an Exchange environment to another mailserver
/// <c>null</c> will be returned when not present
/// </summary>
public string TransportMessageHeaders { get; private set; }
#endregion
#region Constructor
/// <summary>
/// Creates this object and sets all the needed properties
/// </summary>
/// <param name="sender">The <see cref="Sender"/> of the E-mail</param>
/// <param name="subject">The subject of the E-mail</param>
public Email(Sender sender,
string subject)
{
Sender = sender;
Subject = subject;
Importance = MessageImportance.IMPORTANCE_NORMAL;
IconIndex = MessageIconIndex.NewMail;
}
/// <summary>
/// Creates this object and sets all the needed properties
/// </summary>
/// <param name="sender">The <see cref="Sender"/> of the E-mail</param>
/// <param name="representing">The <see cref="MsgKit.Representing"/> sender of the E-mail</param>
/// <param name="subject">The subject of the E-mail</param>
public Email(Sender sender,
Representing representing,
string subject)
{
Sender = sender;
Representing = representing;
Subject = subject;
Importance = MessageImportance.IMPORTANCE_NORMAL;
IconIndex = MessageIconIndex.NewMail;
}
#endregion
#region SetSubject
/// <summary>
/// These properties are computed by message store or transport providers from the PR_SUBJECT (PidTagSubject)
/// and PR_SUBJECT_PREFIX (PidTagSubjectPrefix) properties in the following manner. If the PR_SUBJECT_PREFIX
/// is present and is an initial substring of PR_SUBJECT, PR_NORMALIZED_SUBJECT and associated properties are
/// set to the contents of PR_SUBJECT with the prefix removed. If PR_SUBJECT_PREFIX is present, but it is not
/// an initial substring of PR_SUBJECT, PR_SUBJECT_PREFIX is deleted and recalculated from PR_SUBJECT using
/// the following rule: If the string contained in PR_SUBJECT begins with one to three non-numeric characters
/// followed by a colon and a space, then the string together with the colon and the blank becomes the prefix.
/// Numbers, blanks, and punctuation characters are not valid prefix characters. If PR_SUBJECT_PREFIX is not
/// present, it is calculated from PR_SUBJECT using the rule outlined in the previous step.This property then
/// is set to the contents of PR_SUBJECT with the prefix removed.
/// </summary>
/// <remarks>
/// When PR_SUBJECT_PREFIX is an empty string, PR_SUBJECT and PR_NORMALIZED_SUBJECT are the same. Ultimately,
/// this property should be the part of PR_SUBJECT following the prefix. If there is no prefix, this property
/// becomes the same as PR_SUBJECT. PR_SUBJECT_PREFIX and this property should be computed as part of the
/// IMAPIProp::SaveChanges implementation. A client application should not prompt the IMAPIProp::GetProps
/// method for their values until they have been committed by an IMAPIProp::SaveChanges call.
/// </remarks>
private void SetSubject()
{
if (!string.IsNullOrEmpty(SubjectPrefix))
{
if (Subject.Contains(SubjectPrefix))
SubjectNormalized = Subject.Replace(SubjectPrefix, string.Empty);
else
{
var prefix = Subject.Substring(5);
if (prefix.Contains(": ") && !prefix.Any(char.IsDigit))
{
SubjectPrefix = prefix;
SubjectNormalized = Subject.Replace(prefix, string.Empty);
}
}
}
else if (!string.IsNullOrEmpty(Subject))
{
var prefix = Subject.Substring(5);
if (prefix.Contains(": ") && !prefix.Any(char.IsDigit))
{
SubjectPrefix = prefix;
SubjectNormalized = Subject.Replace(prefix, string.Empty);
}
else
SubjectNormalized = Subject;
}
else
SubjectNormalized = Subject;
if (SubjectPrefix == null) SubjectPrefix = string.Empty;
}
#endregion
#region WriteToStorage
/// <summary>
/// Writes all the properties that are part of the <see cref="Email"/> object either as <see cref="CFStorage"/>'s
/// or <see cref="CFStream"/>'s to the <see cref="CompoundFile.RootStorage"/>
/// </summary>
private void WriteToStorage()
{
var rootStorage = CompoundFile.RootStorage;
long messageSize = 0;
messageSize += Recipients.WriteToStorage(rootStorage);
messageSize += Attachments.WriteToStorage(rootStorage);
var recipientCount = Recipients.Count;
var attachmentCount = Attachments.Count;
var propertiesStream = new TopLevelProperties(recipientCount,
attachmentCount,
recipientCount,
attachmentCount);
propertiesStream.AddProperty(PropertyTags.PR_ENTRYID, Mapi.GenerateEntryId());
propertiesStream.AddProperty(PropertyTags.PR_INSTANCE_KEY, Mapi.GenerateInstanceKey());
propertiesStream.AddProperty(PropertyTags.PR_STORE_SUPPORT_MASK, StoreSupportMaskConst.StoreSupportMask, PropertyFlags.PROPATTR_READABLE);
propertiesStream.AddProperty(PropertyTags.PR_STORE_UNICODE_MASK, StoreSupportMaskConst.StoreSupportMask, PropertyFlags.PROPATTR_READABLE);
propertiesStream.AddProperty(PropertyTags.PR_ALTERNATE_RECIPIENT_ALLOWED, true, PropertyFlags.PROPATTR_READABLE);
propertiesStream.AddProperty(PropertyTags.PR_HASATTACH, attachmentCount > 0);
var messageFlags = MessageFlags.MSGFLAG_UNMODIFIED;
if (attachmentCount > 0)
messageFlags |= MessageFlags.MSGFLAG_HASATTACH;
if (!SentOn.HasValue)
SentOn = DateTime.UtcNow;
propertiesStream.AddProperty(PropertyTags.PR_CLIENT_SUBMIT_TIME, SentOn);
propertiesStream.AddProperty(PropertyTags.PR_MESSAGE_FLAGS, messageFlags);
SetSubject();
propertiesStream.AddProperty(PropertyTags.PR_SUBJECT_W, Subject);
propertiesStream.AddProperty(PropertyTags.PR_NORMALIZED_SUBJECT_W, SubjectNormalized);
propertiesStream.AddProperty(PropertyTags.PR_SUBJECT_PREFIX_W, SubjectPrefix);
propertiesStream.AddProperty(PropertyTags.PR_CONVERSATION_TOPIC_W, SubjectNormalized);
// http://www.meridiandiscovery.com/how-to/e-mail-conversation-index-metadata-computer-forensics/
// http://stackoverflow.com/questions/11860540/does-outlook-embed-a-messageid-or-equivalent-in-its-email-elements
//propertiesStream.AddProperty(PropertyTags.PR_CONVERSATION_INDEX, Subject);
// TODO: Change modification time when this message is opened and only modified
var utcNow = DateTime.UtcNow;
propertiesStream.AddProperty(PropertyTags.PR_CREATION_TIME, utcNow);
propertiesStream.AddProperty(PropertyTags.PR_LAST_MODIFICATION_TIME, utcNow);
propertiesStream.AddProperty(PropertyTags.PR_MESSAGE_CLASS_W, "IPM.Note");
propertiesStream.AddProperty(PropertyTags.PR_PRIORITY, Priority);
propertiesStream.AddProperty(PropertyTags.PR_IMPORTANCE, Importance);
propertiesStream.AddProperty(PropertyTags.PR_MESSAGE_LOCALE_ID, CultureInfo.CurrentCulture.LCID);
propertiesStream.AddProperty(PropertyTags.PR_ICON_INDEX, IconIndex);
if (Sender != null) Sender.WriteProperties(propertiesStream);
if (Receiving != null) Receiving.WriteProperties(propertiesStream);
if (Representing != null) Representing.WriteProperties(propertiesStream);
if (ReceivingRepresenting != null) ReceivingRepresenting.WriteProperties(propertiesStream);
if (recipientCount > 0)
{
var displayTo = new List<string>();
var displayCc = new List<string>();
var displayBcc = new List<string>();
foreach (var recipient in Recipients)
{
switch (recipient.RecipientType)
{
case RecipientType.To:
if (!string.IsNullOrWhiteSpace(recipient.DisplayName))
displayTo.Add(recipient.DisplayName);
else if (!string.IsNullOrWhiteSpace(recipient.Email))
displayTo.Add(recipient.Email);
break;
case RecipientType.Cc:
if (!string.IsNullOrWhiteSpace(recipient.DisplayName))
displayCc.Add(recipient.DisplayName);
else if (!string.IsNullOrWhiteSpace(recipient.Email))
displayCc.Add(recipient.Email);
break;
case RecipientType.Bcc:
if (!string.IsNullOrWhiteSpace(recipient.DisplayName))
displayBcc.Add(recipient.DisplayName);
else if (!string.IsNullOrWhiteSpace(recipient.Email))
displayBcc.Add(recipient.Email);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
propertiesStream.AddProperty(PropertyTags.PR_DISPLAY_TO_W, string.Join(";", displayTo), PropertyFlags.PROPATTR_READABLE);
propertiesStream.AddProperty(PropertyTags.PR_DISPLAY_CC_W, string.Join(";", displayCc), PropertyFlags.PROPATTR_READABLE);
propertiesStream.AddProperty(PropertyTags.PR_DISPLAY_BCC_W, string.Join(";", displayBcc), PropertyFlags.PROPATTR_READABLE);
}
propertiesStream.AddProperty(PropertyTags.PR_INTERNET_CPID, Encoding.UTF8.CodePage);
propertiesStream.AddProperty(PropertyTags.PR_BODY_W, BodyText);
if (!string.IsNullOrEmpty(BodyHtml))
propertiesStream.AddProperty(PropertyTags.PR_HTML, BodyHtml);
propertiesStream.WriteProperties(rootStorage, messageSize);
}
#endregion
#region Save
/// <summary>
/// Saves the message to the given <paramref name="stream" />
/// </summary>
/// <param name="stream"></param>
public new void Save(Stream stream)
{
WriteToStorage();
base.Save(stream);
}
/// <summary>
/// Saves the message to the given <paramref name="fileName" />
/// </summary>
/// <param name="fileName"></param>
public new void Save(string fileName)
{
WriteToStorage();
base.Save(fileName);
}
#endregion
#region Dispose
/// <summary>
/// Disposes all the attachment streams
/// </summary>
public new void Dispose()
{
foreach (var attachment in _attachments)
attachment.Stream.Dispose();
base.Dispose();
}
#endregion
}
}