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 }
/// <summary> /// Updates a specific instance of an event from another calendar in Outlook /// The other shared calendar must be opened in outlook before running. /// Utilizes the updateEvent function in the common library. /// </summary> /// <param name="subject">Title of the Event to update</param> /// <param name="eventDate">The date of the specific event M/d/yyyy hh:mm:ss tt</param> /// <param name="updatedTitle">Updated title. not required</param> /// <param name="updatedStartDate">Updated start date. not required</param> /// <param name="updatedDuration">updated duration. not required</param> /// <param name="otherCalendar">Name of the calendar the event is located in</param> /// <param name="recipients">Recipients to add to the updated event</param> public void UpdateOtherCalendarEvent(string subject, string eventDate, string updatedTitle, string updatedStartDate, string updatedDuration, string otherCalendar, string[] recipients) { string methodTag = "UpdateOtherCalendarEvent"; LogWriter.WriteInfo(TAG, methodTag, "Starting: " + methodTag + " in " + TAG); Outlook.Application app = null; Outlook.AppointmentItem agendaMeeting = null; Outlook.NameSpace NS = null; Outlook.MAPIFolder objFolder = null; Outlook.MailItem objTemp = null; Outlook.Recipient objRecip = null; Outlook.Items objItems = null; app = new Outlook.Application(); NS = app.GetNamespace("MAPI"); objTemp = app.CreateItem(Outlook.OlItemType.olMailItem); objRecip = objTemp.Recipients.Add(otherCalendar); objTemp = null; LogWriter.WriteInfo(TAG, methodTag, "Attempting to resolve recipient object for: " + otherCalendar); objRecip.Resolve(); if (objRecip.Resolved) { objFolder = NS.GetSharedDefaultFolder(objRecip, Outlook.OlDefaultFolders.olFolderCalendar); objItems = objFolder.Items; agendaMeeting = objItems.Find("[Subject]=" + subject); agendaMeeting = CommonLibrary.UpdateEvent(agendaMeeting, eventDate, updatedTitle, updatedStartDate, updatedDuration, recipients); LogWriter.WriteInfo(TAG, methodTag, "Sending event"); agendaMeeting.Send(); LogWriter.WriteInfo(TAG, methodTag, subject + " sucessfully sent"); } else { LogWriter.WriteWarning(TAG, methodTag, "Recipient object was not sucessfully resolved"); throw new NullReferenceException(); } return; }
/// <summary> /// Retuns the calendar folder for the recipient if they are visible /// in the Outlook Explorer / added /// </summary> /// <param name="PobjRecipient"></param> /// <returns></returns> public static Outlook.MAPIFolder IsRecipientValid(Outlook.Recipient PobjRecipient) { // see if the selected user's calendar is available in Outlook // and then switch to it. Otherwise, give an error Outlook.MAPIFolder LobjFolder = null; try { Outlook.Application LobjApp = Globals.ThisAddIn.Application; Outlook.NameSpace LobjNs = LobjApp.GetNamespace("MAPI"); LobjFolder = LobjNs.GetSharedDefaultFolder( PobjRecipient, Outlook.OlDefaultFolders.olFolderCalendar) as Outlook.MAPIFolder; return(LobjFolder); } catch (Exception PobjEx) { throw new Exception("Could not open users's calendar. " + PobjEx.Message); } }
/// <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); }
/// <summary> /// Remove a specific event or a series of event from a shared calendar in outlook /// Requires the shared calendar to be opened in outlook. /// If the eventDate is specificed removes a specific instance. If not removes all events with that subject /// </summary> /// <param name="subject">Subject of the event to remove</param> /// <param name="eventDate">Date of the specific event to remove. If null, deletes all event with the title. M/d/yyyy hh:mm:ss tt</param> /// <param name="otherCalendar">Name of the calendar the event exists in</param> public void RemoveEventOtherCalendar(string subject, string eventDate, string otherCalendar) { string methodTag = "RemoveEventOtherCalendar"; LogWriter.WriteInfo(TAG, methodTag, "Starting: " + methodTag + " in " + TAG); Outlook.Application app = null; Outlook.NameSpace NS = null; Outlook.MAPIFolder objFolder = null; Outlook.MailItem objTemp = null; Outlook.Recipient objRecip = null; Outlook.Items objItems = null; app = new Outlook.Application(); NS = app.GetNamespace("MAPI"); objTemp = app.CreateItem(Outlook.OlItemType.olMailItem); objRecip = objTemp.Recipients.Add(otherCalendar); objTemp = null; LogWriter.WriteInfo(TAG, methodTag, "Attempting to resolve recipient object for: " + otherCalendar); objRecip.Resolve(); if (objRecip.Resolved) { objFolder = NS.GetSharedDefaultFolder(objRecip, Outlook.OlDefaultFolders.olFolderCalendar); objItems = objFolder.Items; LogWriter.WriteInfo(TAG, methodTag, "Attempting to remove: " + subject); CommonLibrary.RemoveEvent(objItems, subject, eventDate); LogWriter.WriteInfo(TAG, methodTag, subject + " successfully removed"); } else { LogWriter.WriteWarning(TAG, methodTag, "Recipient object was not sucessfully resolved"); throw new NullReferenceException(); } return; }