1
votes

I'm having a problem right now with checking if an e-mail was sent or not. So far I've tried these three methods of subscribing to such event:

  • application.ItemSend += ApplicationOnItemSend;
  • application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderOutbox).Items.ItemAdd += ItemsOnItemAdd;
  • application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail).Items.ItemAdd += ItemsOnItemAdd2;

All three of these work great when Outlook is fully opened, my problem arises when you're opening a msg file with Outlook not being open in the background. In those cases all you see of it is the "new message" window and when you send the mail from there, none of these events fire. Even with the option checked that mails should be sent as soon as possible when there's a network connection, the mail gets put in the outbox and at the next full start of Outlook there's one mail that has been put in the outbox but not fully sent. Even on my co-workers computers, without my Outlook addin installed and the same option set (as should be the standard), there is the same problem of the mail just being put in the outbox.
I've also checked the ways I'm trying to subscribe to these events and none of my event handlers contain any code that blocks background threads (like when I'm using MessageBox.Show() for debugging purposes). So there's no apparent reason any of my code is stopping the mail from being sent.
The point of where I'm subscribing to these events also doesn't seem to make a difference. Normally I'd subscribe at the addin startup, but that method is not called when just opening the msg file. So I've moved the subcribing to the load event of the ribbon. The load event prints my debug messages indicating the method was called, but the ItemSend/ItemAdd events are still not firing upon pressing "send".
Is all of this just Outlook malfunctioning or is there still a way I could check for the mail being sent from the "new message" window?

1

1 Answers

0
votes

Firstly, the last two even handlers won't work - the object raising the event must be kept alive. In your case, you are setting event handlers on an implicit variable that will get released buy the Garbage Collector the next time it runs in a few seconds. The Items collection raising the events must be kept in a separate gloab l(class level) variable.

Secondly, do not touch messages in the Outbox folder - doing so aborts the submission.