microsoft/TypeAgent

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-shell-and-cli-windows-job

Branches

Tags

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

Clone

HTTPS

Download ZIP

dotnet/email/EmailExporter.cs

222lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using TypeAgent.Core;
5
6namespace TypeAgent;
7
8public class EmailExporter
9{
10 public enum ExportFormat
11 {
12 Json,
13 Txt
14 }
15
16 const int MaxFileNameLength = 64;
17
18 Outlook _outlook;
19 public EmailExporter(Outlook outlook)
20 {
21 ArgumentNullException.ThrowIfNull(outlook);
22 _outlook = outlook;
23 }
24
25 public void Export(string sourcePath, string destPath)
26 {
27 ArgumentException.ThrowIfNullOrEmpty(sourcePath, nameof(sourcePath));
28
29 if (PathEx.IsDirectory(sourcePath))
30 {
31 ExportDirectory(sourcePath, destPath);
32 }
33 else
34 {
35 ExportFile(sourcePath, destPath);
36 }
37 }
38
39 public void ExportFile(string sourcePath, string? destPath, ExportFormat format = ExportFormat.Json)
40 {
41 Verify.FileExists(sourcePath);
42 if (string.IsNullOrEmpty(destPath))
43 {
44 string destFolderPath = EnsureDestJsonFolder(Path.GetDirectoryName(sourcePath));
45 destPath = DestFilePath(sourcePath, destFolderPath);
46 }
47
48 try
49 {
50 if (format == ExportFormat.Json)
51 {
52 List<Email> emails = _outlook.LoadEmail(sourcePath);
53 for (int i = 0; i < emails.Count; ++i)
54 {
55 var email = emails[i];
56 string savePath = (i > 0) ? PathEx.AppendNumber(destPath, i) : destPath;
57 email.Save(savePath);
58 }
59 }
60 else
61 {
62 string txtFilePath = PathEx.ReplaceFileNameExtension(destPath, ".txt");
63 _outlook.SaveEmailAsText(sourcePath, txtFilePath);
64 }
65 }
66 catch (System.Exception ex)
67 {
68 ConsoleEx.WriteLineColor(ConsoleColor.Red, $"SKIPPED {sourcePath}");
69 ConsoleEx.LogError(ex);
70 }
71 }
72
73 public void ExportDirectory(string sourcePath, string destPath)
74 {
75 Verify.DirectoryExists(sourcePath);
76 if (string.IsNullOrEmpty(destPath))
77 {
78 destPath = EnsureDestJsonFolder(sourcePath);
79 }
80 int count = 0;
81 foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath))
82 {
83 ++count;
84 Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}");
85 ExportFile(sourceFilePath, DestFilePath(sourceFilePath, destPath), ExportFormat.Json);
86 }
87 }
88
89 public void ExportAll(string rootPath, int maxMessages, bool bucketBySize = true, bool convert = false)
90 {
91 if (maxMessages <= 0)
92 {
93 maxMessages = int.MaxValue;
94 }
95 DirectoryEx.Ensure(rootPath);
96 int counter = 0;
97 foreach (MailItem item in _outlook.MapMailItems<MailItem>((item) => item))
98 {
99 ++counter;
100 try
101 {
102 bool isForward = item.IsForward();
103 if (isForward)
104 {
105 // Todo: need to parse Forwards
106 continue;
107 }
108 Console.WriteLine($"#{counter}");
109 Console.WriteLine(item.Subject);
110
111 string destDirPath = bucketBySize ? GetDestDir(rootPath, item.BodyLatest().Length) : rootPath;
112 string fileName = FileEx.SanitizeFileName(item.Subject, MaxFileNameLength);
113 string msgFilePath = FileEx.MakeUnique(destDirPath, fileName, ".msg");
114 item.SaveAs(msgFilePath);
115 if (convert)
116 {
117 Email email = new Email(item, msgFilePath);
118 string jsonDirPath = Path.Join(destDirPath, "json");
119 DirectoryEx.Ensure(jsonDirPath);
120 string jsonFilePath = FileEx.MakeUnique(jsonDirPath, fileName, ".json");
121 email.Save(jsonFilePath);
122 }
123 }
124 catch (System.Exception ex)
125 {
126 ConsoleEx.LogError(ex);
127 }
128 if (counter >= maxMessages)
129 {
130 break;
131 }
132 }
133 }
134
135 public void ExportAllEmailBySizeJson(string rootPath)
136 {
137 int counter = 0;
138 foreach (MailItem item in _outlook.ForEachMailItem())
139 {
140 ++counter;
141 try
142 {
143 bool isForward = item.IsForward();
144 if (isForward)
145 {
146 continue;
147 }
148 Email email = new Email(item);
149 Console.WriteLine($"#{counter}, {email.Body.Length} chars");
150 Console.WriteLine(email.Subject);
151
152 int size = email.Body.Length;
153 string destDirPath = GetDestDir(rootPath, size);
154
155 string fileName = FileEx.SanitizeFileName(email.Subject, MaxFileNameLength);
156 email.Save(FileEx.MakeUnique(destDirPath, fileName, ".json"));
157
158 }
159 catch (System.Exception ex)
160 {
161 ConsoleEx.LogError(ex);
162 }
163 }
164 }
165
166 public void ExportFrom(string senderName)
167 {
168 List<Email> emails = _outlook.LoadFrom(new EmailSender(senderName));
169 foreach (var email in emails)
170 {
171 Console.WriteLine(email.ToString());
172 }
173 }
174
175 string DestFilePath(string sourceFilePath, string destFolderPath)
176 {
177 return Path.Join(destFolderPath, Path.GetFileNameWithoutExtension(sourceFilePath) + ".json");
178 }
179
180 public void PrintEmail(string sourcePath)
181 {
182 if (PathEx.IsDirectory(sourcePath))
183 {
184 int count = 0;
185 foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath))
186 {
187 ++count;
188 Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}");
189 PrintFile(sourceFilePath);
190 }
191 }
192 else
193 {
194 PrintFile(sourcePath);
195 }
196 }
197
198 void PrintFile(string sourcePath)
199 {
200 List<Email> emails = _outlook.LoadEmail(sourcePath);
201 foreach (var email in emails)
202 {
203 Console.WriteLine(email.ToString());
204 Console.WriteLine();
205 }
206 }
207
208 string EnsureDestJsonFolder(string dirPath)
209 {
210 string destFolderPath = Path.Join(dirPath, "json");
211 DirectoryEx.Ensure(destFolderPath);
212 return destFolderPath;
213 }
214
215 string GetDestDir(string rootPath, int size)
216 {
217 int bucket = MailStats.GetBucketForSize(size);
218 string destDirPath = Path.Join(rootPath, bucket.ToString());
219 DirectoryEx.Ensure(destDirPath);
220 return destDirPath;
221 }
222}
223