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