I'm writing an outlook addin office VS 2010 Office tools.
Here I find a way to list of unread emails from Inbox, but I want to process only new mails.
What I'm trying to do is - when an email comes with subject containing "my project", it should create a task. Here is my code
void Application_NewMail()
{
string filter = "my project";
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.
OlDefaultFolders.olFolderInbox);
Outlook.Items items = inbox.Items.Restrict("[Unread] = true");
foreach (Outlook.MailItem mail in items)
{
if (mail.MessageClass == "IPM.Note" &&
mail.Subject.ToUpper().Contains(filter.ToUpper()))
{
CreateTask(mail);
}
}
}
private void CreateTask(Outlook.MailItem mail)
{
Outlook.TaskItem task = new Outlook.TaskItem();
task.Subject = mail.Subject;
task.Body = mail.Body;
task.Save();
}