public MainWindow() { InitializeComponent(); _outlook = new Outlook.Application(); // TODO: is it ok to instantiate this once? _currentUser = _outlook.Application.Session.CurrentUser.AddressEntry; txtAlias.Focus(); }
public AddressEntry(object addressEntry) { m_oif = new OutlookIImplFactory(); IWSAddressEntry rae = addressEntry as IWSAddressEntry; if (rae != null) { m_redemptionAddressEntry = rae; } else { m_outlookAddressEntry = addressEntry as MSOutlook.AddressEntry; } }
bool IRepositoryItemFactory.Send_Outlook(bool actualSend, string MailTo, string Event, string Subject, string Body, string MailCC, List <string> Attachments, List <KeyValuePair <string, string> > EmbededAttachment) { try { Outlook.Application objOutLook = null; if (string.IsNullOrEmpty(MailTo)) { Event = "Failed: Please provide TO email address."; return(false); } if (string.IsNullOrEmpty(Subject)) { Event = "Failed: Please provide email subject."; return(false); } // Check whether there is an Outlook process running. if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0) { // If so, use the GetActiveObject method to obtain the process and cast it to an ApplicatioInstall-Package Microsoft.Office.Interop.Exceln object. objOutLook = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; } else { // If not, create a new instance of Outlook and log on to the default profile. objOutLook = new Outlook.Application(); Outlook.NameSpace nameSpace = objOutLook.GetNamespace("MAPI"); nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value); nameSpace = null; } mOutlookMail = objOutLook.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; mOutlookMail.HTMLBody = Body; mOutlookMail.Subject = Subject; Outlook.AddressEntry currentUser = objOutLook.Session.CurrentUser.AddressEntry; if (currentUser.Type == "EX") { Outlook.ExchangeUser manager = currentUser.GetExchangeUser(); // Add recipient using display name, alias, or smtp address string emails = MailTo; Array arrEmails = emails.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string email in arrEmails) { mOutlookMail.Recipients.Add(email); } //Add CC if (!String.IsNullOrEmpty(MailCC)) { Array arrCCEmails = MailCC.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string MailCC1 in arrCCEmails) { mOutlookMail.Recipients.Add(MailCC1); } } mOutlookMail.Recipients.ResolveAll(); mOutlookMail.CC = MailCC; mOutlookMail.To = MailTo; //Add Attachment foreach (string AttachmentFileName in Attachments) { if (String.IsNullOrEmpty(AttachmentFileName) == false) { mOutlookMail.Attachments.Add(AttachmentFileName, Type.Missing, Type.Missing, Type.Missing); } } //attachment which is embeded into the email body(images). foreach (KeyValuePair <string, string> AttachmentFileName in EmbededAttachment) { if (String.IsNullOrEmpty(AttachmentFileName.Key) == false) { if (System.IO.File.Exists(AttachmentFileName.Key)) { Outlook.Attachment attachment = mOutlookMail.Attachments.Add(AttachmentFileName.Key, Outlook.OlAttachmentType.olEmbeddeditem, null, ""); attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", AttachmentFileName.Value); } } } if (actualSend) { //Send Mail mOutlookMail.Send(); } } return(true); } catch (Exception ex) { if (ex.Message.Contains("Mailbox Unavailabel")) { Event = "Failed: Please provide correct sender email address"; } else if (ex.StackTrace.Contains("System.Runtime.InteropServices.Marshal.GetActiveObject")) { Event = "Please make sure ginger/outlook opened in same security context (Run as administrator or normal user)"; } else if (ex.StackTrace.Contains("System.Security.Authentication.AuthenticationException") || ex.StackTrace.Contains("System.Net.Sockets.SocketException")) { Event = "Please check SSL configuration"; } else { Event = "Failed: " + ex.Message; } return(false); } }
// OnMyButtonClick routine handles all button click events // and displays IRibbonControl.Context in message box public void OnMyButtonClick(Office.IRibbonControl control) { string msg = string.Empty; if (control.Context is Outlook.AttachmentSelection) { msg = "Context=AttachmentSelection" + "\n"; Outlook.AttachmentSelection attachSel = control.Context as Outlook.AttachmentSelection; foreach (Outlook.Attachment attach in attachSel) { msg = msg + attach.DisplayName + "\n"; } } else if (control.Context is Outlook.Folder) { msg = "Context=Folder" + "\n"; Outlook.Folder folder = control.Context as Outlook.Folder; msg = msg + folder.Name; } else if (control.Context is Outlook.Selection) { msg = "Context=Selection" + "\n"; Outlook.Selection selection = control.Context as Outlook.Selection; if (selection.Count == 1) { OutlookItem olItem = new OutlookItem(selection[1]); msg = msg + olItem.Subject + "\n" + olItem.LastModificationTime; } else { msg = msg + "Multiple Selection Count=" + selection.Count; } } else if (control.Context is Outlook.OutlookBarShortcut) { msg = "Context=OutlookBarShortcut" + "\n"; Outlook.OutlookBarShortcut shortcut = control.Context as Outlook.OutlookBarShortcut; msg = msg + shortcut.Name; } else if (control.Context is Outlook.Store) { msg = "Context=Store" + "\n"; Outlook.Store store = control.Context as Outlook.Store; msg = msg + store.DisplayName; } else if (control.Context is Outlook.View) { msg = "Context=View" + "\n"; Outlook.View view = control.Context as Outlook.View; msg = msg + view.Name; } else if (control.Context is Outlook.Inspector) { msg = "Context=Inspector" + "\n"; Outlook.Inspector insp = control.Context as Outlook.Inspector; if (insp.AttachmentSelection.Count >= 1) { Outlook.AttachmentSelection attachSel = insp.AttachmentSelection; foreach (Outlook.Attachment attach in attachSel) { msg = msg + attach.DisplayName + "\n"; } } else { OutlookItem olItem = new OutlookItem(insp.CurrentItem); msg = msg + olItem.Subject; } } else if (control.Context is Outlook.Explorer) { msg = "Context=Explorer" + "\n"; Outlook.Explorer explorer = control.Context as Outlook.Explorer; if (explorer.AttachmentSelection.Count >= 1) { Outlook.AttachmentSelection attachSel = explorer.AttachmentSelection; foreach (Outlook.Attachment attach in attachSel) { msg = msg + attach.DisplayName + "\n"; } } else { Outlook.Selection selection = explorer.Selection; if (selection.Count == 1) { OutlookItem olItem = new OutlookItem(selection[1]); msg = msg + olItem.Subject + "\n" + olItem.LastModificationTime; } else { msg = msg + "Multiple Selection Count=" + selection.Count; } } } else if (control.Context is Outlook.NavigationGroup) { msg = "Context=NavigationGroup" + "\n"; Outlook.NavigationGroup navGroup = control.Context as Outlook.NavigationGroup; msg = msg + navGroup.Name; } else if (control.Context is Microsoft.Office.Core.IMsoContactCard) { msg = "Context=IMsoContactCard" + "\n"; Office.IMsoContactCard card = control.Context as Office.IMsoContactCard; if (card.AddressType == Office.MsoContactCardAddressType. msoContactCardAddressTypeOutlook) { // IMSOContactCard.Address is AddressEntry.ID Outlook.AddressEntry addr = Globals.ThisAddIn.Application.Session.GetAddressEntryFromID( card.Address); if (addr != null) { msg = msg + addr.Name; } } } else if (control.Context is Outlook.NavigationModule) { msg = "Context=NavigationModule"; } else if (control.Context == null) { msg = "Context=Null"; } else { msg = "Context=Unknown"; } MessageBox.Show(msg, "RibbonXOutlook14AddinCS", MessageBoxButtons.OK, MessageBoxIcon.Information); }
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()); } }
private static string GetOutlookEmailAddress(Outlook.ContactItem outlookContactItem, string emailAddressType, string emailEntryID, string emailAddress) { switch (emailAddressType) { case "EX": // Microsoft Exchange address: "/o=xxxx/ou=xxxx/cn=Recipients/cn=xxxx" Outlook.NameSpace outlookNameSpace = outlookContactItem.Application.GetNamespace("mapi"); try { // The emailEntryID is garbage (bug in Outlook 2007 and before?) - so we cannot do GetAddressEntryFromID(). // Instead we create a temporary recipient and ask Exchange to resolve it, then get the SMTP address from it. //Outlook.AddressEntry addressEntry = outlookNameSpace.GetAddressEntryFromID(emailEntryID); Outlook.Recipient recipient = outlookNameSpace.CreateRecipient(emailAddress); try { recipient.Resolve(); if (recipient.Resolved) { Outlook.AddressEntry addressEntry = recipient.AddressEntry; if (addressEntry != null) { try { if (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry) { Outlook.ExchangeUser exchangeUser = addressEntry.GetExchangeUser(); if (exchangeUser != null) { try { return(exchangeUser.PrimarySmtpAddress); } finally { Marshal.ReleaseComObject(exchangeUser); } } } else { Logger.Log(string.Format("Unsupported AddressEntryUserType {0} for contact '{1}'.", addressEntry.AddressEntryUserType, outlookContactItem.FileAs), EventType.Debug); } } finally { Marshal.ReleaseComObject(addressEntry); } } } } finally { if (recipient != null) { Marshal.ReleaseComObject(recipient); } } } catch (Exception ex) { // Fallback: If Exchange cannot give us the SMTP address, we give up and use the Exchange address format. // TODO: Can we do better? Logger.Log(string.Format("Error getting the email address of outlook contact '{0}' from Exchange format '{1}': {2}", outlookContactItem.FileAs, emailAddress, ex.Message), EventType.Warning); return(emailAddress); } finally { if (outlookNameSpace != null) { Marshal.ReleaseComObject(outlookNameSpace); } } // Fallback: If Exchange cannot give us the SMTP address, we give up and use the Exchange address format. // TODO: Can we do better? return(emailAddress); case "SMTP": default: return(emailAddress); } }