允许使用ews进行假冒

时间:2016-02-25 08:32:30

标签: c# calendar exchangewebservices impersonation

我试图在C#中创建一个程序,使其能够在其他人的Outlook日历中创建约会。我有代码,可以在我自己的日历中创建约会。我在谷歌搜索,发现我应该使用模仿。所以我添加了一行:

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

这是我的代码:

private void button2_Click(object sender, EventArgs e) {
try{
    ExchangeService service = new ExchangeService();
    service.UseDefaultCredentials = true;   
    service.Credentials = new WebCredentials("Test@domain.com", "password");
    service.AutodiscoverUrl("Test@domain.com", adAutoDiscoCallBack);
    service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "Test2@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);
        }
}

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;
}

问题是我一直收到此错误"" SMTP地址没有与之关联的邮箱。"

是否因为服务器上不允许假冒?如果是这样我怎么允许呢? 我希望有人可以提供帮助。

Ps:抱歉英文不好

1 个答案:

答案 0 :(得分:1)

一些建议,如果这是Office365或Exchange 2013,您首先需要您希望访问的邮箱的PrimarySMTP地址,例如

String MailboxToAccess = "PriamarySMTP@domain.com";

对于某些租户,请注意SMTP和UPN / Logon是相同的,但情况并非总是如此。

这就是你要模仿的用户,例如

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, MailboxToAccess);

您还应该添加X-AnchorMailbox标头http://blogs.msdn.com/b/webdav_101/archive/2015/05/11/ews-authentication-issues.aspx,例如

service.HttpHeaders.Add("X-AnchorMailbox", MailboxToAccess);

另外,当您去保存约会时,请使用带有邮箱重载的FolderId类,以确保您点击正确的邮箱,例如

FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, MailboxToAccess);
appointment.Save(CalendarFolderId,SendInvitationsMode.SendToNone);

干杯 格伦