public FolderType CreateFolder <FolderType>(string name) where FolderType : IFolder { using (ComRelease com = new ComRelease()) { NSOutlook.Folders folders = com.Add(_item.Folders); if (typeof(FolderType) == typeof(IFolder)) { return(folders.Add(name).Wrap <FolderType>()); } else if (typeof(FolderType) == typeof(IAddressBook)) { NSOutlook.MAPIFolder newFolder = folders.Add(name, NSOutlook.OlDefaultFolders.olFolderContacts); newFolder.ShowAsOutlookAB = true; return(newFolder.Wrap <FolderType>()); } else { throw new NotSupportedException(); } } }
/// <summary> /// Method for Creating new folders and subfolders inside Inbox for each Account /// </summary> /// <param name="foldersToCreate">List of Folders that should be created inside Inbox. Each created folder will be created with one default Subfolder</param> /// <returns></returns> /// public static void CreateFolders(List <String> foldersToCreate, string account) { Outlook.Application application = new Outlook.Application(); Outlook.Accounts accounts = application.Session.Accounts; foreach (Outlook.Account acc in accounts) { Console.WriteLine("Account Display name: " + acc.DisplayName); if (acc.DisplayName.Equals(account)) { NameSpace oNs = application.GetNamespace("MAPI"); Recipient oRep = oNs.CreateRecipient(acc.DisplayName); Outlook.Folder folder = application.Session.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderInbox) as Outlook.Folder; Outlook.Folders folders = folder.Folders; foreach (String fold in foldersToCreate) { try { Outlook.Folder newFolder = folders.Add( fold, Type.Missing) as Outlook.Folder; Outlook.Folder newSubfolder = folders.Add("Subfolder", Type.Missing) as Outlook.Folder; newSubfolder.MoveTo(newFolder); } catch { // } } } } }
public static Outlook.MAPIFolder GetOrCreateFolder(this Outlook.Folders folders, string path) { var pathSeparatorIndex = path.IndexOf("\\", StringComparison.Ordinal); var currentFolderName = pathSeparatorIndex < 0 ? path : path.Substring(0, pathSeparatorIndex); var matchingFolder = folders.OfType <Outlook.MAPIFolder>() .FirstOrDefault(f => f.Name == currentFolderName) ?? folders.Add(currentFolderName); return(pathSeparatorIndex < 0 ? matchingFolder : GetOrCreateFolder(matchingFolder.Folders, path.Substring(pathSeparatorIndex + 1))); }
//in // folder: "Fabrikam" // folders" starting point to create below. // (Outlook.MAPIFolder)Globals.ThisAddIn.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); //out reference to MAPIFolder private void createFolder(string folder, Outlook.MAPIFolder MAPIfolder) { Outlook.Folders folders = MAPIfolder.Folders; if (folders == null) { Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)Globals.ThisAddIn.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); folders = inBox.Folders; } try { folders.Add(folder, Outlook.OlDefaultFolders.olFolderInbox); folderBox.Text = null; } catch (Exception ex) { writeError("Error creating folder"); } }
public static void CreateVCFolder() { Outlook.Folder folder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks) as Outlook.Folder; Outlook.Folders folders = folder.Folders; try { //add our new folder folders.Add(folderName, Outlook.OlDefaultFolders.olFolderTasks); //then select it SetToDoFolder(); //and add our user defined property ToDoFolder.UserDefinedProperties.Add(fileIdName, Outlook.OlUserPropertyType.olText, true); } catch (Exception ex) { XLant.XLtools.LogException("CreateVCFolder", ex.ToString()); MessageBox.Show("Could not add task folder", "Add Folder", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void CreateInboxSubFolder(Outlook.Application OutlookApp) { Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI"); Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderInbox); Outlook.Folders inboxFolders = folderInbox.Folders; Outlook.MAPIFolder subfolderInbox = null; try { subfolderInbox = inboxFolders.Add("InboxSubfolder", Outlook.OlDefaultFolders.olFolderInbox); } catch (COMException exception) { if (exception.ErrorCode == -2147352567) { // Cannot create the folder. MessageBox.Show(exception.Message); } } if (subfolderInbox != null) { Marshal.ReleaseComObject(subfolderInbox); } if (inboxFolders != null) { Marshal.ReleaseComObject(inboxFolders); } if (folderInbox != null) { Marshal.ReleaseComObject(folderInbox); } if (nameSpace != null) { Marshal.ReleaseComObject(nameSpace); } }
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); } }
private static Outlook.Folder AddFolderInternal(Outlook.Folders folders, string folderName) { Outlook.Folder newFolder = folders.Add(folderName) as Outlook.Folder; return(newFolder); }
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()); } }