// RopModifyRules RopGetRulesTable public void CreateNewRule() { Outlook.AddressEntry currentUser = oApp.Session.CurrentUser.AddressEntry; Outlook.ExchangeUser manager = currentUser.GetExchangeUser(); Outlook.Rules rules = oApp.Session.DefaultStore.GetRules(); if (manager != null) { string displayName = manager.Name; int num = rules.Count; Outlook.Rule rule = rules.Create(displayName + "_" + num, Outlook.OlRuleType.olRuleReceive); // Rule conditions: From condition rule.Conditions.From.Recipients.Add(manager.PrimarySmtpAddress); rule.Conditions.From.Recipients.ResolveAll(); rule.Conditions.From.Enabled = true; // Sent only to me rule.Conditions.ToMe.Enabled = true; // Rule actions: MarkAsTask action rule.Actions.MarkAsTask.MarkInterval = Outlook.OlMarkInterval.olMarkToday; rule.Actions.MarkAsTask.FlagTo = "Follow-up"; rule.Actions.MarkAsTask.Enabled = true; try { rules.Save(true); } catch (Exception e) { throw new Exception(e.Message); } bool result = MessageParser.ParseMessage(); Assert.IsTrue(result, "Case failed, check the details information in error.txt file."); } }
public static bool RuleExist(string ruleName, Outlook.Rules rules) { foreach (Outlook.Rule rule in rules) { if (rule.Name == ruleName) { return(true); } } return(false); }
private void DisableAddIn() { Application.NewMailEx -= Application_NewMailEx; sentboxItems.ItemAdd -= Items_ItemAdd; Application.AdvancedSearchComplete -= Application_AdvancedSearchComplete; inbox = null; sentboxItems = null; sentbox = null; rules = null; searchFolders = null; defaultNamespace = null; }
private void EnableAddIn() { defaultNamespace = Application.GetNamespace("MAPI"); rules = defaultNamespace.DefaultStore.GetRules(); inbox = defaultNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); sentbox = defaultNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); sentboxItems = sentbox.Items; searchFolders = defaultNamespace.DefaultStore.GetSearchFolders(); Application.NewMailEx += Application_NewMailEx; sentboxItems.ItemAdd += Items_ItemAdd; Application.AdvancedSearchComplete += Application_AdvancedSearchComplete; }
private bool RuleExists(Outlook.Rules ruleSet, string ruleName) { Outlook.Rule rule; try { rule = ruleSet[ruleName]; return(true); // rule exists } catch { // rule does not exist } return(false); }
private Outlook.Rule CreateOutgoingRule(Outlook.Rules ruleSet, string ruleName, string domain, Outlook.MAPIFolder folder, Outlook.MailItem mailItem) { Outlook.Rule rule = ruleSet.Create(ruleName, Outlook.OlRuleType.olRuleSend); // Rule Conditions // To condition rule.Conditions.RecipientAddress.Address = new string[] { "@" + domain }; rule.Conditions.RecipientAddress.Enabled = true; // Rule Exceptions // nothing yet // Rule Actions rule.Actions.CopyToFolder.Folder = folder; rule.Actions.CopyToFolder.Enabled = true; rule.Enabled = true; return(rule); }
private void buttonCreateRule_Click(object sender, EventArgs e) { string folderName = "Test", folderFolderName = "Inbox"; try { Outlook.MailItem mailItem = null; Outlook.NameSpace nameSpace = null; Outlook.Rules rules = null; Outlook.Rule rule = null; Outlook.MAPIFolder defaultFolder = null, parentFolder = null, tmpFolder = null, destFolder = null; try { if ((mailItem = OutlookItem as Outlook.MailItem) != null && (nameSpace = mailItem.Application.GetNamespace("MAPI")) != null && (rules = mailItem.Application.Session.DefaultStore.GetRules()) != null && (defaultFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)) != null && (parentFolder = defaultFolder.Parent as Outlook.MAPIFolder) != null && (tmpFolder = GetFolder(parentFolder, folderName)) != null && (destFolder = GetFolder(tmpFolder, folderFolderName)) != null) { foreach (Outlook.Rule r in rules) { string tmpString = string.Format("{0} {1} {2} {3} {4}", r.Name, r.Conditions.From.Recipients.Count > 0 ? r.Conditions.From.Recipients[1].Name : "!From.Recipients", r.Conditions.SentTo.Recipients.Count > 0 ? r.Conditions.SentTo.Recipients[1].Name : "!SendTo.Recipients", r.Actions.MoveToFolder.Folder != null ? r.Actions.MoveToFolder.Folder.FullFolderPath : "!MoveToFolder", r.Actions.CopyToFolder.Folder != null ? r.Actions.CopyToFolder.Folder.FullFolderPath : "!CopyToFolder"); } rule = rules.Create("TestRule", Outlook.OlRuleType.olRuleReceive); rule.Conditions.From.Enabled = true; rule.Conditions.From.Recipients.Add("*****@*****.**"); //rule.Conditions.From.Recipients.ResolveAll(); rule.Actions.MoveToFolder.Enabled = true; rule.Actions.MoveToFolder.Folder = destFolder; rule.Actions.DesktopAlert.Enabled = true; rules.Save(false); textBoxLog.Text = "Done"; } } finally { if (destFolder != null) { Marshal.ReleaseComObject(destFolder); destFolder = null; } if (tmpFolder != null) { Marshal.ReleaseComObject(tmpFolder); tmpFolder = null; } if (parentFolder != null) { Marshal.ReleaseComObject(parentFolder); parentFolder = null; } if (defaultFolder != null) { Marshal.ReleaseComObject(defaultFolder); defaultFolder = null; } if (rule != null) { Marshal.ReleaseComObject(rule); rule = null; } if (rules != null) { Marshal.ReleaseComObject(rules); rules = null; } if (nameSpace != null) { Marshal.ReleaseComObject(nameSpace); nameSpace = null; } if (mailItem != null) { Marshal.ReleaseComObject(mailItem); mailItem = null; } } } catch (Exception eException) { string msg; ThisAddIn.WriteToLog(msg = eException.GetType().FullName + Environment.NewLine + "Message: " + eException.Message + Environment.NewLine + (eException.InnerException != null && !string.IsNullOrEmpty(eException.InnerException.Message) ? "InnerException.Message" + eException.InnerException.Message + Environment.NewLine : string.Empty) + "StackTrace:" + Environment.NewLine + eException.StackTrace); textBoxLog.Text = msg; } }
void btnCheckForRule_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) { Outlook.MailItem sitem = Application.ActiveExplorer().Selection[1]; //Load selected mail item Outlook.Rules TheRules = Application.Session.DefaultStore.GetRules(); //Retrieve Rules string TheEmail = sitem.SenderEmailAddress; string TheName = sitem.SenderName; string TheDisplayName = sitem.SenderName; int recipientindex = 1; int deleteADRindex = 0; bool deleteADRrecipient = false; int deleteNAMEindex = 0; bool deleteNAMErecipient = false; bool NoMatchFound = true; int RecipientCount = 0; foreach (Outlook.Rule RL in TheRules) { recipientindex = 1; RecipientCount = RL.Conditions.From.Recipients.Count; foreach (Outlook.Recipient s in RL.Conditions.From.Recipients)//Check by sender address or name { if (s.Name == TheName) { DialogResult Namefound; Namefound = System.Windows.Forms.MessageBox.Show(TheName + @" exist in Rule <" + RL.Name + @">" + Environment.NewLine + "Remove from Rule?", "Rule Found", System.Windows.Forms.MessageBoxButtons.YesNo); NoMatchFound = false; if (Namefound == DialogResult.Yes) { deleteNAMErecipient = true; deleteNAMEindex = recipientindex; } } if (s.Address == TheEmail)//skip if already being removed based on name { DialogResult ADRfound; ADRfound = System.Windows.Forms.MessageBox.Show(TheEmail + @" exist in Rule <" + RL.Name + @">" + Environment.NewLine + "Remove from Rule?", "Rule Found", System.Windows.Forms.MessageBoxButtons.YesNo); NoMatchFound = false; if (ADRfound == DialogResult.Yes) { deleteADRrecipient = true; deleteADRindex = recipientindex; } } recipientindex++; } if (deleteADRrecipient) { deleteADRindex = deleteADRindex - (RecipientCount - RL.Conditions.From.Recipients.Count);//Adjust index to account for any deletions made to Recipient list after index was set RL.Conditions.From.Recipients.Remove(deleteADRindex); RL.Conditions.From.Recipients.GetEnumerator(); deleteADRrecipient = false; } if (deleteNAMErecipient) { deleteNAMEindex = deleteNAMEindex - (RecipientCount - RL.Conditions.From.Recipients.Count);//Adjust index to account for any deletions made to Recipient list after index was set RL.Conditions.From.Recipients.Remove(deleteNAMEindex); RL.Conditions.From.Recipients.GetEnumerator(); deleteNAMErecipient = false; } } if (NoMatchFound) { System.Windows.Forms.MessageBox.Show("Not in any Rules", "Rule Check", System.Windows.Forms.MessageBoxButtons.OK); } else { TheRules.Save(false); } }
void btnRules_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault) { int rulecount = 1; int ruleindex = 1; string RuleName = Ctrl.Caption.Substring(10);//Load selected Rule Name from button caption bool AlreadyThere = false; Outlook.MailItem citem = Application.ActiveExplorer().Selection[1]; //Load selected mail item Outlook.Rules MyRules = Application.Session.DefaultStore.GetRules(); //Retrieve Rules Outlook.Folder Folder = Application.ActiveExplorer().CurrentFolder //Retrieve Folder as Outlook.Folder; string Email = citem.SenderEmailAddress; //Extract Selected Mail Item Email Address foreach (Outlook.Rule RL in MyRules) { if (RuleName == RL.Name) { ruleindex = rulecount;//Assign indext to selected Rule } rulecount++; } foreach (Outlook.RuleCondition RC in MyRules[ruleindex].Conditions) { if (RC.Enabled) //Add selected item parts to respective conditions if condition enabled { switch (RC.ConditionType) //When I put this in as switch condition vs automatically added all the case statements below! { case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionAccount: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionAnyCategory: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionBody: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionBodyOrSubject: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionCategory: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionCc: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionDateRange: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionFlaggedForAction: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionFormName: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionFrom: foreach (Outlook.Recipient ite in MyRules[ruleindex].Conditions.From.Recipients) { string rec = ite.Address; string name = ite.Name; if (rec == item.SenderEmailAddress || name == item.SenderName) { AlreadyThere = true; } } if (!AlreadyThere) { MyRules[ruleindex].Conditions.From.Recipients.Add(item.SenderEmailAddress); MyRules[ruleindex].Conditions.From.Recipients.Add(item.SenderName); MyRules[ruleindex].Conditions.From.Recipients.GetEnumerator(); //This here thingy keeps the new address and rule condition from returning void and hince being added multiple times and not executing when rule is run. Otherwise even though the new condition recipient shows up in the wizard, but has no effect when wizard is run. } else { System.Windows.Forms.MessageBox.Show(item.SenderEmailAddress + @" already in sender list!", "Error Adding To Rule", System.Windows.Forms.MessageBoxButtons.OK); } break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionFromAnyRssFeed: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionFromRssFeed: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionHasAttachment: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionImportance: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionLocalMachineOnly: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionMeetingInviteOrUpdate: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionMessageHeader: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionNotTo: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionOOF: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionOnlyToMe: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionOtherMachine: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionProperty: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionRecipientAddress: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSenderAddress: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSenderInAddressBook: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSensitivity: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSentTo: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSizeRange: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionSubject: //Next to do######################################### break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionTo: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionToOrCc: break; case Microsoft.Office.Interop.Outlook.OlRuleConditionType.olConditionUnknown: break; default: break; } } } MyRules.Save(false); MyRules[ruleindex].Execute(true, Folder, Type.Missing, Type.Missing); }
private void AfterSendUnsafe(object Item) { if (!CurrentSettings.AddInEnabled || CurrentSettings.OutgoingFirstAction == OutgoingFirstAction.DoNothing) { return; } Outlook.MailItem mailItem = Item as Outlook.MailItem; var domains = new HashSet <string>(); foreach (Outlook.Recipient recipient in mailItem.Recipients) { string recipientAddress; if (!TryGetRecipientAddress(recipient, out recipientAddress)) { continue; } string domain; if (!TryGetDomain(recipientAddress, out domain)) { continue; } if (!domains.Contains(domain)) { domains.Add(domain); } } var domainsAllowed = domains.Except(CurrentSettings.OutgoingExceptions); Outlook.Rules ruleSet = null; var needsSave = false; if (domainsAllowed.Count() > 0 && CurrentSettings.OutgoingFirstAction == OutgoingFirstAction.CreateSentFolderRule) { RefreshRules(); ruleSet = RuleSet; } foreach (var domain in domainsAllowed) { var ruleName = OutgoingRulePrefix + domain; var initChar = char.ToUpper(domain.Take(1).First()); var folderName = SentboxFolderNamePrefix + initChar.ToString() + domain.Substring(1); Outlook.MAPIFolder parentFolder = null; if (CurrentSettings.OutgoingCreateParentFolders) { var parentFolderName = GetParentFolderName(initChar); if (!TryGetFolder(parentFolderName, Sentbox, out parentFolder)) { parentFolder = CreateFolder(parentFolderName, Sentbox); } } else { parentFolder = Sentbox; } Outlook.MAPIFolder folder = null; if (!TryGetFolder(folderName, parentFolder, out folder)) { folder = CreateFolder(folderName, parentFolder); } try { var copy = mailItem.Copy() as Outlook.MailItem; copy.Move(folder); } catch { // tried to move if it does not happen ignore } if (CurrentSettings.OutgoingFirstAction == OutgoingFirstAction.CreateSentFolderRule) { if (!RuleExists(ruleSet, ruleName)) { CreateOutgoingRule(ruleSet, ruleName, domain, folder, mailItem); needsSave = true; } } } if (needsSave && ruleSet != null) { ruleSet.Save(false); } }
private void RefreshRules() { rules = defaultNamespace.DefaultStore.GetRules(); }
public static void suggestRulesBasedOnCountOfSender(Dictionary <string, int> dictionary) { Console.WriteLine("hi"); Outlook.NameSpace session = null; Outlook.Store store = null; Outlook.Rules rules = null; Outlook.MAPIFolder destinationFolder = null; Outlook.MAPIFolder rootFolder = null; Outlook.Folders rootFolderFolders = null; Outlook.Rule rule = null; Outlook.RuleConditions ruleConditions = null; Outlook.TextRuleCondition subjectTextRuleCondition = null; Outlook.RuleActions ruleActions = null; Outlook.MoveOrCopyRuleAction moveRuleAction = null; string ruleName = string.Empty; try { ruleName = "Move Order Mails Rule"; //session = Application.Session; // have to correct this error, it was working in 10102017 build when everything was in ThisAddin.cs store = session.DefaultStore; rules = store.GetRules(); if (!RuleExist(ruleName, rules)) { rootFolder = store.GetRootFolder(); // destinationFolder = GetFolder(rootFolder.FolderPath + "\\Orders", this); if (destinationFolder == null) { rootFolderFolders = rootFolder.Folders; destinationFolder = rootFolderFolders.Add("Orders"); } rule = rules.Create(ruleName, Outlook.OlRuleType.olRuleReceive); ruleConditions = rule.Conditions; subjectTextRuleCondition = ruleConditions.Subject; subjectTextRuleCondition.Text = new string[] { "Orders", "orders", "Order", "order" }; subjectTextRuleCondition.Enabled = true; ruleActions = rule.Actions; moveRuleAction = ruleActions.MoveToFolder; moveRuleAction.Folder = destinationFolder; moveRuleAction.Enabled = true; ruleActions.DesktopAlert.Enabled = true; rules.Save(true); } } catch (System.Exception ex) { System.Diagnostics.Debug.Write(ex.Message); } }
// This sets an existing Outlook email rule to look for SR Numbers provided public bool SetEmailRules(Object[] textCondition) { try { Outlook.Application oApp = new Outlook.Application(); Outlook.NameSpace oNamespace = oApp.GetNamespace("MAPI"); Outlook.MAPIFolder vEmailFolder = null; Outlook.MAPIFolder CaseFolder = null; Outlook.Rules rules = null; Outlook.Rule moveRule = null; foreach (Outlook.MAPIFolder folder in oNamespace.Folders) { if (folder.Name.Contains("@microsoft.com") && !folder.Name.Contains("Public")) { vEmailFolder = folder; Debug.WriteLine("Initializing master inbox as : " + folder.Name); } } foreach (Outlook.MAPIFolder folder in vEmailFolder.Folders) { if (folder.Name.Contains("Case")) { CaseFolder = folder; Debug.WriteLine("Initializing slave inbox as : " + folder.Name); } } rules = oApp.Session.DefaultStore.GetRules(); foreach (Outlook.Rule r in rules) { if (r.Name.Contains("Case")) { moveRule = r; } } if (moveRule == null) { moveRule = rules.Create("Cases", Outlook.OlRuleType.olRuleReceive); } moveRule.Conditions.BodyOrSubject.Text = textCondition; moveRule.Conditions.BodyOrSubject.Enabled = true; try { rules.Save(); return(true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(false); } } catch (Exception e) { MessageBox.Show(e.ToString()); return(false); } }
private void AddRules() { Common.WriteToDebugWindow("AddRules"); Outlook.Folders sessionFolders = Globals.ThisAddIn.Application.Session.Folders; Outlook.Folder inbox = (Outlook.Folder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); Outlook.Folders inboxFolders = inbox.Folders; Outlook.Folder junkFolder = (Outlook.Folder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk); //Outlook.Folders advertisementFolders = Globals.ThisAddIn.Application.Session.GetFolderFromID("Advertisements").Folders; Outlook.Folder advertisementsFolder = (Outlook.Folder)sessionFolders["crhodes"].Folders["Advertisements"]; Outlook.Folder edvantageFolder = (Outlook.Folder)sessionFolders["crhodes"].Folders["EDVantage"]; foreach (Outlook.Folder folder in sessionFolders) { Common.WriteToDebugWindow(folder.Name); } Outlook.Folder victoriaFolder; try { victoriaFolder = (Outlook.Folder)inboxFolders["Victoria Secret"]; } catch { victoriaFolder = (Outlook.Folder)inboxFolders.Add("Victoria Secret"); } Outlook.AddressEntry currentUser = Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry; Outlook.Rules currentRules = Globals.ThisAddIn.Application.Session.DefaultStore.GetRules(); Outlook.Rule victoriaRule; Outlook.Rule advertisementsRule; //try //{ // victoriaRule = currentRules["Victoria Secret"]; //} //catch (Exception ex) //{ //} victoriaRule = currentRules.Create("Victoria Secret", Outlook.OlRuleType.olRuleReceive); string[] victoriaAddress = { "*****@*****.**" }; victoriaRule.Conditions.SenderAddress.Address = victoriaAddress; victoriaRule.Conditions.SenderAddress.Enabled = true; victoriaRule.Actions.MoveToFolder.Folder = victoriaFolder; victoriaRule.Actions.MoveToFolder.Enabled = true; advertisementsRule = currentRules.Create("Advertisements", Outlook.OlRuleType.olRuleReceive); string[] advertisersAddresses = { "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" , "*****@*****.**" }; advertisementsRule.Conditions.SenderAddress.Address = advertisersAddresses; advertisementsRule.Conditions.SenderAddress.Enabled = true; advertisementsRule.Actions.MoveToFolder.Folder = advertisementsFolder; advertisementsRule.Actions.MoveToFolder.Enabled = true; try { currentRules.Save(); } catch (Exception ex) { Common.WriteToDebugWindow(ex.ToString()); } }