使用EWS和C#动态发送批量日历约会时出错

时间:2014-02-12 09:27:42

标签: c# calendar exchangewebservices

我正在开发一个出站呼叫系统,该系统向用户显示要呼叫的下一家公司,并为所选销售代表添加约会。

我最初将约会编程为实时发送,因为用户保存当前数据并转到下一个引导。但是,在将约会发送到Office 365上的电子邮件帐户时,存在显着延迟。因此,我决定创建一个小应用程序,每15分钟批量发送一次。

这适用于前三次约会,但后来我在第4次预约时收到错误:

  

无法找到自动发现服务。

我怀疑这就像托管公司阻止邮箱被用于批量电子邮件而且存在限制。 EWS中是否有东西允许一次超过三个?或者我需要修改我的代码吗?

以下是我发送约会的代码:

const string o365Server = "mydomain.co.uk";

var appointmenntList = AppointmentList.GetAppointnmetsToSend();
ExchangeService service = new ExchangeService();

foreach (var appointment in appointmenntList)
{
    Console.WriteLine(appointment.IntLeadID);

    service.Credentials = new WebCredentials(appointment.StrSalesRepEmail, apointment.StrSalesRepEmailPassword, o365Server);
    service.AutodiscoverUrl(appointment.StrSalesRepEmail, RedirectionCallback);

    Appointment app = new Appointment(service);

    app.Subject = "ASH Waste Appointment with " + appointment.StrLeadAppointmentContact;
    app.Body = appointment.StrLeadAppointmentNotes;
    app.Start = appointment.DtLeadAppointmentDate;
    app.End = app.Start.AddHours(1);
    app.Location = appointment.StrLeadAppointmentLocation;
    app.RequiredAttendees.Add(appointment.StrSalesRepEmail);
    app.ReminderMinutesBeforeStart = 60;
    app.Save(SendInvitationsMode.SendOnlyToAll);
    Console.WriteLine(appointment);
}

static bool RedirectionCallback(string url)
{
    return url.ToLower().StartsWith("https://");
}

我可以将应用设置为一次只执行三次,每2分钟运行一次应用。虽然这对我们的销售团队来说足够了,但它限制了操作。

有什么建议吗?

编辑:

这是完整的错误:

  

未处理的类型异常   Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException”   发生在Microsoft.Exchange.WebServices.dll

它出现在这行代码中:

service.AutodiscoverUrl(appointment.StrSalesRepEmail, RedirectionCallback);

2 个答案:

答案 0 :(得分:1)

我可以在使用CreateItems方法时批量处理4个日历项目。你试过吗?这是一些示例代码。

   public static Collection<ItemId> BatchCreateCalendarItems(ExchangeService service)
    {
        // These are unsaved local instances of an Appointment object.
        // Despite the required parameter of an ExchangeService object (service), no call
        // to an Exchange server is made when the objects are instantiated.
        // A call to the Exchange server is made when the service.CreateItems() method is called.

        Appointment appt1 = new Appointment(service);
        Appointment appt2 = new Appointment(service);
        Appointment appt3 = new Appointment(service);
        Appointment appt4 = new Appointment(service);

        // Set the properties for a single instance appointment
        appt1.Subject = "Appt1";
        appt1.Body = "Appt1";
        appt1.Start = DateTime.Now.AddDays(1);
        appt1.End = appt1.Start.AddHours(3);
        appt1.Location = "My office";
        appt1.ReminderMinutesBeforeStart = 30;

        // Set the properties for a single instance appointment
        appt2.Subject = "Appt2";
        appt2.Body = "Appt2";
        appt2.Start = DateTime.Now.AddDays(1);
        appt2.End = appt1.Start.AddHours(4);
        appt2.Location = "My office";
        appt2.ReminderMinutesBeforeStart = 30;

        // Set the properties for a single instance appointment
        appt3.Subject = "Appt3";
        appt3.Body = "Appt3";
        appt3.Start = DateTime.Now.AddDays(1);
        appt3.End = appt1.Start.AddHours(5);
        appt3.Location = "My office";
        appt3.ReminderMinutesBeforeStart = 30;

        // Set the properties for a single instance appointment
        appt4.Subject = "Appt4";
        appt4.Body = "Appt4";
        appt4.Start = DateTime.Now.AddDays(1);
        appt4.End = appt1.Start.AddHours(6);
        appt4.Location = "My office";
        appt4.ReminderMinutesBeforeStart = 30;

        // Add the appointment objects to a collection
        Collection<Appointment> calendarItems = new Collection<Appointment>() { appt1, appt2, appt3, appt4 };

        // Instantiate a collection of item ids to populate from the values that are returned by the Exchange server.
        Collection<ItemId> itemIds = new Collection<ItemId>();

        // Send the batch of appointment objects.
        // Note that multiple calls to the Exchange server may be made when appointment objects have attachments.
        // Note also that the item collection passed as the first parameter to CreateItems will have their ids set on return.
        ServiceResponseCollection<ServiceResponse> response = service.CreateItems(calendarItems,
                                                                                  WellKnownFolderName.Calendar,
                                                                                  MessageDisposition.SendAndSaveCopy,
                                                                                  SendInvitationsMode.SendToAllAndSaveCopy);
        if (response.OverallResult == ServiceResult.Success)
        {
            Console.WriteLine("All appointments and meetings sucessfully created.");
        }

        // Collect the item ids from the created calendar items.
        foreach (Appointment appt in calendarItems)
        {
            itemIds.Add(appt.Id);
        }

        int counter = 1;
        // Show the ids and errors for each message
        foreach (ServiceResponse resp in response)
        {
            // Note that since item ids are long, show only 5 characters.
            Console.WriteLine("Result (message {0}), id {1}: {2}", counter, itemIds[counter - 1].ToString().Substring(0, 5), resp.Result);
            Console.WriteLine("Error Code: {0}", resp.ErrorCode);
            Console.WriteLine("ErrorMessage: {0}\r\n", resp.ErrorMessage);

            counter++;
        }
        // Return the collection of item ids
        return itemIds;
    }

答案 1 :(得分:1)

嗯,您不会被阻止发送特定数量的约会,而且自动发现失败了。该错误是EWS托管API喜欢为各种场景返回的错误,因此它并不能告诉我它为什么会失败。这可能是一个临时的网络问题,或者可能是自动发现服务器限制了你。我不确定自动发现是否有任何限制,但它当然是可能的。

要了解它失败的原因,您可以enable tracing启用所有与自动发现相关的跟踪标记,并查看会出现哪种错误。

相关问题