How to catch email deletion on the main inbox explorer? - c#

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.

Related

How can i get the outlook search folder deleted event?

I need to get an event when delete outlook search folder
current i am using below event but its not fire
void folders_FolderRemove()
{
MessageBox.Show("A folder was removed");
}
It is not clear how you subscribe to the Remove event of the Folders class.
Most probably the source object is swiped by the garbage collector. You need to declare it at the global scope (i.e. class level) to make sure it is alive when the folder is removed.

C# Outlook Remove event when email sent successfully?

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.

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.

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.

Deleting event methods

Accidentally added some event methods in C# from the form builder. If you delete them from the .cs file it throws an error. How do I get rid of them?
There are two parts to subscribing to an event.
You have the event method itself, which you tried deleting.
You have subscriptions to the event method. You can have any number of controls subscribing to a single event method.
If you just delete the event method, then you still have controls subscribed to that event. But it no longer exists, so you get an error.
You can delete the subscription to the event from the designer by right-clicking the event in the properties window and clicking "Reset":
Or you could open the Designer.cs file and delete event subscription from there. For example:
this.richTextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyDown);
As well as deleting them from the code file, you will need to find the control that is referencing those event methods and remove the reference to the method.
You need to go to Form.Designer.cs and remove the red line which is subscription of the event handler.If you see an error screen like this:
Just click the link under the Instances of this error, and remove that line and it should be fine.
Go to the button or element that reference the Event and remove it from there, Desingn - > Right click on element -> Properties - > Events - > Remove from there what you dont need, or do it in the code, you can search in it for the name of the method you deleted.
And the next time just use Ctrl + Z.

Categories