// Although NewMailEx event is triggered before outlook rule processing, these 2 run asynchronized, // so the rule processing doesn't guarentee to run "after" the completion of NewMailEx handler. // And NewMailEx event will not be triggered for every new mail if a lot of new mails coming in a short period of time. // // so the most reliable way to process every new mail is to add an outlook email rule to "run a script" for // every new mail. // // put the following function into outlook vba editor, under "TheOutlookSession", and create an email rule // to "run a script" this one. // // Enable "run a script" in Outlook 2013: // Create a DWORD "EnableUnsafeClientMailRules" under // HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Security, and set to 1 // // Public Sub XXX(Item As Outlook.MailItem) // Header = Item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E") // Pos = InStr(Header, "X-Mailer: nodemailer") // If Pos Then // Item.Categories = "No need to popup new mail alarm" // Item.Save // End If // End Sub private void ApplicationNewMailEx(string EntryIDCollection) { Outlook.NameSpace nameSpace = Application.GetNamespace("MAPI"); string[] entryIds = EntryIDCollection.Split(','); for (int i = 0; i < entryIds.Length; ++i) { Outlook.MailItem mailItem = null; try { mailItem = nameSpace.GetItemFromID(entryIds[i]) as Outlook.MailItem; } catch (COMException) { } if (mailItem != null) { FilterEmailUtil.FilterOutUnwantedEmail(mailItem); if (Config.AutoBackupEmailFromMe == true && Util.GetSenderSMTPAddress(mailItem) == Config.MyEmailAddress) { BackupEmailUtil.MarkEmailReadAndClearAllCategories(mailItem); EmailFlagUtil.FlagEmail(mailItem); BackupEmailUtil.BackupEmail(mailItem); } } } }
public void BtnBackupEmail_Click(Office.IRibbonControl control) { Outlook.MailItem mailItem = null; Outlook.Explorer explorer = null; Outlook.Inspector inspector = null; if (ControlIsInInspector(control, ref inspector) == true) { mailItem = inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { if (BackupEmailUtil.IsEmailAlreadyInBackupFolder(mailItem)) { mailItem.Close(Outlook.OlInspectorClose.olSave); } else { BackupEmailUtil.MarkEmailReadAndClearAllCategories(mailItem); EmailFlagUtil.FlagEmail(mailItem); BackupEmailUtil.BackupEmail(mailItem); } } } else if (ControlIsInExplorer(control, ref explorer) == true) { try { // I have to wrap 'explorer.selction' into a try block, // becasue outlook will raise an exception on this line when the first page is 'Outlook Today' Outlook.Selection selection = explorer.Selection; foreach (var selected in selection) { mailItem = selected as Outlook.MailItem; if (mailItem != null) { if (BackupEmailUtil.IsEmailAlreadyInBackupFolder(mailItem) == false) { EmailFlagUtil.FlagEmail(mailItem); } } } foreach (var selected in selection) { mailItem = selected as Outlook.MailItem; if (mailItem != null) { if (BackupEmailUtil.IsEmailAlreadyInBackupFolder(mailItem) == false) { BackupEmailUtil.MarkEmailReadAndClearAllCategories(mailItem); BackupEmailUtil.BackupEmail(mailItem); } } } } catch (COMException ex) { Debug.WriteLine(ex.ToString()); } } }