-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathEmailExporter.cs
199 lines (178 loc) · 5.89 KB
/
EmailExporter.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using TypeAgent.Core;
namespace TypeAgent;
public class EmailExporter
{
const int MaxFileNameLength = 64;
Outlook _outlook;
public EmailExporter(Outlook outlook)
{
ArgumentNullException.ThrowIfNull(outlook);
_outlook = outlook;
}
public void Export(string sourcePath, string destPath)
{
ArgumentException.ThrowIfNullOrEmpty(sourcePath, nameof(sourcePath));
if (PathEx.IsDirectory(sourcePath))
{
ExportDirectory(sourcePath, destPath);
}
else
{
ExportFile(sourcePath, destPath);
}
}
public void ExportFile(string sourcePath, string? destPath)
{
Verify.FileExists(sourcePath);
if (string.IsNullOrEmpty(destPath))
{
string destFolderPath = EnsureDestJsonFolder(Path.GetDirectoryName(sourcePath));
destPath = DestFilePath(sourcePath, destFolderPath);
}
try
{
Email email = _outlook.LoadEmail(sourcePath);
email.Save(destPath);
}
catch (System.Exception ex)
{
ConsoleEx.WriteLineColor(ConsoleColor.Red, $"SKIPPED {sourcePath}");
ConsoleEx.LogError(ex);
}
}
public void ExportDirectory(string sourcePath, string destPath)
{
Verify.DirectoryExists(sourcePath);
if (string.IsNullOrEmpty(destPath))
{
destPath = EnsureDestJsonFolder(sourcePath);
}
int count = 0;
foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath))
{
++count;
Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}");
ExportFile(sourceFilePath, DestFilePath(sourceFilePath, destPath));
}
}
public void ExportAll(string rootPath, int maxMessages, bool bucketBySize = true, bool convert = false)
{
if (maxMessages <= 0)
{
maxMessages = int.MaxValue;
}
DirectoryEx.Ensure(rootPath);
int counter = 0;
foreach (MailItem item in _outlook.MapMailItems<MailItem>((item) => item))
{
++counter;
try
{
bool isForward = item.IsForward();
if (item.IsForward())
{
// Todo: need to parse Forwards
continue;
}
Console.WriteLine($"#{counter}");
Console.WriteLine(item.Subject);
string destDirPath = bucketBySize ? GetDestDir(rootPath, item.BodyLatest().Length) : rootPath;
string fileName = FileEx.SanitizeFileName(item.Subject, MaxFileNameLength);
string msgFilePath = FileEx.MakeUnique(destDirPath, fileName, ".msg");
item.SaveAs(msgFilePath);
if (convert)
{
Email email = new Email(item, msgFilePath);
string jsonDirPath = Path.Join(destDirPath, "json");
DirectoryEx.Ensure(jsonDirPath);
string jsonFilePath = FileEx.MakeUnique(jsonDirPath, fileName, ".json");
email.Save(jsonFilePath);
}
}
catch(System.Exception ex)
{
ConsoleEx.LogError(ex);
}
if (counter >= maxMessages)
{
break;
}
}
}
public void ExportAllEmailBySizeJson(string rootPath)
{
int counter = 0;
foreach(MailItem item in _outlook.ForEachMailItem())
{
++counter;
try
{
bool isForward = item.IsForward();
if (item.IsForward())
{
continue;
}
Email email = new Email(item);
Console.WriteLine($"#{counter}, {email.Body.Length} chars");
Console.WriteLine(email.Subject);
int size = email.Body.Length;
string destDirPath = GetDestDir(rootPath, size);
string fileName = FileEx.SanitizeFileName(email.Subject, MaxFileNameLength);
email.Save(FileEx.MakeUnique(destDirPath, fileName, ".json"));
}
catch (System.Exception ex)
{
ConsoleEx.LogError(ex);
}
}
}
public void ExportFrom(string senderName)
{
List<Email> emails = _outlook.LoadFrom(new EmailSender(senderName));
foreach (var email in emails)
{
Console.WriteLine(email.ToString());
}
}
string DestFilePath(string sourceFilePath, string destFolderPath)
{
return Path.Join(destFolderPath, Path.GetFileNameWithoutExtension(sourceFilePath) + ".json");
}
public void PrintEmail(string sourcePath)
{
if (PathEx.IsDirectory(sourcePath))
{
int count = 0;
foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath))
{
++count;
Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}");
PrintFile(sourceFilePath);
}
}
else
{
PrintFile(sourcePath);
}
}
void PrintFile(string sourcePath)
{
Email email = _outlook.LoadEmail(sourcePath);
Console.WriteLine(email.ToString());
}
string EnsureDestJsonFolder(string dirPath)
{
string destFolderPath = Path.Join(dirPath, "json");
DirectoryEx.Ensure(destFolderPath);
return destFolderPath;
}
string GetDestDir(string rootPath, int size)
{
int bucket = MailStats.GetBucketForSize(size);
string destDirPath = Path.Join(rootPath, bucket.ToString());
DirectoryEx.Ensure(destDirPath);
return destDirPath;
}
}