microsoft/TypeAgent

Public

mirrored from https://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0657252fbeb2eb0e4613333df6d0535cbf09cfdc

Branches

Tags

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

Clone

HTTPS

Download ZIP

dotnet/email/Email.cs

224lines · 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 /*
20 * HEADERS
21 */
22 [JsonPropertyName("from")]
23 public EmailAddress From { get; set; }
24 [JsonPropertyName("to")]
25 public List<EmailAddress> To { get; set; }
26 [JsonPropertyName("cc")]
27 public List<EmailAddress> Cc { get; set; }
28 [JsonPropertyName("bcc")]
29 public List<EmailAddress> Bcc { get; set; }
30 [JsonPropertyName("subject")]
31 public string Subject { get; set; }
32 [JsonPropertyName("sentOn")]
33 public DateTime SentOn { get; set; }
34 [JsonPropertyName("receivedOn")]
35 public DateTime ReceivedOn { get; set; }
36 [JsonPropertyName("importance")]
37 public string Importance { get; set; }
38 [JsonPropertyName("threadId")]
39 public string ThreadId { get; set; }
40
41 [JsonPropertyName("sourcePath")]
42 public string SourcePath { get; set; }
43
44 /*
45 * BODY
46 */
47 [JsonPropertyName("body")]
48 public string Body { get; set; }
49
50 public string ToJson()
51 {
52 return Json.Stringify(this);
53 }
54
55 public void Save(string filePath)
56 {
57 File.WriteAllText(filePath, ToJson());
58 }
59
60 public static Email Load(string filePath)
61 {
62 string json = File.ReadAllText(filePath);
63 return Json.Parse<Email>(json);
64 }
65
66 public override string ToString()
67 {
68 return ToString(true);
69 }
70
71 public string ToString(bool includeBody)
72 {
73 StringBuilder sb = new StringBuilder();
74 if (From != null)
75 {
76 sb.AppendHeader("From", From.ToString());
77 }
78 if (To != null)
79 {
80 sb.AppendHeader("To", To.Join());
81 }
82 if (Cc != null)
83 {
84 sb.AppendHeader("Cc", Cc.Join());
85 }
86 if (Bcc != null)
87 {
88 sb.AppendHeader("Bcc", Bcc.Join());
89 }
90 sb.AppendHeader("Subject", Subject);
91 sb.AppendHeader("Sent", SentOn.ToString());
92 sb.AppendHeader("Received", ReceivedOn.ToString());
93 sb.AppendHeader("Importance", Importance);
94 sb.AppendHeader("SourcePath", SourcePath);
95 if (includeBody && !string.IsNullOrEmpty(Body))
96 {
97 sb.AppendLine();
98 sb.AppendLine(Body);
99 }
100 return sb.ToString();
101 }
102
103 void Load(MailItem item)
104 {
105 LoadRecipients(item);
106 From = item.Sender != null ? new EmailAddress(SmtpAddressOf(item.Sender), item.Sender.Name) : null;
107 Subject = item.Subject;
108 SentOn = item.SentOn;
109 ReceivedOn = item.ReceivedTime;
110 Importance = GetImportance(item);
111 ThreadId = item.ConversationID;
112 LoadBody(item);
113 }
114
115 void LoadRecipients(MailItem mail)
116 {
117 Recipients recipients = mail.Recipients;
118 try
119 {
120 foreach (Recipient recipient in recipients)
121 {
122 try
123 {
124 LoadRecipient(recipient);
125 }
126 finally
127 {
128 COMObject.Release(recipient);
129 }
130 }
131
132 }
133 finally
134 {
135 COMObject.Release(recipients);
136 recipients = null;
137 }
138 }
139
140 bool LoadRecipient(Recipient recipient)
141 {
142 if (!recipient.Resolve())
143 {
144 return false;
145 }
146 EmailAddress emailAddress = new EmailAddress(SmtpAddressOf(recipient) ?? string.Empty, recipient.Name);
147 switch (recipient.Type)
148 {
149 default:
150 break;
151 case (int)OlMailRecipientType.olTo:
152 To ??= new List<EmailAddress>();
153 To.Add(emailAddress);
154 break;
155 case (int)OlMailRecipientType.olCC:
156 Cc ??= new List<EmailAddress>();
157 Cc.Add(emailAddress);
158 break;
159 case (int)OlMailRecipientType.olBCC:
160 Bcc ??= new List<EmailAddress>();
161 Bcc.Add(emailAddress);
162 break;
163 }
164 return true;
165 }
166
167 void LoadBody(MailItem item)
168 {
169 string body = item.Body;
170 body = BodyParser.Default.GetLatest(body);
171 Body = body;
172 }
173
174 string GetImportance(MailItem item)
175 {
176 switch (item.Importance)
177 {
178 default:
179 break;
180 case OlImportance.olImportanceNormal:
181 return "Normal";
182 case OlImportance.olImportanceHigh:
183 return "High";
184 case OlImportance.olImportanceLow:
185 return "Low";
186 }
187 return null;
188 }
189
190 string SmtpAddressOf(Recipient recipient)
191 {
192 AddressEntry addrEntry = recipient.AddressEntry;
193 return addrEntry != null ? SmtpAddressOf(addrEntry) : null;
194 }
195
196
197 string SmtpAddressOf(AddressEntry addrEntry)
198 {
199 try
200 {
201 return addrEntry.AddressEntryUserType != OlAddressEntryUserType.olSmtpAddressEntry
202 ? SmtpAddressOfExchangeUser(addrEntry)
203 : addrEntry.Address;
204 }
205 finally
206 {
207 COMObject.Release(addrEntry);
208 }
209 }
210
211 string SmtpAddressOfExchangeUser(AddressEntry addrEntry)
212 {
213 ExchangeUser exchUser = addrEntry.GetExchangeUser();
214 try
215 {
216 return exchUser?.PrimarySmtpAddress;
217 }
218 finally
219 {
220 COMObject.Release(exchUser);
221 exchUser = null;
222 }
223 }
224}
225