Mail Compose Window hung while processing Send in Outlook 2013

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

Mail Compose Window hung while processing Send in Outlook 2013
Mail Compose Window hung while processing Send in Outlook 2013 
Madhusudhana Narayanappa




Posts: 8
Joined: 2017-04-04
Hi,
I am using a sample code "outlook Item events Demo" to process the email send event.

My requirement is
When a mail is being sent, I need to upload some files(defined in a file) to my server which gives me back a hyperlink.
And this hyper link to added to the email body.

The problem I see is
while I upload is progress, the mail compose windows stays on top unresponsive, which gives a feel of application hang.

It is preferable it goes off and sits in the outbox as this upload is in progress.


Please suggest if my approach is wrong and if there is a better way of handling this situation.


regards,
Madhusudhana
Posted 04 Apr, 2017 09:29:31 Top
Andrei Smolin


Add-in Express team


Posts: 14136
Joined: 2006-05-11
Hello Madhusudhana,

First off, you should use the ItemSend event; it is provided by the Outlook Events component (ADXOutlookAppEvents).

If the upload operation is lengthy, you can cancel the ItemSend event, close the inspector window, upload the files, modify the email, and send it using MailItem.Send(). This will trigger the ItemSend event; you will need to use a flag to prevent an endless loop.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Apr, 2017 10:12:38 Top
Madhusudhana Narayanappa




Posts: 8
Joined: 2017-04-04
I tried this
public override void ProcessSend(AddinExpress.MSO.ADXCancelEventArgs e)
{
if (this.ItemObj is Outlook._MailItem)
{
//e.Cancel = true;

Outlook._MailItem mail = (Outlook._MailItem)ItemObj;
((Outlook._Inspector)(mail.GetInspector)).Close(Outlook.OlInspectorClose.olDiscard);
.
.
.
//Upload File

// Modify the emailbody

mail.Send();//This is causes an exception since the it is send already in process
.
.
.
}
}


mail.Send() causes an exception saying send already in process. when I comment it out, the operation is successful.

It closes the mail compose window. But still the main window is unresponsive, and the mail is not in outbox until this is completed.
Posted 08 Apr, 2017 01:17:22 Top
Andrei Smolin


Add-in Express team


Posts: 14136
Joined: 2006-05-11
Hello Madhusudhana,

Don't send in the ItemSend event. Cancel the ItemSend event and let it complete. Then close the inspector window, upload the files, modify the email, and send it using MailItem.Send().

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Apr, 2017 03:18:18 Top
Madhusudhana Narayanappa




Posts: 8
Joined: 2017-04-04
Sorry for replying late.
I worked.
I set the e.Cancel=true in the mail send event handler and schedule a thread to modify the email and then call mailitem.Send()
Now the main window is responsive.


While this is being processed it sits in the Drafts folder.
Posted 20 Apr, 2017 14:16:12 Top
Andrei Smolin


Add-in Express team


Posts: 14136
Joined: 2006-05-11
Hello Madhusudhana,

Madhusudhana Narayanappa writes:
I set the e.Cancel=true in the mail send event handler and schedule a thread to modify the email and then call mailitem.Send() Now the main window is responsive.


Using a thread to retrieve data from the server is a good idea. Using a thread to modify an email isn't a good idea. The problem is: all of the Office object models are *not* thread-safe. You should only do change an email in the main thread. Say, you may want to use the machinery that we describe in section Wait a Little at https://www.add-in-express.com/docs/net-office-tips.php#wait-a-little to send a message from a background thread to let your code code modify the email in the main thread.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Apr, 2017 05:31:18 Top