-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
232 lines (210 loc) · 9.23 KB
/
Program.cs
File metadata and controls
232 lines (210 loc) · 9.23 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Linq;
using System.IO;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAttachmentExtractor
{
class Program
{
// Path where attachments will be saved
static string basePath = @"c:\temp\emails\";
static int totalfilesize = 0;
static void Main(string[] args)
{
EnumerateAccounts();
//EnumerateFoldersInDefaultStore();
//Console.WriteLine("Total file size:" + totalfilesize);
}
static void EnumerateFoldersInDefaultStore()
{
Outlook.Application Application = new Outlook.Application();
Outlook.Folder root = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
EnumerateFolders(root);
}
// Uses recursion to enumerate Outlook subfolders.
static void EnumerateFolders(Outlook.Folder folder)
{
Outlook.Folders childFolders = folder.Folders;
if (childFolders.Count > 0)
{
foreach (Outlook.Folder childFolder in childFolders)
{
// We only want Inbox folders - ignore Contacts and others
if (childFolder.FolderPath.Contains("Inbox"))
{
// Write the folder path.
Console.WriteLine(childFolder.FolderPath);
// Call EnumerateFolders using childFolder, to see if there are any sub-folders within this one
EnumerateFolders(childFolder);
}
}
}
Console.WriteLine("Checking in " + folder.FolderPath);
IterateMessages(folder);
}
static void IterateMessages(Outlook.Folder folder)
{
// attachment extensions to save
string[] extensionsArray = { ".pdf", ".doc", ".xls", ".ppt", ".vsd", ".zip", ".rar", ".txt", ".csv", ".proj" };
// Iterate through all items ("messages") in a folder
var fi = folder.Items;
if (fi != null)
{
try
{
foreach (Object item in fi)
{
Outlook.MailItem mi = (Outlook.MailItem)item;
var attachments = mi.Attachments;
if (attachments.Count != 0)
{
// Create a directory to store the attachment
if (!Directory.Exists(basePath + folder.FolderPath))
{
Directory.CreateDirectory(basePath + folder.FolderPath);
}
//Console.WriteLine(mi.Sender.Address);
//Console.WriteLine(mi.Subject + " [" + attachments.Count + "]");
//Console.WriteLine(generateFolder(folder.FolderPath, mi.Sender.Address));
for (int i = 1; i <= mi.Attachments.Count; i++)
{
var fn = mi.Attachments[i].FileName.ToLower();
//check wither any of the strings in the extensionsArray are contained within the filename
if (extensionsArray.Any(fn.Contains))
{
// Create a further sub-folder for the sender
if (!Directory.Exists(basePath + folder.FolderPath + @"\" + mi.Sender.Address))
{
Directory.CreateDirectory(basePath + folder.FolderPath + @"\" + mi.Sender.Address);
}
totalfilesize = totalfilesize + mi.Attachments[i].Size;
if (!File.Exists(basePath + folder.FolderPath + @"\" + mi.Sender.Address + @"\" + mi.Attachments[i].FileName))
{
Console.WriteLine("Saving " + mi.Attachments[i].FileName);
mi.Attachments[i].SaveAsFile(basePath + folder.FolderPath + @"\" + mi.Sender.Address + @"\" + mi.Attachments[i].FileName);
//mi.Attachments[i].Delete();
}
else
{
Console.WriteLine("Already saved " + mi.Attachments[i].FileName);
}
}
}
}
}
}
catch (Exception e)
{
//Console.WriteLine("An error occurred: '{0}'", e);
}
}
}
// Retrieves the email address for a given account object
static string EnumerateAccountEmailAddress(Outlook.Account account)
{
try
{
if (string.IsNullOrEmpty(account.SmtpAddress) || string.IsNullOrEmpty(account.UserName))
{
Outlook.AddressEntry oAE = account.CurrentUser.AddressEntry as Outlook.AddressEntry;
if (oAE.Type == "EX")
{
Outlook.ExchangeUser oEU = oAE.GetExchangeUser() as Outlook.ExchangeUser;
return oEU.PrimarySmtpAddress;
}
else
{
return oAE.Address;
}
}
else
{
return account.SmtpAddress;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "";
}
}
static void EnumerateAccounts()
{
Console.Clear();
Console.WriteLine("Outlook Attachment Extractor v0.1");
Console.WriteLine("---------------------------------");
int id;
Outlook.Application Application = new Outlook.Application();
Outlook.Accounts accounts = Application.Session.Accounts;
string response = "";
while (true == true)
{
id = 1;
foreach (Outlook.Account account in accounts)
{
Console.WriteLine(id + ":" + EnumerateAccountEmailAddress(account));
id++;
}
Console.WriteLine("Q: Quit Application");
response = Console.ReadLine().ToUpper();
if (response == "Q")
{
Console.WriteLine("Quitting");
return;
}
if (response != "")
{
if (Int32.Parse(response.Trim()) >= 1 && Int32.Parse(response.Trim()) < id)
{
Console.WriteLine("Processing: " + accounts[Int32.Parse(response.Trim())].DisplayName);
Console.WriteLine("Processing: " + EnumerateAccountEmailAddress(accounts[Int32.Parse(response.Trim())]));
Outlook.Folder selectedFolder = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
selectedFolder = GetFolder(@"\\" + accounts[Int32.Parse(response.Trim())].DisplayName);
EnumerateFolders(selectedFolder);
Console.WriteLine("Finished Processing " + accounts[Int32.Parse(response.Trim())].DisplayName);
Console.WriteLine("");
}
else
{
Console.WriteLine("Invalid Account Selected");
}
}
}
}
// Returns Folder object based on folder path
static Outlook.Folder GetFolder(string folderPath)
{
Console.WriteLine("Looking for: " + folderPath);
Outlook.Folder folder;
string backslash = @"\";
try
{
if (folderPath.StartsWith(@"\\"))
{
folderPath = folderPath.Remove(0, 2);
}
String[] folders = folderPath.Split(backslash.ToCharArray());
Outlook.Application Application = new Outlook.Application();
folder = Application.Session.Folders[folders[0]] as Outlook.Folder;
if (folder != null)
{
for (int i = 1; i <= folders.GetUpperBound(0); i++)
{
Outlook.Folders subFolders = folder.Folders;
folder = subFolders[folders[i]] as Outlook.Folder;
if (folder == null)
{
return null;
}
}
}
return folder;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}