使用C#在其他人的Outlook日历中创建约会

时间:2016-02-23 10:28:55

标签: c# outlook calendar

我想创建一个程序,可以在其他人的Outlook日历中创建约会。例如:如果某人要求他们的老板免费五天,他们的老板需要能够批准它并立即在个人的展望日程中显示。我已经制作了一些代码,允许您设置自己的约会。这是我的代码:

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddAppointment("ConferenceRoom #2345", "We will discuss progression the group project.", "Group Project", new DateTime(2016, 02, 23, 15, 30, 52), new DateTime(2016, 02, 23, 20, 30, 52));
    }
    private void AddAppointment(string location, string body, string subject, DateTime startdatum, DateTime einddatum)
    {
        try
        {
            var AppOutlook = new Outlook.Application();

            Outlook.AppointmentItem newAppointment =
            (Outlook.AppointmentItem)
            AppOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem);
            newAppointment.Start = startdatum;
            newAppointment.End = einddatum;
            newAppointment.Location = location;
            newAppointment.Body = body;
            newAppointment.BusyStatus=Outlook.OlBusyStatus.olTentative;
            newAppointment.AllDayEvent = true;
            newAppointment.Subject = subject;
            newAppointment.Save();
            newAppointment.Display(true);
        }
        catch (Exception ex)
        {
            MessageBox.Show("The following error occurred: " + ex.Message);
        }
    }

PS:对不起,如果我的英语不是很好。

2 个答案:

答案 0 :(得分:0)

在Outlook中创建约会有三种主要方法。您需要使用文件夹的Items集合(在我们的示例中为Calendar文件夹)。 Add方法也接受OlItemType枚举并返回新创建的Outlook项。 How To: Create a new Outlook Appointment item文章深入介绍了所有可能的方法。

     items = calendarFolder.Items;
     appItem = items.Add(Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
     appItem.Save();
     appItem.Display(true);

注意,您需要先获取共享日历文件夹。 Namespace类的GetSharedDefaultFolder方法返回一个Folder对象,该对象表示指定用户的指定默认文件夹。此方法用于委派方案,其中一个用户已为一个或多个默认文件夹(例如,其共享的日历文件夹)委派了对另一个用户的访问权限。例如:

  Sub ResolveName()  
   Dim myNamespace As Outlook.NameSpace  
   Dim myRecipient As Outlook.Recipient  
   Dim CalendarFolder As Outlook.Folder 
   Set myNamespace = Application.GetNamespace("MAPI")  
   Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev")  
   myRecipient.Resolve  
   If myRecipient.Resolved Then  
    Call ShowCalendar(myNamespace, myRecipient)  
   End If  
  End Sub 

  Sub ShowCalendar(myNamespace, myRecipient)  
   Dim CalendarFolder As Outlook.Folder 
   Set CalendarFolder = _  
   myNamespace.GetSharedDefaultFolder _  
   (myRecipient, olFolderCalendar)  
   CalendarFolder.Display  
  End Sub

答案 1 :(得分:0)

我使用了EWS api(https://msdn.microsoft.com/en-us/library/office/dd633710(v=exchg.80).aspx

我使用的代码是:

try
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            service.UseDefaultCredentials = true;
            service.Credentials = new WebCredentials("user@domain.com", "password");
            service.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx");
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "user2@domain.com");




            Appointment appointment = new Appointment(service);
            // Set the properties on the appointment object to create the appointment.
            appointment.Subject = "Tennis lesson";
            appointment.Body = "Focus on backhand this week.";
            appointment.Start = DateTime.Now.AddDays(2);
            appointment.End = appointment.Start.AddHours(1);
            appointment.Location = "Tennis club";
            appointment.ReminderDueBy = DateTime.Now;

            // Save the appointment to your calendar.
            appointment.Save(SendInvitationsMode.SendToNone);

            // Verify that the appointment was created by using the appointment's item ID.
            Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
相关问题