public static ExchangeService ConnectToServiceWithImpersonation( IUserData userData, string impersonatedUserSMTPAddress, ITraceListener listener) { ExchangeService service = new ExchangeService(userData.Version); if (listener != null) { service.TraceListener = listener; service.TraceFlags = TraceFlags.All; service.TraceEnabled = true; } service.Credentials = new NetworkCredential(userData.EmailAddress, userData.Password); ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impersonatedUserSMTPAddress); service.ImpersonatedUserId = impersonatedUserId; if (userData.AutodiscoverUrl == null) { service.AutodiscoverUrl(userData.EmailAddress, RedirectionUrlValidationCallback); userData.AutodiscoverUrl = service.Url; } else { service.Url = userData.AutodiscoverUrl; } return(service); }
private static async System.Threading.Tasks.Task Run() { var configuration = new ConfigurationBuilder() .AddUserSecrets("a8aa09bf-0683-4154-81b4-9fd6392e0a74") .Build(); var webCredentials = new WebCredentials(configuration["Username"], configuration["Password"]); var impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, configuration["Email"]); var uri = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"); var service = new ExchangeService(ExchangeVersion.Exchange2010_SP1) { ImpersonatedUserId = impersonatedUserId, Credentials = webCredentials, Url = uri }; var folderId = new FolderId(WellKnownFolderName.Calendar); var propertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Start, AppointmentSchema.AppointmentType); var syncFolder = await service.SyncFolderItems(folderId, propertySet, null, 512, SyncFolderItemsScope.NormalItems, null); foreach (var itemChange in syncFolder) { if (itemChange.Item is Appointment appointment && appointment.AppointmentType == AppointmentType.RecurringMaster) { var collection = await OccurrenceCollection.Bind(service, appointment.Id); foreach (var item in collection) { Console.WriteLine(item.Start.ToString("g")); } } } }
private ExchangeService CreateImpersonatedService(ImpersonatedUserId UserId, ExchangeCredentials exchangeCredentials) { return(new ExchangeService(this.exchangeVersion) { Credentials = exchangeCredentials, ImpersonatedUserId = UserId }); }
// This private method creates a new ExchangeService object using the email addresess and credentails passed from the // public methods. private static ExchangeService InternalConnectToService(string emailAddress, ICredentials credentials, string impersonatedUserEmail) { Console.Write(" Calling Autodiscover service to get Exchange endpoint..."); ExchangeService service = new ExchangeService(ServerVersion); if (TraceListener != null) { service.TraceListener = TraceListener; service.TraceFlags = TraceFlags.All; service.TraceEnabled = true; } service.Credentials = (NetworkCredential)credentials; if (!string.IsNullOrEmpty(impersonatedUserEmail)) { ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impersonatedUserEmail); service.ImpersonatedUserId = impersonatedUserId; } service.AutodiscoverUrl(emailAddress, RedirectionUrlValidationCallback); Console.WriteLine("complete."); Console.WriteLine(string.Format(" EWS URL: {0}", service.Url)); return(service); }
static ExchangeService ConnectService(string emailAddress, string password, string url, ExchangeVersion version = ExchangeVersion.Exchange2013_SP1, bool trace = false) { ImpersonatedUserId impersonatedUserId = null; NetworkCredential networkCredentials = null; if (networkCredentials == null) { networkCredentials = new NetworkCredential(emailAddress, password); } var service = new ExchangeService(version, TimeZoneInfo.Utc) { ImpersonatedUserId = impersonatedUserId, Credentials = networkCredentials, // SCP is slow and only applies to in-network Exchange // see https://blogs.msdn.microsoft.com/webdav_101/2015/05/11/best-practices-ews-authentication-and-access-issues/ EnableScpLookup = false, UserAgent = "MSBotFramework" }; // set X-AnchorMailbox when impersonation is used // https://blogs.msdn.microsoft.com/webdav_101/2015/05/11/best-practices-ews-authentication-and-access-issues/ // https://blogs.msdn.microsoft.com/mstehle/2013/07/25/managing-affinity-for-ews-impersonation-in-exchange-2013-and-exchange-online-w15/ if (service.ImpersonatedUserId?.Id != null) { service.HttpHeaders.Add("X-AnchorMailbox", service.ImpersonatedUserId.Id); } if (trace) { service.TraceEnablePrettyPrinting = true; service.TraceFlags = TraceFlags.All; service.TraceListener = ConsoleTraceListener.Instance; service.TraceEnabled = true; } if (!string.IsNullOrEmpty(url)) { service.Url = new Uri(url); } else { service.AutodiscoverUrl(emailAddress, RedirectionUrlValidationCallback); } return(service); }
static void Main(string[] args) { // Get the email address of the account to impersonate. Console.Write("Email address to impersonate: "); string impersonatedEmailAddress = Console.ReadLine(); // Add the imersonated account to the Exchange service object. ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impersonatedEmailAddress); service.ImpersonatedUserId = impersonatedUserId; CreateFolder(service, "Custom Folder", WellKnownFolderName.Inbox); Console.WriteLine("\r\n"); Console.WriteLine("Press or select Enter..."); Console.Read(); }