【发布时间】: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