Working with MAPIStoreAccessor gives problems

Add-in Express™ Support Service
That's what is more important than anything else

Working with MAPIStoreAccessor gives problems
Outlook stops sending e-mails 
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
Hi,
I have an add-in where I use MAPIStoreAccessor to monitor events and inspect MAPI properties on mail items.
When the add-in loads I call Initialize(false), and the requested events is fired as needed.

I monitor ItemMoved and ItemCreated events. I inspect which folder the items are in, as items dropped into certain folders must be stored on a document server. If they are in one of my special flders, the items are saved and then deleted. And that works perfectly.

But when I send e-mails and calendar invitations, they just stay in the outbox, unless I manually resend the items from the Outbox. So I'm thinking that it may be something my code is doing, which prevent the mails from being send. Could it be as easy as not releasing a COM object? However, I'm not sure what that should be? I get the item as an object and then typcaste the object into MailItem or DocumentItem (depending) and in the end release the object. But I do not release the typcasted version, which I believe is the correct way, as they point to the object which is released later.

This is my code:

private void adxmapiStoreAccessor1_OnObjectMoved(object sender, AddinExpress.MAPI.ADXMAPIObjectNotificationEventArgs e)
{
ProcessMAPIEvent(e);
}

private void adxmapiStoreAccessor1_OnObjectCreated(object sender, AddinExpress.MAPI.ADXMAPIObjectNotificationEventArgs e)
{
ProcessMAPIEvent(e);
}

private void ProcessMAPIEvent(AddinExpress.MAPI.ADXMAPIObjectNotificationEventArgs e)
{
string id = e.EntryId.ToString();
string storeid = e.StoreId.ToString();

object item = null;
bool handleDrop = false;
string parentDesc = "";
try
{
item = OutlookApp.GetNamespace("MAPI").GetItemFromID(id, storeid);

// check for mails
if (item is Outlook.MailItem)
{
Outlook.MailItem mail = null;
Outlook.MAPIFolder parent = null;
try
{
mail = (Outlook.MailItem)item;
if (mail.Parent is Outlook.MAPIFolder)
{
parent = (Outlook.MAPIFolder)mail.Parent;
if IsMyFolder(parent)
X handleDrop = true;
}
}
finally
{
if (parent != null)
Marshal.ReleaseComObject(parent);
}
}
// check for documents
else if (item is Outlook.DocumentItem)
{
Outlook.DocumentItem doc = null;
Outlook.MAPIFolder parent = null;
try
{
doc = (Outlook.DocumentItem)item;
if (doc.Parent is Outlook.MAPIFolder)
{
parent = (Outlook.MAPIFolder)doc.Parent;
if IsMyFolder(parent)
X handleDrop = true;
}
}
finally
{
if (parent != null)
Marshal.ReleaseComObject(parent);
}
}

if (handleDrop)
SaveItem(item);
}
finally
{
if (item != null)
Marshal.ReleaseComObject(item);
}

}

Can you find any fault with the code?
Regards
Thomas
Posted 20 Jan, 2010 03:25:20 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hello Thomas,

Please try releasing COM objects. For example:

OutlookApp.GetNamespace("MAPI").GetItemFromID(id, storeid);

Where do you release a namespace class instance?

mail.Parent is Outlook.MAPIFolder

Where do you release a parent object?
.NET, Extended MAPI, Windows Phone & RT
Posted 20 Jan, 2010 04:00:27 Top
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
It's probably the namespace, as I doesn't release that one. Thanks! I completely overlooked that one!

The MAPIFolder parent is released in a try finally for each type (MailItem and DocumentItem):
finally
{
if (parent != null)
Marshal.ReleaseComObject(parent);
}

Regards
Thomas
Posted 20 Jan, 2010 05:16:07 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Thomas,

The MAPIFolder parent is released in a try finally for each type (MailItem and DocumentItem):
finally
{
if (parent != null)
Marshal.ReleaseComObject(parent);
}


It is the second instance. Did you release the first one?
.NET, Extended MAPI, Windows Phone & RT
Posted 20 Jan, 2010 06:22:11 Top
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
Yes, each instance is in it's own "if (item is MailItem)" or "if (item is DocumentItem)" and both has a try... finally section.

Anyway, releasing the NameSpace didn't help, so I did a little reengineering, and found out, that as soon as I touched the Parent property of the MailItem, the mail wasn't sent, even if I did release the MAPIFolder after. So what else could cause this problem?

Here's my new code:

private void ProcessMAPIEvent(AddinExpress.MAPI.ADXMAPIObjectNotificationEventArgs e)
{
string id = e.EntryId.ToString();
string storeid = e.StoreId.ToString();

Outlook.NameSpace ns = null;
Outlook.MAPIFolder parent = null;
Outlook.MailItem mail = null;
Outlook.DocumentItem doc = null;
object item = null;
bool handleDrop = false;
string parentDesc = "";
try
{
ns = OutlookApp.GetNamespace("MAPI");
item = ns.GetItemFromID(id, storeid);

if (item == null)
return;

// check for mails
if (item is Outlook.MailItem)
{
mail = (Outlook.MailItem)item;
parent = (Outlook.MAPIFolder)mail.Parent;

if (parent != null)
{
if ((parent.Description != null) && (parent.Description.StartsWith(PROP_FOLDER_ID)))
handleDrop = true;
}
}
// check for documents
else if (item is Outlook.DocumentItem)
{
doc = (Outlook.DocumentItem)item;
parent = (Outlook.MAPIFolder)doc.Parent;

if (parent != null)
{
if ((parent.Description != null) && (parent.Description.StartsWith(PROP_FOLDER_ID)))
handleDrop = true;
}
}

if (handleDrop)
SaveItem(item);
}
finally
{
if (parent != null)
Marshal.ReleaseComObject(parent);

if (item != null)
Marshal.ReleaseComObject(item);

if (ns != null)
Marshal.ReleaseComObject(ns);
}

}
Posted 21 Jan, 2010 06:41:23 Top
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
Just to clarify: As soon as I disable this line:

parent = (Outlook.MAPIFolder)mail.Parent;

the mails are sent correctly. When I enabled it, mails are no longer sent.

I'm using:
Windows XP latest SP, latest patch
Office 2007 latest SP and patch.
AdxMAPIStoreAccessor version 2.3.219.2008.

Regards
Thomas
Posted 21 Jan, 2010 06:51:56 Top
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
Just one more bit of information:

If I reply to an e-mail, it is sent.
If I create a new e-mail, it is not sent.

What could be the difference in these two scenarios?

Regards
Thomas
Posted 21 Jan, 2010 07:46:03 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hello Thomas,

Could you send a sample add-in project which can reproduce the issue to the support e-mail address (see readme.txt)?
.NET, Extended MAPI, Windows Phone & RT
Posted 21 Jan, 2010 07:57:30 Top
Bargholz Thomas




Posts: 236
Joined: 2006-12-18
Hi Eugene,
I have sent a demo project to support, with subject "Working with MAPIStoreAccessor gives problems".
Regards
Thomas
Posted 21 Jan, 2010 10:00:11 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Thomas,

The problem occurs because your code reads the item when it is moved to the Outbox. You can try getting the folder using ns.GetFolderFromID(e.ParentID) instead of mail.Parent.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Jan, 2010 11:01:11 Top