using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAttachmentExtractor
static string basePath = @"c:\temp\emails\";
static int totalfilesize = 0;
static void Main(string[] args)
static void EnumerateFoldersInDefaultStore()
Outlook.Application Application = new Outlook.Application();
Outlook.Folder root = Application.Session.DefaultStore.GetRootFolder() as Outlook.Folder;
static void EnumerateFolders(Outlook.Folder folder)
Outlook.Folders childFolders = folder.Folders;
if (childFolders.Count > 0)
foreach (Outlook.Folder childFolder in childFolders)
if (childFolder.FolderPath.Contains("Inbox"))
Console.WriteLine(childFolder.FolderPath);
EnumerateFolders(childFolder);
Console.WriteLine("Checking in " + folder.FolderPath);
static void IterateMessages(Outlook.Folder folder)
string[] extensionsArray = { ".pdf", ".doc", ".xls", ".ppt", ".vsd", ".zip", ".rar", ".txt", ".csv", ".proj" };
foreach (Object item in fi)
Outlook.MailItem mi = (Outlook.MailItem)item;
var attachments = mi.Attachments;
if (attachments.Count != 0)
if (!Directory.Exists(basePath + folder.FolderPath))
Directory.CreateDirectory(basePath + folder.FolderPath);
for (int i = 1; i <= mi.Attachments.Count; i++)
var fn = mi.Attachments[i].FileName.ToLower();
if (extensionsArray.Any(fn.Contains))
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);
Console.WriteLine("Already saved " + mi.Attachments[i].FileName);
static string EnumerateAccountEmailAddress(Outlook.Account account)
if (string.IsNullOrEmpty(account.SmtpAddress) || string.IsNullOrEmpty(account.UserName))
Outlook.AddressEntry oAE = account.CurrentUser.AddressEntry as Outlook.AddressEntry;
Outlook.ExchangeUser oEU = oAE.GetExchangeUser() as Outlook.ExchangeUser;
return oEU.PrimarySmtpAddress;
return account.SmtpAddress;
Console.WriteLine(ex.Message);
static void EnumerateAccounts()
Console.WriteLine("Outlook Attachment Extractor v0.1");
Console.WriteLine("---------------------------------");
Outlook.Application Application = new Outlook.Application();
Outlook.Accounts accounts = Application.Session.Accounts;
foreach (Outlook.Account account in accounts)
Console.WriteLine(id + ":" + EnumerateAccountEmailAddress(account));
Console.WriteLine("Q: Quit Application");
response = Console.ReadLine().ToUpper();
Console.WriteLine("Quitting");
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("Invalid Account Selected");
static Outlook.Folder GetFolder(string folderPath)
Console.WriteLine("Looking for: " + folderPath);
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;
for (int i = 1; i <= folders.GetUpperBound(0); i++)
Outlook.Folders subFolders = folder.Folders;
folder = subFolders[folders[i]] as Outlook.Folder;
Console.WriteLine(ex.Message);