无法在 C# 中创建模拟的约会
Can't create an impersonated appoinement in C#
我正在尝试创建一个允许我向多个用户添加多个 appointments/events 的程序(不使用共享日历)。
当我试图冒充某人时出现问题(我是管理员,所以我认为我没有“权限”问题),我得到一个错误 System.NullReferenceException : 'Object reference not set to an instance of an object.'保存约会时(不是创建模拟时,我不明白为什么)
不知道要实例化什么对象
如果我不模拟,代码就可以工作。约会已创建
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.Credentials = new WebCredentials(userEmail, userPassword);
service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, destEmail);
//service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, myEmail);
Appointment appointment = new Appointment(service);
// Set the properties on the appointment object to create the appointment.
appointment.Subject = subject;
appointment.Body = "Test Event";
appointment.Start = DateTime.Now.AddDays(2);
appointment.End = appointment.Start.AddHours(1);
appointment.Location = "Here";
appointment.ReminderDueBy = DateTime.Now;
// Save the appointment to your calendar.
appointment.Save();
// Verify that the appointment was created by using the appointment's item ID.
Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
Console.WriteLine("Appoinement created");
}
catch (Exception ex)
{
Console.WriteLine("The following error occurred: " + ex.Message);
}
Error when I try to save the appoinement
--------------------更新 02.07.2021
谢谢评论。
我删除了 service.UseDefaultCredentials = true
,如果在下一行中设置了新凭据,那是虚拟的。
约会 class 是默认的 Microsoft.Exchange.WebServices.Data.Appointment,我没有在那里更改任何内容。
这是一个 security/rights 问题。尽管我是管理员,但默认情况下管理员没有模拟权限。
问题是我给自己添加了所需的模拟权限,并且成功了。谢谢
它工作了一段时间。它无故停止工作,我没有改变任何东西。
所以我等了几个小时,再试了,还是不行,然后我重新添加权限,又可以了。
非常感谢comments/hints
我正在尝试创建一个允许我向多个用户添加多个 appointments/events 的程序(不使用共享日历)。
当我试图冒充某人时出现问题(我是管理员,所以我认为我没有“权限”问题),我得到一个错误 System.NullReferenceException : 'Object reference not set to an instance of an object.'保存约会时(不是创建模拟时,我不明白为什么)
不知道要实例化什么对象
如果我不模拟,代码就可以工作。约会已创建
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.Credentials = new WebCredentials(userEmail, userPassword);
service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, destEmail);
//service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, myEmail);
Appointment appointment = new Appointment(service);
// Set the properties on the appointment object to create the appointment.
appointment.Subject = subject;
appointment.Body = "Test Event";
appointment.Start = DateTime.Now.AddDays(2);
appointment.End = appointment.Start.AddHours(1);
appointment.Location = "Here";
appointment.ReminderDueBy = DateTime.Now;
// Save the appointment to your calendar.
appointment.Save();
// Verify that the appointment was created by using the appointment's item ID.
Item item = Item.Bind(service, appointment.Id, new PropertySet(ItemSchema.Subject));
Console.WriteLine("Appoinement created");
}
catch (Exception ex)
{
Console.WriteLine("The following error occurred: " + ex.Message);
}
Error when I try to save the appoinement
--------------------更新 02.07.2021 谢谢评论。
我删除了 service.UseDefaultCredentials = true
,如果在下一行中设置了新凭据,那是虚拟的。
约会 class 是默认的 Microsoft.Exchange.WebServices.Data.Appointment,我没有在那里更改任何内容。
这是一个 security/rights 问题。尽管我是管理员,但默认情况下管理员没有模拟权限。 问题是我给自己添加了所需的模拟权限,并且成功了。谢谢
它工作了一段时间。它无故停止工作,我没有改变任何东西。
所以我等了几个小时,再试了,还是不行,然后我重新添加权限,又可以了。
非常感谢comments/hints