【问题标题】:Create an appointment in someone else's outlook calendar with C#使用 C# 在别人的 Outlook 日历中创建约会
【发布时间】:2016-02-25 18:02:29
【问题描述】:

我想创建一个程序,可以在其他人的 Outlook 日历中创建约会。例如:如果有人要求他们的老板免费五天,他们的老板需要能够批准并立即在该人的 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:对不起,如果我的英语不好。

【问题讨论】:

  • 如何将员工添加为必需的与会者? (见msdn.microsoft.com/en-us/library/office/ff867639.aspx
  • 一种需要更多时间的方法是使用 EWS(我已经使用过它,效果很好,但您可能会遇到权限问题),例如,请参阅this question on how to do它:stackoverflow.com/questions/2419651/…
  • 如果我将某人添加为必需的与会者,我想这个人的空闲天数也会在老板的日历中设置
  • 是的,没错。但在我的公司,几乎每个老板都希望在他们的员工休假时看到他们的日历

标签: c# outlook calendar


【解决方案1】:

我使用了 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);
        }

【讨论】:

    【解决方案2】:

    在 Outlook 中创建约会的主要方法有三种。您需要使用文件夹的Items 集合(在我们的例子中是日历文件夹)。 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
    

    【讨论】:

    • 但是如何使用他人的电子邮件在其他人的日历上设置项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多