// 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."); } }
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; } }
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()); } }