繁体   English   中英

允许使用 ews 进行模拟

[英]Allow impersonation using ews

我正在尝试用 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:抱歉英语不好

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

String MailboxToAccess = "PriamarySMTP@domain.com";

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

这就是您要冒充的用户,例如

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

您还应该添加 X-AnchorMailbox 标头https://docs.microsoft.com/en-us/archive/blogs/webdav_101/best-practices-ews-authentication-and-access-issues例如

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

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

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

干杯格伦

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM