使用EWS创建c#约会

时间:2016-02-24 09:27:28

标签: c# visual-studio outlook calendar exchangewebservices

我想创建一个程序,使其能够在其他人的Outlook日历中创建约会。例如:如果有人向他的老板免费询问5天,那么老板需要能够批准它并立即在该人的展望日历中显示。我尝试使用EWS制作代码,但我总是收到此错误:enter image description here Microsoft.Exchange.WebServices.dll中发生未处理的“Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException”类型异常 其他信息:自动发现阻止了可能不安全的重定向到

这是我的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Exchange.WebServices.Data;

namespace exchangetest
{
public partial class Test1 : Form
{

    public Test1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ExchangeService service = new ExchangeService();
        service.UseDefaultCredentials = true;
        service.Credentials = new WebCredentials("username@domain.com", "password");
        service.AutodiscoverUrl("username@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));
    }
}
}

我真的希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

您需要使用AutoDiscoverURL重载,允许您指定回调验证,例如

     service.AutodiscoverUrl("username@domain.com",adAutoDiscoCallBack);

        internal static bool adAutoDiscoCallBack(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }

        return result;

    }

Office365总是进行重定向,所以需要这样的东西,你可以输入更多的验证代码,通过验证服务器名称等来防止中间人攻击等更安全。

干杯 格伦