private void button1_Click(object sender, EventArgs e) { try { // Create the Outlook Application Outlook.Application oApp = new Outlook.Application(); // Create NameSpace Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("MAPI"); oNS.Logon(null, null, false, false); // Get DDS Service Desk Calendar Outlook.MAPIFolder oFolder; Outlook.Recipient oRecipient; string calendarName = "*****@*****.**"; // Create a new appointment item Outlook.AppointmentItem oMsg = (Outlook.AppointmentItem)oApp.CreateItem(Outlook.OlItemType.olAppointmentItem); // Set Recipients oRecipient = oNS.CreateRecipient(calendarName); oRecipient.Resolve(); oFolder = oNS.GetSharedDefaultFolder(oRecipient, Outlook.OlDefaultFolders.olFolderCalendar); // Set Appointment Start Time and End Time oMsg = oFolder.Items.Add("IPM.Appointment") as Outlook.AppointmentItem; oMsg.Start = DateTime.Parse(dateTimePicker1.Text + " " + comboBox1.Text); oMsg.End = DateTime.Parse(dateTimePicker2.Text + " " + comboBox2.Text); //Set the Body string message = "The Below Is Information Regarding The IT Equipment Reservation" + Environment.NewLine; oMsg.Body = "The Below Is Information Regarding The IT Equipment Reservation" + Environment.NewLine + Environment.NewLine + "Requester Information:" + Environment.NewLine + "Email:" + " " + textBox1.Text + Environment.NewLine + "First Name:" + " " + textBox2.Text + Environment.NewLine + "Last Name:" + " " + textBox3.Text + Environment.NewLine + Environment.NewLine + "Reservation Information:" + Environment.NewLine + "Pick-Up Time:" + " " + comboBox1.Text + " " + "to" + " " + comboBox2.Text + Environment.NewLine + "Pick-Up Date:" + " " + dateTimePicker1.Text + " " + "to" + " " + dateTimePicker2.Text + Environment.NewLine + "Service Request Number:" + " " + textBox8.Text + Environment.NewLine + Environment.NewLine + "Service Request Link:" + " " + "http://oasis.dds.ca.gov/applications/ISDRequests_Staff/index.cfm?pageAction=ShowDetail&RequestNum=" + textBox8.Text + Environment.NewLine + "Reserved By Technician:" + Environment.NewLine + "Email:" + " " + textBox4.Text + Environment.NewLine + "First Name:" + " " + textBox5.Text + Environment.NewLine + "Last Name:" + " " + textBox6.Text + Environment.NewLine + "Telephone Number:" + " " + textBox7.Text + Environment.NewLine + Environment.NewLine + "Comments" + Environment.NewLine + richTextBox1.Text; //Set the Subject oMsg.Subject = "SR #" + " " + textBox8.Text + " " + textBox2.Text + " " + textBox3.Text + "-" + " Pick Up" + " " + textBox9.Text; //Set the location oMsg.Location = "DDS Service Desk -- HQ"; //save the appointment oMsg.Save(); //Show Confirmation Message MessageBox.Show("Successful"); } catch (System.Exception ex) { } }
/// <summary> /// A lot of code to monitor the outlook inbox and fire an e-vent for new e-mails in the inbox /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ThisAddIn_Startup(object sender, EventArgs e) { outlookNameSpace = Application.GetNamespace("MAPI"); recipient = outlookNameSpace.CreateRecipient("SFEng"); recipient.Resolve(); if (recipient.Resolved) { sharedInbox = outlookNameSpace.GetSharedDefaultFolder(recipient, Outlook.OlDefaultFolders.olFolderInbox); sharedItems = sharedInbox.Items; sharedItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(SharedItems_ItemAdd); } inbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox); items = inbox.Items; items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd); //Event that fires from new e-mails into the inbox }
public IRecipient ResolveRecipient(string name) { using (ComRelease com = new ComRelease()) { NSOutlook.NameSpace session = com.Add(_app.Session); // Add recipient, unlock after Resolve (which might throw) to wrap NSOutlook.Recipient recipient = com.Add(session.CreateRecipient(name)); if (recipient == null) { return(null); } IRecipient wrapped = Mapping.Wrap(com.Remove(recipient)); wrapped.Resolve(); return(wrapped); } }
/// <summary> /// Get all possible apointments for a specific email for X days /// </summary> /// <param name="UserName"></param> /// <param name="Days"></param> /// <returns></returns> private List <OutlookCalendarItem> _GetAppointmentsInRange(string UserName, int Days) { List <OutlookCalendarItem> ret = new List <OutlookCalendarItem>(); if (ConnectToServer() == false) { return(ret); } Outlook.NameSpace oNS = outlookObj.GetNamespace("mapi"); oNS.Logon(Missing.Value, Missing.Value, true, true); Outlook.Recipient oRecip = (Outlook.Recipient)oNS.CreateRecipient(UserName); Outlook.Folder calFolder = null; try { calFolder = (Outlook.MAPIFolder)oNS.GetSharedDefaultFolder(oRecip, Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder; } catch { return(ret); } // Outlook.Folder calFolder = outlookObj.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.Folder; DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, 0); DateTime end = start.AddDays(Days); Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); if (rangeAppts != null) { foreach (Outlook.AppointmentItem item in rangeAppts) { OutlookCalendarItem ci = new OutlookCalendarItem(); ci.Start = item.Start; ci.Duration = item.Duration; ci.ResponseType = (OutlookResponseStatus)item.ResponseStatus; ret.Add(ci); } } return(ret); }
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); } }