AdrianMills1973

Hi

We currently have a shared mail box which users email support request to. I am trying to write an Outlook Add-on in C# which alerts us when a new email arrives into the mailbox. I can get the MAPI folder required but how can I get the NewMailEx event to fire from this folder and not my default Inbox

Cheers

Ade



Re: Visual Studio Tools for Office Monitoring a shared mailbox

Sue Mosher - Outlook MVP

NewMailEx is the wrong event. Instead, you need to be using the MAPIFolder.Items.ItemAdd event on the remote folder.





Re: Visual Studio Tools for Office Monitoring a shared mailbox

AdrianMills1973

Thanks Sue

I have added the event handler for the ItemAdd Event. When i have the code in a normal Windows app everything works however when I try it in an outlook add on the event doesn't fire. my code is below any help would be appreciated.

using System;

using System.Windows.Forms;

using Microsoft.VisualStudio.Tools.Applications.Runtime;

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;

namespace FolderMon

{

public partial class ThisApplication

{

private Outlook.MAPIFolder MonitoredFolder;

private void ThisApplication_Startup(object sender, System.EventArgs e)

{

//Get the Folder to be monitered

MonitoredFolder = GetFolder("Mailbox - $Ecommerce\\Inbox");

//If there are already items in the monitered folder then display

//Message box with how many emails are in there.

if (MonitoredFolder.Items.Count > 0)

{

MessageBox.Show("There are " + MonitoredFolder.Items.Count + " Items in the shared mailbox awaiting proccessing", "Items in Shared Mail Box", MessageBoxButtons.OK, MessageBoxIcon.Hand);

}

//Create the event handler that handles any new items going into the

//Monitored folder

this.MonitoredFolder.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

}

private Outlook.MAPIFolder GetFolder(string strFolderPath)

{

//create Outlook application

Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();

//create namespace object

Outlook.NameSpace objNameSpace;

//Create a collection of folders

Outlook.Folders colFolders;

//Create the temp MAPI Folder

Outlook.MAPIFolder myFolder = null;

//Create a string array to store the list of folders

string[] arrFolders;

//Replace Any occurance in the folder path of / with \

strFolderPath = strFolderPath.Replace('/', '\\');

//SPlit the folder path into its seperate folders

arrFolders = strFolderPath.Split('\\');

//Set the namespace object with the MAPI Name Space

objNameSpace = objOutlook.GetNamespace("MAPI");

//Attempt to get the first folder in the array

myFolder = objNameSpace.Folders[arrFolders[0]];

//if the folder is null then exit

if (myFolder != null)

{

//loop around the folder until we get to the last one

for (long i = 1; i == arrFolders.GetUpperBound(0); i++)

{

//Get the folders in my folders

colFolders = myFolder.Folders;

//reset the My folder to null

myFolder = null;

//attempt to get the next folder in the list

myFolder = colFolders[arrFoldersIdea];

// If it is null then exit the loop

if (myFolder != null)

break;

}

}

colFolders = null;

objNameSpace = null;

objOutlook = null;

return myFolder;

}

Cheers

Ade





Re: Visual Studio Tools for Office Monitoring a shared mailbox

Sue Mosher - Outlook MVP

The object firing the events needs to be declared at the class level. That's the Items object, not MonitoredFolder.

BTW, a statement like this does not belong in an Outlook add-in:

Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();

Instead, you need to be using the Outlook.Application object that the add-in architecture exposes, so that all objects will not be subject to Outlook security prompts.





Re: Visual Studio Tools for Office Monitoring a shared mailbox

AdrianMills1973

Thanks for the fast response Sue. I am having trouble getting my head around this the only time i am seeing the Event with intellisense is when i call it from my MonitoredFolder object. Could you give me a code example of the Items Object using the event.

Cheers

Ade





Re: Visual Studio Tools for Office Monitoring a shared mailbox

Sue Mosher - Outlook MVP

Exactly. The object shows up in Intellisense just fine. But the problem is because you have no Items object declared at the class level, the Items object firing the events is implicit, not explicit, and will be released by the .NET garbage collection. To fix this problem, you should make four changes to your code:

  • Declare an Items object at the class level instead of MonitoredFolder, e.g.:

private Outlook.Items MonitoredItems;

  • Use GetFolder() to return MonitoredFolder as you're already doing, but as a procedure-level variable.
  • Instantiate the Items object declared in #1, e.g.:

MonitoredItems = MonitoredFolder.Items;

  • Wire the event handler to the ItemAdd event of the Items object declared in #1, e.g.:

this.MonitoredItems.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler

Unfortunately, I don't actually write C# code, so I all I've done is copy, paste, and do some variable name changes on your original statements. I hope it makes sense to you.





Re: Visual Studio Tools for Office Monitoring a shared mailbox

AdrianMills1973

Thanks Sue

You have made it all clear to me now thanks.

Cheers

Ade





Re: Visual Studio Tools for Office Monitoring a shared mailbox

sairama

Thank you very much,

I struggled with NewMailEx for a while and saw this thread and within no time I was able to get what I want.

sairama