microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f46fff4e5103217703b51e27ba3f6405ac000e21

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/email/Email.cs

281lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using TypeAgent.Core;
5namespace TypeAgent;
6
7public class Email
8{
9 public Email()
10 {
11 }
12
13 public Email(MailItem mail, string? sourcePath = null)
14 {
15 Load(mail);
16 SourcePath = sourcePath;
17 }
18
19 public Email(Email email)
20 {
21 this.Bcc = email.Bcc;
22 this.Cc = email.Cc;
23 this.From = email.From;
24 this.Importance = email.Importance;
25 this.ReceivedOn = email.ReceivedOn;
26 this.SentOn = email.SentOn;
27 this.SourcePath = email.SourcePath;
28 this.Subject = email.Subject;
29 this.Body = email.Body;
30 }
31
32 /*
33 * HEADERS
34 */
35 [JsonPropertyName("from")]
36 public EmailAddress From { get; set; }
37 [JsonPropertyName("to")]
38 public List<EmailAddress> To { get; set; }
39 [JsonPropertyName("cc")]
40 public List<EmailAddress> Cc { get; set; }
41 [JsonPropertyName("bcc")]
42 public List<EmailAddress> Bcc { get; set; }
43 [JsonPropertyName("subject")]
44 public string Subject { get; set; }
45 [JsonPropertyName("sentOn")]
46 public DateTime SentOn { get; set; }
47 [JsonPropertyName("receivedOn")]
48 public DateTime? ReceivedOn { get; set; }
49 [JsonPropertyName("importance")]
50 public string Importance { get; set; }
51 [JsonPropertyName("threadId")]
52 public string ThreadId { get; set; }
53
54 [JsonPropertyName("sourcePath")]
55 public string SourcePath { get; set; }
56
57 /*
58 * BODY
59 */
60 [JsonPropertyName("body")]
61 public string Body { get; set; }
62
63 public string ToJson()
64 {
65 return Json.Stringify(this);
66 }
67
68 public void Save(string filePath)
69 {
70 File.WriteAllText(filePath, ToJson());
71 }
72
73 public static Email Load(string filePath)
74 {
75 string json = File.ReadAllText(filePath);
76 return Json.Parse<Email>(json);
77 }
78
79 public override string ToString()
80 {
81 return ToString(true);
82 }
83
84 public string ToString(bool includeBody)
85 {
86 StringBuilder sb = new StringBuilder();
87 if (From != null)
88 {
89 sb.AppendHeader("From", From.ToString());
90 }
91 if (To != null)
92 {
93 sb.AppendHeader("To", To.Join());
94 }
95 if (Cc != null)
96 {
97 sb.AppendHeader("Cc", Cc.Join());
98 }
99 if (Bcc != null)
100 {
101 sb.AppendHeader("Bcc", Bcc.Join());
102 }
103 sb.AppendHeader("Subject", Subject);
104 sb.AppendHeader("Sent", SentOn.ToString());
105 sb.AppendHeader("Received", ReceivedOn.ToString());
106 sb.AppendHeader("Importance", Importance);
107 sb.AppendHeader("SourcePath", SourcePath);
108 if (includeBody && !string.IsNullOrEmpty(Body))
109 {
110 sb.AppendLine();
111 sb.AppendLine(Body);
112 }
113 return sb.ToString();
114 }
115
116 public static Email FromText(string emailText)
117 {
118 Email email = new Email();
119 foreach (var part in MailParser.Default.ParseParts(emailText))
120 {
121 switch (part.Key.ToLower())
122 {
123 default:
124 break;
125 case "from":
126 email.From = EmailAddress.FromString(part.Value);
127 break;
128 case "to":
129 email.To = EmailAddress.ListFromString(part.Value);
130 break;
131 case "cc":
132 email.Cc = EmailAddress.ListFromString(part.Value);
133 break;
134 case "bcc":
135 email.Bcc = EmailAddress.ListFromString(part.Value);
136 break;
137 case "subject":
138 email.Subject = part.Value;
139 break;
140 case "sent":
141 email.SentOn = DateTime.Parse(part.Value);
142 break;
143 case "received":
144 email.ReceivedOn = DateTime.Parse(part.Value);
145 break;
146 case "body":
147 email.Body = part.Value;
148 break;
149 case "importance":
150 email.Importance = part.Value;
151 break;
152 case "threadid":
153 email.ThreadId = part.Value;
154 break;
155 }
156 }
157 return email;
158 }
159
160 void Load(MailItem item)
161 {
162 LoadRecipients(item);
163 From = item.Sender != null ? new EmailAddress(SmtpAddressOf(item.Sender), item.Sender.Name) : null;
164 Subject = item.Subject;
165 SentOn = item.SentOn;
166 ReceivedOn = item.ReceivedTime;
167 Importance = GetImportance(item);
168 ThreadId = item.ConversationID;
169 LoadBody(item);
170 }
171
172 void LoadRecipients(MailItem mail)
173 {
174 Recipients recipients = mail.Recipients;
175 try
176 {
177 foreach (Recipient recipient in recipients)
178 {
179 try
180 {
181 LoadRecipient(recipient);
182 }
183 finally
184 {
185 COMObject.Release(recipient);
186 }
187 }
188
189 }
190 finally
191 {
192 COMObject.Release(recipients);
193 recipients = null;
194 }
195 }
196
197 bool LoadRecipient(Recipient recipient)
198 {
199 if (!recipient.Resolve())
200 {
201 return false;
202 }
203 EmailAddress emailAddress = new EmailAddress(SmtpAddressOf(recipient) ?? string.Empty, recipient.Name);
204 switch (recipient.Type)
205 {
206 default:
207 break;
208 case (int)OlMailRecipientType.olTo:
209 To ??= new List<EmailAddress>();
210 To.Add(emailAddress);
211 break;
212 case (int)OlMailRecipientType.olCC:
213 Cc ??= new List<EmailAddress>();
214 Cc.Add(emailAddress);
215 break;
216 case (int)OlMailRecipientType.olBCC:
217 Bcc ??= new List<EmailAddress>();
218 Bcc.Add(emailAddress);
219 break;
220 }
221 return true;
222 }
223
224 void LoadBody(MailItem item)
225 {
226 string body = item.Body;
227 body = BodyParser.Default.GetLatest(body);
228 Body = body;
229 }
230
231 string GetImportance(MailItem item)
232 {
233 switch (item.Importance)
234 {
235 default:
236 break;
237 case OlImportance.olImportanceNormal:
238 return "Normal";
239 case OlImportance.olImportanceHigh:
240 return "High";
241 case OlImportance.olImportanceLow:
242 return "Low";
243 }
244 return null;
245 }
246
247 string SmtpAddressOf(Recipient recipient)
248 {
249 AddressEntry addrEntry = recipient.AddressEntry;
250 return addrEntry != null ? SmtpAddressOf(addrEntry) : null;
251 }
252
253
254 string SmtpAddressOf(AddressEntry addrEntry)
255 {
256 try
257 {
258 return addrEntry.AddressEntryUserType != OlAddressEntryUserType.olSmtpAddressEntry
259 ? SmtpAddressOfExchangeUser(addrEntry)
260 : addrEntry.Address;
261 }
262 finally
263 {
264 COMObject.Release(addrEntry);
265 }
266 }
267
268 string SmtpAddressOfExchangeUser(AddressEntry addrEntry)
269 {
270 ExchangeUser exchUser = addrEntry.GetExchangeUser();
271 try
272 {
273 return exchUser?.PrimarySmtpAddress;
274 }
275 finally
276 {
277 COMObject.Release(exchUser);
278 exchUser = null;
279 }
280 }
281}