允许使用 ews 进行模拟

Qui*_*nry 1 c# impersonation calendar exchangewebservices

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

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

问题是我不断收到此错误“SMTP 地址没有与之关联的邮箱。”

是因为服务器不允许模拟吗?如果是这样我该如何允许?我希望有人能帮帮忙。

PS:抱歉英语不好

Gle*_*les 6

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

String MailboxToAccess = "PrimarySMTP@domain.demo";
Run Code Online (Sandbox Code Playgroud)

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

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

service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, MailboxToAccess);
Run Code Online (Sandbox Code Playgroud)

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

service.HttpHeaders.Add("X-AnchorMailbox", MailboxToAccess);
Run Code Online (Sandbox Code Playgroud)

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

FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, MailboxToAccess);
appointment.Save(CalendarFolderId,SendInvitationsMode.SendToNone);
Run Code Online (Sandbox Code Playgroud)

干杯格伦