microsoft/TypeAgent
Publicmirrored from https://github.com/microsoft/TypeAgentAvailable
dotnet/email/EmailExporter.cs
187lines · 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 | Outlook _outlook; |
| 11 | public EmailExporter(Outlook outlook) |
| 12 | { |
| 13 | ArgumentNullException.ThrowIfNull(outlook); |
| 14 | _outlook = outlook; |
| 15 | } |
| 16 | |
| 17 | public void Export(string sourcePath, string destPath) |
| 18 | { |
| 19 | ArgumentException.ThrowIfNullOrEmpty(sourcePath, nameof(sourcePath)); |
| 20 | |
| 21 | if (PathEx.IsDirectory(sourcePath)) |
| 22 | { |
| 23 | ExportDirectory(sourcePath, destPath); |
| 24 | } |
| 25 | else |
| 26 | { |
| 27 | ExportFile(sourcePath, destPath); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | public void ExportFile(string sourcePath, string? destPath) |
| 32 | { |
| 33 | Verify.FileExists(sourcePath); |
| 34 | if (string.IsNullOrEmpty(destPath)) |
| 35 | { |
| 36 | string destFolderPath = EnsureDestJsonFolder(Path.GetDirectoryName(sourcePath)); |
| 37 | destPath = DestFilePath(sourcePath, destFolderPath); |
| 38 | } |
| 39 | |
| 40 | try |
| 41 | { |
| 42 | Email email = _outlook.LoadEmail(sourcePath); |
| 43 | email.Save(destPath); |
| 44 | } |
| 45 | catch(System.Exception ex) |
| 46 | { |
| 47 | WriteLineColor(ConsoleColor.Red, $"SKIPPED {sourcePath}"); |
| 48 | LogError(ex); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public void ExportDirectory(string sourcePath, string destPath) |
| 53 | { |
| 54 | Verify.DirectoryExists(sourcePath); |
| 55 | if (string.IsNullOrEmpty(destPath)) |
| 56 | { |
| 57 | destPath = EnsureDestJsonFolder(sourcePath); |
| 58 | } |
| 59 | int count = 0; |
| 60 | foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath)) |
| 61 | { |
| 62 | ++count; |
| 63 | Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}"); |
| 64 | ExportFile(sourceFilePath, DestFilePath(sourceFilePath, destPath)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public void ExportFrom(string senderName) |
| 69 | { |
| 70 | List<Email> emails = _outlook.LoadFrom(senderName); |
| 71 | foreach(var email in emails) |
| 72 | { |
| 73 | Console.WriteLine(email.ToString()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | string DestFilePath(string sourceFilePath, string destFolderPath) |
| 78 | { |
| 79 | return Path.Join(destFolderPath, Path.GetFileNameWithoutExtension(sourceFilePath) + ".json"); |
| 80 | } |
| 81 | |
| 82 | public void PrintEmail(string sourcePath) |
| 83 | { |
| 84 | if (PathEx.IsDirectory(sourcePath)) |
| 85 | { |
| 86 | int count = 0; |
| 87 | foreach (string sourceFilePath in Directory.EnumerateFiles(sourcePath)) |
| 88 | { |
| 89 | ++count; |
| 90 | Console.WriteLine($"{count}: {Path.GetFileName(sourceFilePath)}"); |
| 91 | PrintFile(sourceFilePath); |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | PrintFile(sourcePath); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void PrintFile(string sourcePath) |
| 101 | { |
| 102 | Email email = _outlook.LoadEmail(sourcePath); |
| 103 | Console.WriteLine(email.ToString()); |
| 104 | } |
| 105 | |
| 106 | string EnsureDestJsonFolder(string dirPath) |
| 107 | { |
| 108 | string destFolderPath = Path.Join(dirPath, "json"); |
| 109 | DirectoryEx.Ensure(destFolderPath); |
| 110 | return destFolderPath; |
| 111 | } |
| 112 | |
| 113 | static void Main(string[] args) |
| 114 | { |
| 115 | args = EnsureArgs(args); |
| 116 | if (args == null || args.Length == 0) |
| 117 | { |
| 118 | return; |
| 119 | } |
| 120 | try |
| 121 | { |
| 122 | using Outlook outlook = new Outlook(); |
| 123 | var exporter = new EmailExporter(outlook); |
| 124 | switch(args[0]) |
| 125 | { |
| 126 | default: |
| 127 | exporter.Export(args.ElementAtOrDefault(0), args.ElementAtOrDefault(1)); |
| 128 | break; |
| 129 | |
| 130 | case "--sender": |
| 131 | exporter.ExportFrom(args.GetArg(1)); |
| 132 | break; |
| 133 | |
| 134 | case "--print": |
| 135 | exporter.PrintEmail(args.GetArg(1)); |
| 136 | Console.ReadLine(); |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | catch(System.Exception ex) |
| 141 | { |
| 142 | LogError(ex); |
| 143 | } |
| 144 | finally |
| 145 | { |
| 146 | COMObject.ReleaseAll(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static string[]? EnsureArgs(string[] args) |
| 151 | { |
| 152 | if (args != null && args.Length > 0) |
| 153 | { |
| 154 | return args; |
| 155 | } |
| 156 | return GetInput(); |
| 157 | } |
| 158 | |
| 159 | static string[] GetInput() |
| 160 | { |
| 161 | Console.Write(">"); |
| 162 | string line = Console.ReadLine(); |
| 163 | if (line != null) |
| 164 | { |
| 165 | line = line.Trim(); |
| 166 | } |
| 167 | if (string.IsNullOrEmpty(line)) |
| 168 | { |
| 169 | return null; |
| 170 | } |
| 171 | return line.ParseCommandLine(); |
| 172 | } |
| 173 | |
| 174 | static void LogError(System.Exception ex) |
| 175 | { |
| 176 | WriteLineColor(ConsoleColor.DarkYellow, $"##Error##\n{ex.Message}\n####"); |
| 177 | Console.WriteLine(); |
| 178 | } |
| 179 | |
| 180 | static void WriteLineColor(ConsoleColor color, string message) |
| 181 | { |
| 182 | var prevColor = Console.ForegroundColor; |
| 183 | Console.ForegroundColor = color; |
| 184 | Console.WriteLine(message); |
| 185 | Console.ForegroundColor = prevColor; |
| 186 | } |
| 187 | } |
| 188 | |