Get Outlook folders and email messages

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class ThisAddIn
{
 private Outlook.Inspectors withEventsField_PublicOutlookInspectors;
 public Outlook.Inspectors PublicOutlookInspectors {
  get { return withEventsField_PublicOutlookInspectors; }
  set {
   if (withEventsField_PublicOutlookInspectors != null) {
    withEventsField_PublicOutlookInspectors.NewInspector -= PublicOutlookInspectors_NewInspector;
   }
   withEventsField_PublicOutlookInspectors = value;
   if (withEventsField_PublicOutlookInspectors != null) {
    withEventsField_PublicOutlookInspectors.NewInspector += PublicOutlookInspectors_NewInspector;
   }
  }

 }
 private void ThisAddIn_Startup(object sender, System.EventArgs e)
 {
  PublicOutlookInspectors = this.Application.Inspectors;
  Outlook.Application OutlookApplication = new Outlook.Application[];
  Outlook._NameSpace OutlookNameSpace = (Outlook._NameSpace)OutlookApplication.GetNamespace("MAPI");
  Outlook.MAPIFolder OutlookMapiFolder = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent;
  //Dim OutlookExplorer As Outlook._Explorer = OutlookMapiFolder.GetExplorer(False)
  OutlookNameSpace.Logon(string.Empty, string.Empty, false, true);
  Outlook.Items OutlookMapiFolderItems = OutlookMapiFolder.Items;
  GetFoldersRecursive(OutlookMapiFolder);
 }

 public void GetFoldersRecursive(Outlook.MAPIFolder OutlookMAPIFolder)
 {
  if (OutlookMAPIFolder.Folders.Count == 0) {
   System.Diagnostics.Debug.WriteLine("Folder: " + OutlookMAPIFolder.FullFolderPath);
   foreach (object OutlookMAPIFolderItem in OutlookMAPIFolder.Items) {
    if ((OutlookMAPIFolderItem) is Outlook.MailItem) {
     System.Diagnostics.Debug.WriteLine("Subject: " + OutlookMAPIFolderItem.Subject);
    }
   }
  } else {
   foreach (Outlook.MAPIFolder OutlookMAPISubFolder in OutlookMAPIFolder.Folders) {
    GetFoldersRecursive(OutlookMAPISubFolder);
   }
  }
 }


 private void ThisAddIn_Shutdown()
 {
 }

 private void PublicOutlookInspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
 {
  Outlook.MailItem OutlookMailItem = Inspector.CurrentItem as Outlook.MailItem;
  if (OutlookMailItem != null) {
   if (OutlookMailItem.EntryID == null) {
    OutlookMailItem.Subject = "This text was added by using code";
    OutlookMailItem.Body = "This text was added by using code";
   }
  }
 }
 public ThisAddIn()
 {
  Shutdown += ThisAddIn_Shutdown;
  Startup += ThisAddIn_Startup;
 }

}

No comments:

Post a Comment