N
nickname
I am trying to automate outlook to save attachments to emails in a folder
c:\test and then move the emails from the inbox to a folder (in outlook)
called "Auto Processed Emails". I have got this to work, however outlook will
not close itself afterwards. The code i have written is below, can somebody
let me know what i have done wrong?
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
ns.Logon("test", Type.Missing, Type.Missing, true);
//Fetch mail
ns.SendAndReceive(false);
//block 30seconds give time to process
System.Threading.Thread.Sleep(30);
Outlook.MAPIFolder inBox =
ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items inboxItems = inBox.Items;
for (int i = 1; i <= inboxItems.Count; i++)
{
object item = inboxItems;
if (item is Outlook.MailItem)
{
Outlook.MailItem mailItem = (Outlook.MailItem)item;
//Check Drop Folder Exists
bool folderFound = false;
Outlook.MAPIFolder parentFolder =
(Outlook.MAPIFolder)inBox.Parent;
Outlook.Folders foldersCollection = parentFolder.Folders;
for (int j = 1; j <= foldersCollection.Count; j++)
{
Outlook.MAPIFolder currentFolder =
foldersCollection[j];
if (currentFolder.Name == "Auto Processed Emails")
{
folderFound = true;
}
Marshal.ReleaseComObject(currentFolder).ToString();
}
if (!folderFound)
{
return;
}
//Save Attachments
for (int j = 1; j <= mailItem.Attachments.Count; j++)
{
Outlook.Attachment oAttach = mailItem.Attachments[j];
oAttach.SaveAsFile(Path.Combine(@"c:\test",
oAttach.FileName));
Marshal.ReleaseComObject(oAttach).ToString();
}
}
Marshal.ReleaseComObject(item);
}
Marshal.ReleaseComObject(inboxItems);
Marshal.ReleaseComObject(inBox);
ns.Logoff();
Marshal.ReleaseComObject(ns);
app.Quit();
Marshal.ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();
c:\test and then move the emails from the inbox to a folder (in outlook)
called "Auto Processed Emails". I have got this to work, however outlook will
not close itself afterwards. The code i have written is below, can somebody
let me know what i have done wrong?
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
ns.Logon("test", Type.Missing, Type.Missing, true);
//Fetch mail
ns.SendAndReceive(false);
//block 30seconds give time to process
System.Threading.Thread.Sleep(30);
Outlook.MAPIFolder inBox =
ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items inboxItems = inBox.Items;
for (int i = 1; i <= inboxItems.Count; i++)
{
object item = inboxItems;
if (item is Outlook.MailItem)
{
Outlook.MailItem mailItem = (Outlook.MailItem)item;
//Check Drop Folder Exists
bool folderFound = false;
Outlook.MAPIFolder parentFolder =
(Outlook.MAPIFolder)inBox.Parent;
Outlook.Folders foldersCollection = parentFolder.Folders;
for (int j = 1; j <= foldersCollection.Count; j++)
{
Outlook.MAPIFolder currentFolder =
foldersCollection[j];
if (currentFolder.Name == "Auto Processed Emails")
{
folderFound = true;
}
Marshal.ReleaseComObject(currentFolder).ToString();
}
if (!folderFound)
{
return;
}
//Save Attachments
for (int j = 1; j <= mailItem.Attachments.Count; j++)
{
Outlook.Attachment oAttach = mailItem.Attachments[j];
oAttach.SaveAsFile(Path.Combine(@"c:\test",
oAttach.FileName));
Marshal.ReleaseComObject(oAttach).ToString();
}
}
Marshal.ReleaseComObject(item);
}
Marshal.ReleaseComObject(inboxItems);
Marshal.ReleaseComObject(inBox);
ns.Logoff();
Marshal.ReleaseComObject(ns);
app.Quit();
Marshal.ReleaseComObject(app);
GC.Collect();
GC.WaitForPendingFinalizers();