C# Outllook 2003 Add-In Problem with Application.ItemSend processi

E

Estaup

I am capturing the Application.ItemSend event without issues, the problem
comes in with what I am trying to do.

When a user clicks the Send button I do the following processing:

Lookup user to see if they have ability to send emails (this is handled with
DB table) if so cancel=false and the email is sent.

If the user does not have the ability to send emails then put the email in a
folder called "Pending" and cancel=true and do not send the email.

here is the code
myInspector = this.application.activeinspector();
Microsoft.Office.Interop.Outlook.Inspector myItem = myInspector;
mailitem = (Outlook.Mailitem)myItem.CurrentItem;

Outlook.MAPIFolder inbox =
(Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GeDefaultFolder(Outlook.OlDefaultFolders.olFolderInBox);

Outlook.MAPIFolder destFolder = inbox.Folders["Pending"];
mailitem.Move(destFolder);
cancel = true;


When this executes, the mailitem.Move(desFolder) line causes an exception
error:
Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

(This same code works just fine in Outlook 2007. The mailitem even closes.)

I have tried making a copy of the mailitem and then moving the copy to the
Pending folder. This works without issue. But the current mail item is still
displayed to the user. I can not get this to close.

Please help.

Thanks.
 
K

Ken Slovak - [MVP - Outlook]

It's usually better to handle the item.Send() event rather than the
Application.ItemSend() event, which occurs later in the send cycle. Since
the item in ItemSend() has already been submitted to the mail transport
moving it can fire exceptions.

The best solution is usually to cancel the send in item.Send() and start a
timer to fire in maybe 10 ms. When the timer fires move the item and close
the Inspector if it's still open.

BTW, Move() is a function that returns a handle to the new item.
 
E

Estaup

I am attempting to change to the Item.Send event for the process but I am
having difficulty in specificing it in the code.

Outlook.ItemSend is not it.

I've tried this.ItemSend. What is the syntax on getting to this level?

Thanks.

Ken Slovak - said:
It's usually better to handle the item.Send() event rather than the
Application.ItemSend() event, which occurs later in the send cycle. Since
the item in ItemSend() has already been submitted to the mail transport
moving it can fire exceptions.

The best solution is usually to cancel the send in item.Send() and start a
timer to fire in maybe 10 ms. When the timer fires move the item and close
the Inspector if it's still open.

BTW, Move() is a function that returns a handle to the new item.




Estaup said:
I am capturing the Application.ItemSend event without issues, the problem
comes in with what I am trying to do.

When a user clicks the Send button I do the following processing:

Lookup user to see if they have ability to send emails (this is handled
with
DB table) if so cancel=false and the email is sent.

If the user does not have the ability to send emails then put the email in
a
folder called "Pending" and cancel=true and do not send the email.

here is the code
myInspector = this.application.activeinspector();
Microsoft.Office.Interop.Outlook.Inspector myItem = myInspector;
mailitem = (Outlook.Mailitem)myItem.CurrentItem;

Outlook.MAPIFolder inbox =
(Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GeDefaultFolder(Outlook.OlDefaultFolders.olFolderInBox);

Outlook.MAPIFolder destFolder = inbox.Folders["Pending"];
mailitem.Move(destFolder);
cancel = true;


When this executes, the mailitem.Move(desFolder) line causes an exception
error:
Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))

(This same code works just fine in Outlook 2007. The mailitem even
closes.)

I have tried making a copy of the mailitem and then moving the copy to the
Pending folder. This works without issue. But the current mail item is
still
displayed to the user. I can not get this to close.

Please help.

Thanks.

.
 
K

Ken Slovak - [MVP - Outlook]

It's the Send() event for an item, in this case a MailItem. You need to
handle Inspectors.NewInspector() and instantiate an Inspector handler. Then
you instantiate the Inspector.CurrentItem and add a handler for the Send()
event.
 
E

Estaup

Running into the problem of everything working but in the end, the new
mailitem is still open. I need for this to close and trying to execute a
..Close on the mailitem results in exception indicating the you can not close
a mailitem during the send event.

If the user is not allowed to send the email, I want to save the email but
also close the mailitem automatically.

Any ideas?

In Outllook 2007, the mailitem.Move() command takes care of this and the
cancel=true takes care of the open mailitem without any expections.

Thanks.
 
K

Ken Slovak - [MVP - Outlook]

Read what I wrote earlier in this thread:

The best solution is usually to cancel the send in item.Send() and start a
timer to fire in maybe 10 ms. When the timer fires move the item and close
the Inspector if it's still open.

That's what I've been doing for this sort of thing since the Outlook 2000
days.
 
E

Estaup

OK. That worked just perfect.

Thanks.

Ken Slovak - said:
Read what I wrote earlier in this thread:

The best solution is usually to cancel the send in item.Send() and start a
timer to fire in maybe 10 ms. When the timer fires move the item and close
the Inspector if it's still open.

That's what I've been doing for this sort of thing since the Outlook 2000
days.






.
 
E

Estaup

OK. Now something else happening that is kind of strange.

When I determine that a user able to send an email, the email gets sent but
a copy is still sitting in the Outbox with the Sent value equal to "None".

Is there something that I am missing within the ItemSend processing?

I have searched for something similar and just found information where you
turn the Add-in's off one at a time until you find the add-in causing the
problem. Well I have done that and it certainly is my add-in.

Thanks.
 
K

Ken Slovak - [MVP - Outlook]

Does the item send eventually or never? Is it italicized in the outbox or
not?

Usually if the item never sends it's due to a reference to the object not
released, or the item was touched in the Outbox. Once an item is submitted
to the transport if you modify it you must send it again. That does not
apply to modifications in the Send() event though.

If you just want the item to send and not wait for a scheduled send/receive
you can use SyncObjects[1].Start(). That will start a send/receive.
 
E

Estaup

The item sends immediately and a copy is in the Sent Items folder but a copy
is also now in the Outbox.

If I open one of those emails and push the send button again, it will send
the mail and will then be removed from the Outbox.

Thanks.

Ken Slovak - said:
Does the item send eventually or never? Is it italicized in the outbox or
not?

Usually if the item never sends it's due to a reference to the object not
released, or the item was touched in the Outbox. Once an item is submitted
to the transport if you modify it you must send it again. That does not
apply to modifications in the Send() event though.

If you just want the item to send and not wait for a scheduled send/receive
you can use SyncObjects[1].Start(). That will start a send/receive.




Estaup said:
OK. Now something else happening that is kind of strange.

When I determine that a user able to send an email, the email gets sent
but
a copy is still sitting in the Outbox with the Sent value equal to "None".

Is there something that I am missing within the ItemSend processing?

I have searched for something similar and just found information where you
turn the Add-in's off one at a time until you find the add-in causing the
problem. Well I have done that and it certainly is my add-in.

Thanks.

.
 
K

Ken Slovak - [MVP - Outlook]

I've never seen that. I'd check the code and make sure you aren't creating a
new item and sending it that's showing up in Outbox.

Are you cancelling the original send and leaving that item around? There has
to be a reason for the duplications.
 
E

Estaup

I have the code working correctly now and have instituted something you
mention before as well.

Originally I was making a copy of the current mail item and then place the
copy into the Pending folder for an email. I was then closing the closing all
the inspectors and mail items with a discard in the timer event.

You had mentioned doing the move in the timer event so I made that change
and got rid of the copy event that was taking place however that did not
completely resolve the issue.

I then changed all the .Close with Discard commands to be
Marshal.ReleaseComObject commands and followed up with a GC.Collect in the
timer event.

This took care of the duplication problem and all the windows were closing
correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top