Capturing Outlook New Mail and Reminder Events

Visual Studio 2008′s Outlook Add-in projects allows you to create add-ins that can customize almost any feature of Ms Outlook.
Two useful events that can be captured are ReminderFire and NewMailEx. The first occurs before a reminder of a calendar item
is executed. And the second occurs when a new email is received in the Inbox.

 
using Outlook=Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;  //for MessageBox object

privateOutlook.Explorers _Explorers;  // the Outlook Explorers collection
    privateOutlook.Inspectors _Inspectors;  // the Outlook Inspectors collection
    privateOutlook.NameSpaceoutlookNamespace;
 
    privatevoidThisAddIn_Startup(objectsender,System.EventArgse)
    {
        _Explorers=this.Application.Explorers;
        _Inspectors=this.Application.Inspectors;
 
        _Explorers.Application.NewMailEx+=new
            Outlook.ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);
 
        _Explorers.Application.Reminders.ReminderFire+=new
            Outlook.ReminderCollectionEvents_ReminderFireEventHandler(Application_ReminderFire);
 
        outlookNamespace=this.Application.GetNamespace("MAPI");
    }

 privatevoidApplication_ReminderFire(Outlook.Reminder reminder)
    {
        MessageBox.Show(reminder.Caption,"New Reminder",MessageBoxButtons.OK);
    }

 privatevoidApplication_NewMailEx(stringEntryID)
    {
        Outlook.MailItem newMail=(Outlook.MailItem)_Explorers.Application.Session.GetItemFromID(
            EntryID,System.Reflection.Missing.Value);
 
        if(newMail.Subject!=null)
        {
            MessageBox.Show("From: "+newMail.SenderEmailAddress+"\nSubject: "+
                newMail.Subject,"New Email",MessageBoxButtons.OK);
        }
        else
        {
            MessageBox.Show("You've got mail.");
        }
    }

customoutlookprompt.zip (52.35 kb)

Posted by: Saurabh

Categories: C#

Tags:

How Can I read an Outlook PST file in C#

using System;
using
System.Collections.Generic;
using
Microsoft.Office.Interop.Outlook;

namespace
PSTReader{
   
classProgram{
       
staticvoidMain(){
           
try{
               
IEnumerable<MailItem> mailItems = readPst(@"C:\temp\PST\Test.pst","Test PST");
               
foreach(MailItem mailItem in mailItems){
                   
Console.WriteLine(mailItem.SenderName+" - "+ mailItem.Subject);
               
}
           
}catch(System.Exception ex){
               
Console.WriteLine(ex.Message);
           
}
           
Console.ReadLine();
       
}

       
privatestaticIEnumerable<MailItem> readPst(string pstFilePath,string pstName){
           
List<MailItem> mailItems =newList<MailItem>();
           
Application app =newApplication();
           
NameSpace outlookNs = app.GetNamespace("MAPI");
           
// Add PST file (Outlook Data File) to Default Profile
            outlookNs
.AddStore(pstFilePath);
           
MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
           
// Traverse through all folders in the PST file
           
// TODO: This is not recursive, refactor
           
Folders subFolders = rootFolder.Folders;
           
foreach(Folder folder in subFolders){
               
Items items = folder.Items;
               
foreach(object item in items){
                   
if(item isMailItem){
                       
MailItem mailItem = item asMailItem;
                        mailItems
.Add(mailItem);
                   
}
               
}
           
}
           
// Remove PST file from Default Profile
            outlookNs
.RemoveStore(rootFolder);
           
return mailItems;
       
}
   
}
}

Posted by: Saurabh

Categories: C#

Tags: