Outlook 2010 add in for auto extraction of attachment from the configured email ids on outlook 2010.For each configured email ids there will be a separate folder in which it's attachments will be saved automatically.I don't want to click any button or reload it all the times.If a mail arrives in the inbox folder,if it's unread, it's attachments will be extracted and saved in its respective folder.
My problem is that i haven't been able to extract the attachments of non-default email ids on outlook 2010 and also my process is not automatically extracting the attachment.
How can i make the unread mail's attachment extraction and saving automatic for multiple configured email ids on outlook 2010? Here i'm attaching the code which i have tried....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.IO;
namespace ITAPOutlookAddIn
{
public partial class ThisAddIn
{
public void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new Microsoft.Office.Interop.Outlook
.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
this.Application.NewMail += new Microsoft.Office.Interop.Outlook
.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMailStatus);
}
public void ThisApplication_NewMail()
{
const string destinationDirectory = @"C:\TestFileSave";
const string destinationDirectory2 = @"C:\TestFileForm";
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
if (!Directory.Exists(destinationDirectory2))
{
Directory.CreateDirectory(destinationDirectory2);
}
Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
.Session.GetDefaultFolder(Outlook
.OlDefaultFolders.olFolderInbox);
Outlook.Items inBoxItems = inBox.Items;
Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");
try
{
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Outlook.MailItem;
if (newEmail == null)
continue;
if (newEmail != null)
{
if (newEmail.ReceivedByName == "Sumit Ray")
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
string filepath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
string Sname = newEmail.SentOnBehalfOfName;
string timestamp = newEmail.ReceivedTime.ToString("MMddyyyy.HHmmss");
string result = filepath + Sname + timestamp + ".docx";
newEmail.Attachments[i].SaveAsFile(result);
// newEmail.Attachments[i].SaveAsFile
// (@"C:\TestFileSave\" +
// newEmail.Attachments[i].FileName);
}
}
} //end of inner-if
} //end of outer-if
} //end of for-each
}//end of try
catch (Exception ex)
{
Console.WriteLine(ex);
string errorInfo = (string)ex.Message
.Substring(0, 11);
if (errorInfo == "Cannot save" && newEmail.SenderName == "Sumit Ray")
{
MessageBox.Show(@"Create Folder C:\TestFileSave");
}
} //end of catch void ThisApplication_NewMailStatus()
{
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
// Mark each unread message from Jeff Hay with a yellow flag icon.
Outlook.Items unreadMailItems =
inbox.Items.Restrict("[Unread]= true");
// if (Convert.ToBoolean(unreadMailItems)
if(unreadMailItems.Equals(true))
{
ThisApplication_NewMail();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}