【问题标题】:Allow impersonation using ews允许使用 ews 进行模拟
【发布时间】:2023-03-31 12:05:01
【问题描述】:

我正在尝试用 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: 对不起英文不好

【问题讨论】:

  • 我们缺少异常消息;在导致该异常的代码周围放置一个 try/catch 并查看错误消息是什么。看起来其他人在这里也有类似的问题:stackoverflow.com/questions/32062394/…
  • 现在,我不断收到消息:“SMTP 地址没有与之关联的邮箱。
  • 你的问题!在我看来,您在代码中所做的一切都是正确的 - 但是帐户的先决条件没有得到满足。使用此错误消息更新您的问题。

标签: c# calendar exchangewebservices impersonation


【解决方案1】:

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

String MailboxToAccess = "PriamarySMTP@domain.com";

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

这就是您要模拟的用户,例如

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

干杯 格伦

【讨论】:

    猜你喜欢
    • 2011-12-04
    • 1970-01-01
    • 2017-04-22
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多