C# Outlook Remove event when email sent successfully? - c#

I have a plugin outlook, I create an event when an email sent successfully, this code:
private Outlook.Items _items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Application application = this.Application;
_items = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).Items;
_items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
}
Then, I have the user setting, and if the user doesn't select, I want to remove that event (Items_ItemAdd).
So should I do?

Well, tracking the Sent Items folder is not really a good idea. Outlook allows to remove a sent item by skipping the Sent Items folder. The DeleteAfterSubmit property of Outlook items is a Boolean value that is True if a copy of the mail message is not saved upon being sent, and False if a copy is saved in Sent Items folder. So, you will never get the event fired if users or other software like VBA macros or add-ins set this property before sending emails.
A better way is to handle the ItemSend event of the Application which is fired when a new item is received in the Inbox.
This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem , MeetingItem , or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Note that this behavior has changed from earlier versions of the event when the EntryIDCollection contained a list of comma-delimited Entry IDs of all the items received in the Inbox since the last time the event was fired.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance. However, depending on the setup on the client computer, after a new message arrives in the Inbox, processes like spam filtering and client rules that move the new message from the Inbox to another folder can occur asynchronously. You should not assume that after these events fire, you will always get a one-item increase in the number of items in the Inbox.
In the NewMailEx event handler you may ask users whether to process emails after sending and, if it is not, you can simply set the DeleteAfterSubmit property to true.

Related

VSTO Outlook: Catching Send Event

I am trying to create plug-in that will catch any send item and show warning before sent out.
Currently I am using the below code
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Application application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
However, I noticed it catches almost all the send event but when send button is pressed from main screen of Outlook, I mean when the mail is not popped out to separate window, it does not catch the event and my plugin function is not executed. My environment is Visual Studio 2019 and Outlook 2019. Any advise is appreciated.
Thank you,
Catch all the send event from Outlook.
Update 11/7/2022: it turned out that the event is caught but
I am tryning to catch the item type by the below method and inline item does not detected as an item.
itemtype = Application.ActiveWindow().CurrentItem.Class;
I can confirm that the ItemSend event is fired for all outgoing emails in Outlook, including inline responses. To make sure I've just created a sample VSTO add-in and placed your code there. Everything works correctly when I reply to items in-place. Try to declare the Application object at the class level. But I think everything should work as is now.
Outlook.Application application = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
application = Globals.ThisAddIn.Application;
application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
You may consider subscribing to the Explorer.InlineResponse event which is fired when the user performs an action that causes an inline response to appear in the Reading Pane. So, you may get the item composed and do whatever you need (subscribe to the Send event).
Be aware, a low level API such as Extended or Simple MAPI doesn't trigger the ItemSend event in Outlook - only actions made in the Outlook object model or made manually can trigger the event.
It should work in all cases, but Outlook disables most OOM events for the items created using Simple MAPI or from the "mailto:" links. Is that the case?

Outlook AddIn: showing popup while dragging mail in Outlook

I have an outlook addin which shows a popup after sending mail which asks the user whether he/she wants to save the mail.
The problem is that this popup is shown when a mail is added to the sent folder.
So when you send a mail and immediately start dragging a mail to another folder because, e.g. you want to clean up mails, then the popup shows when you're in the middle of dragging which stops the dragging and makes the popup with the question unresponsive to the mouse.
Is there any way around this?
Thanks in advance!
The event for an mail being added to sent folder is set in the Ribbon Load method:
private void CodexRibbon_Load(object sender, RibbonUIEventArgs e)
{
if (!Globals.ThisAddIn.FirstLoadComplete)
{
Messenger.Default.Register<object>(this, ItemSend);
Globals.ThisAddIn.FirstLoadComplete = true;
}
}
The question is asked as follows:
private void ItemSend(object item)
{
if (MessageBox.Show(#" Wilt u de zojuist verzonden e-mail opslaan?", #"Opslaan in Codex", MessageBoxButtons.YesNo) == DialogResult.Yes)
ShowSaveWindow(item);
}
I don't think there is a way to find out if the control is inside the DoDragDrop Windows API function (you can patch it and have the patched version set some kind of a global flag before entering the original function, but I don't think that will be a good idea in .Net).
Try to display the message box in the Application.ItemSendevent handler, mark the message somehow (user property?), then do the processing in Items.ItemAdd without a message box.

is it possible capture sending email status outlook add-in c#

I have ApplicationEvents_11_ItemSendEventHandler email sending event, but this event in email sending status is always false, so any event capture email sending is true or false.
Do you mean MailItem.Sent is still false? This is to be expected. The very first time you can access the sent message is when Items.ItemAdd event fires on the Sent Items folder. By then Sent property will be true and all the sender related properties will be populated.

How to catch email deletion on the main inbox explorer?

I'm having problems catching the deletion event for emails that are deleted on the inbox explorer. None of the events I've tried catch this. MailItem.BeforeDelete only triggers if the email is deleted using the inspectors delete button, which is fine but it only catches a portion of deletions. Explorer.BeforeItemCut only triggers when the user uses Ctrl+X. Explorer.BeforeMove doesn't trigger either, I thought that may "deletion" was really just moving the email to the Deleted Items folder.
I thought of maybe catching when an email arrives in the Deleted Items folder, but MAPIFolder items don't seem to have events. So, I'm not sure where else to look.
Just to make sure, by deleting an email I mean when the user hits the red X below.
You can catch the Items.ItemAdd event on the Deleted Items folder's Items collection.
It will not of course fire in case of Shift+Delete.

mailItem.PropertyChange stops firing

I am implementing Custom Task Panes with E-Mail Messages in Outlook.
The core is taken from this link MSDN (Walkthrough: Displaying Custom Task Panes with E-Mail Messages in Outlook)
Handler for property change is added:
void TaskPane_VisibleChanged(object sender, EventArgs e)
{
Globals.Ribbons[inspector].ManageTaskPaneRibbon
.toggleButton1.Checked = taskPane.Visible;
...some code here...
mailItem.PropertyChange += PropertyChangeHandler;
}
PropertyChangeHandler checks is recipients have changed and does some heavy routine with posts and so on. But... If I add 10 recipients and start to remove them with backspace PropertyChangeHandler stops firing at some point.
No errors. Buttons on custom task pane work fine.
What is wrong?
Seems that either event is eaten or inspector is incorrect, but I cannot spot problem and find the solution.
I also think that it might be about "heavy load" when next event is fired before previous is completed, but this is a guess
You need to call the Save method or save the message explicitly to make the PropertyChange event fired. Outlook caches values in the UI and doesn't propagate changes until the item is saved.
Also I'd suggest creating a log file (a regular text file) where you can write the debug statements. Thus, you will understand what happens in the code.

Categories