使用 C# 在 Outlook 日历中创建约会
Create an appointment in outlook calendar with C#
我想在 outlook 中创建一个约会 calendar.I 已经编写了一些代码,您可以在其中设置自己的约会,但无法正常工作
var pcaOptions = new PublicClientApplicationOptions
{
ClientId = "xxxxxx",
TenantId = "xxxxxx"
};
var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();
var ewsScopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };
// Make the interactive token request
var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();
// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
Appointment appointment = new Appointment(ewsClient);
// 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(ewsClient, appointment.Id, new PropertySet(ItemSchema.Subject));
exception:The request failed. Unable to connect to the remote server
有什么办法解决吗?
谢谢
找了这么多终于找到答案了
string ewsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
string userName = "outlookUsername";
string password = "outlookpasword";
ExchangeService servicex = new ExchangeService();
servicex.Url = new Uri(ewsUrl);
servicex.UseDefaultCredentials = true;
servicex.Credentials = new WebCredentials(userName, password);
try
{
Appointment appointment = new Appointment(servicex);
// 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(servicex, appointment.Id, new PropertySet(ItemSchema.Subject));
MessageBox.Show("Sucessfully Event Created");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
以上代码在 Outlook 中使用成功创建事件。
谢谢。
我想在 outlook 中创建一个约会 calendar.I 已经编写了一些代码,您可以在其中设置自己的约会,但无法正常工作
var pcaOptions = new PublicClientApplicationOptions
{
ClientId = "xxxxxx",
TenantId = "xxxxxx"
};
var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();
var ewsScopes = new string[] { "https://outlook.office.com/EWS.AccessAsUser.All" };
// Make the interactive token request
var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();
// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://mail.domain.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
Appointment appointment = new Appointment(ewsClient);
// 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(ewsClient, appointment.Id, new PropertySet(ItemSchema.Subject));
exception:The request failed. Unable to connect to the remote server
有什么办法解决吗?
谢谢
找了这么多终于找到答案了
string ewsUrl = "https://outlook.office365.com/EWS/Exchange.asmx";
string userName = "outlookUsername";
string password = "outlookpasword";
ExchangeService servicex = new ExchangeService();
servicex.Url = new Uri(ewsUrl);
servicex.UseDefaultCredentials = true;
servicex.Credentials = new WebCredentials(userName, password);
try
{
Appointment appointment = new Appointment(servicex);
// 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(servicex, appointment.Id, new PropertySet(ItemSchema.Subject));
MessageBox.Show("Sucessfully Event Created");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
以上代码在 Outlook 中使用成功创建事件。 谢谢。