Merhaba arkadaşlar bugün sizler C# kodu ile Microsoft Exchange Web Servis kullanarak Outlook ta da yer alan Exchange üzerinde yer alan Takvimlerdeki dataların çekilmesinden bahsedeceğim.
İlk olarak Development yapacağımız yere Microsoft Exchange Web Services Managed API 2.0 indirerek kurmamız gerekmektedir.
Daha sonra oluşturduğumuz projede referans olarak “C:\Program Files\Microsoft\Exchange\Web Services\2.2” konumunda yüklenmiş olan “Microsoft.Exchange.WebServices.dll” DLL dosyasını projemize referans olarak ekliyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static ExchangeService ConnectToService(string UserName, string UserPass, string DomainName, ExchangeVersion exchangeVersion, string Url, ITraceListener listener) { ExchangeService service = new ExchangeService(exchangeVersion); if (listener != null) { service.TraceListener = listener; service.TraceFlags = TraceFlags.All; service.TraceEnabled = true; } service.Credentials = new NetworkCredential(UserName, UserPass, DomainName); Console.Write(string.Format("Connect EWS URL for {0}. Please wait... ", UserName)); service.Url = new Uri(Url); Console.WriteLine("Complete"); return service; } |
Yukarıdaki methodu projemize ekliyoruz ve bu methoda kullanıcı adı, kullanıcı şifresi, domain adı, exchange versiyon türünü, exchange asmx servis adresini ve ITraceListener bilgilerini vererek belirttiğimiz kullanıcı üzerinden exhange server ımıza erişmiş oluyoruz.
1 2 3 |
ExchangeService service = ConnectToService(userName, userPassword, domainName, ExchangeVersion.Exchange2013, EwsAsmxUrl, null); ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true; |
Yukarıda görüldüğü gibi exchange serverımıza bağlandık ve altkısımda da herhangi bir sertifikasyon hatasına yakalanmamak için her durumda validation dönüşümüzü true olarak belirliyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 |
EmailAddressCollection myRoomLists = service.GetRoomLists(); foreach (EmailAddress myAddress in myRoomLists) { Console.WriteLine("Room name: {0}", myAddress.Name); System.Collections.ObjectModel.Collection<EmailAddress> myRoomAddresses = service.GetRooms(myAddress); foreach (EmailAddress address in myRoomAddresses) { Console.WriteLine("Email Address: {0}", address.Address); Console.WriteLine("Name: {0}", address.Name); } } |
Yukarıdaki kod ile exchange server a bağlandığınız kullanıcı ile paylaşılan odaları ve onlarda bulunan mail adreslerini listeleyebilir ve bu sayede hangi mail adreslerinin de verilerine erişe bileceğinizi görebilirsiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
ItemView view = new ItemView(5); string querystring = "Subject:'Weekly Tennis Lesson'"; try { FindItemsResults<Item> masterResults = service.FindItems(WellKnownFolderName.Calendar, querystring, view); foreach (Item item in masterResults.Items) { Appointment appointment = item as Appointment; Console.WriteLine("Subject : " + appointment.Subject); Console.WriteLine("Date : " + appointment.DateTimeCreated); if (appointment.AppointmentType == AppointmentType.RecurringMaster) { Console.WriteLine("Appointment is the recurring master appointment."); } else { Console.WriteLine("Appointment is not part of a recurring series."); } } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } |
Yukarıdaki kodumuz ile servise bağlandığımız kullanıcının takvimine sorgu atarak istediğimiz kriterlerde takvim getirebiliriz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
try { String MailboxToAccess = ""; DateTime startDate = new DateTime(2017, 6, 1); DateTime endDate = new DateTime(2017, 8, 1); CalendarView calView = new CalendarView(startDate, endDate); FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, MailboxToAccess); FindItemsResults<Item> instanceResults = service.FindItems(CalendarFolderId, calView); service.LoadPropertiesForItems(instanceResults, new PropertySet( AppointmentSchema.Id, AppointmentSchema.ICalUid, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.IsAllDayEvent, AppointmentSchema.DisplayTo, AppointmentSchema.DisplayCc, AppointmentSchema.Organizer, AppointmentSchema.Categories, AppointmentSchema.Location, AppointmentSchema.RequiredAttendees, AppointmentSchema.Sensitivity)); foreach (Item item in instanceResults.Items) { Appointment appointment = item as Appointment; Console.WriteLine("Id : " + appointment.Id); Console.WriteLine("Subject : " + appointment.Subject); Console.WriteLine("Organizer Email : " + appointment.Organizer.Address); Console.WriteLine("Date : " + appointment.DateTimeReceived); Console.WriteLine("start : " + appointment.Start); Console.WriteLine("end : " + appointment.End); Console.WriteLine("location : " + appointment.Location); Console.WriteLine("Organizer : " + appointment.Organizer); Console.WriteLine("Duration : " + appointment.Duration); Console.WriteLine("AllDay : " + appointment.IsAllDayEvent); Console.WriteLine("DisplayTo : " + appointment.DisplayTo); for (int j = 0; j < appointment.RequiredAttendees.Count; j++) { Console.WriteLine("Attendee Email : " + appointment.RequiredAttendees[j].Address); } if (appointment.AppointmentType == AppointmentType.Occurrence) { Console.WriteLine("Appointment is a recurring occurrence."); } else if (appointment.AppointmentType == AppointmentType.Exception) { Console.WriteLine("Appointment is an exception occurrence."); } else { Console.WriteLine("Appointment is not part of a recurring series."); } } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } |
Yukarıda ki kod kısmında ise “MailboxToAccess” değişkenine yazmış olduğunuz mail adresi ile belirlediğimiz tarihler arasında bulunan bu adresin exchange üzerindeki outlook takvimine erişerek oradaki dataları çekebiliriz.
Veya bu iki kodu karıştırarak istediğimiz mail adresine sorgu atarak belirlediğimiz kriterlere uyan takvim öğelerini getirtebiliriz.
[…] erişerek takvimimizde yeni bir etkinlik oluşturmayı anlatacağım. Bir önceki yazımda C# ile Microsoft Exchange Web Service Kullanarak Takvime Erişip Takvim Verilerini Almak konusunu […]
[…] önceki yazılarımda C# ile Microsoft Exchange Web Service Kullanarak Takvime Erişip Takvim Verilerini Almak ve C# ile Microsoft Exchange Web Service Kullanarak Takvime Erişip Takvime Etkinlik […]
[…] C# ile Microsoft Exchange Web Service Kullanarak Takvime Erişip Takvim Verilerini Almak […]
merhaba çok teşekkürler, kaynak kodları paylaşabilir misiniz acaba
Merhabalar, eğer blog yazısını okursanız kodları da açıklamalı bir şekilde yazının içinde paylaştığımı da göreceksiniz.
çok pardon c# da yeniyim,
ExchangeService service = ConnectToService(userName, userPassword, domainName, ExchangeVersion.Exchange2013, EwsAsmxUrl, null);
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
bu kodları;
public static ExchangeService ConnectToService(string UserName, string UserPass, string DomainName, ExchangeVersion exchangeVersion, string Url, ITraceListener listener)
bu methodun içinde mi yazacağız acaba
Hayır. ConnectToService methodu exchange bağlantısını sağlayan bir method yukarıdaki eklemek istediğiniz kod ise bağlantınızı nerede yapmaz isterseniz orada kullanacağınız kod kısmı. Mesele Main ya da buton tıklanması gibi.
methoda gönderdiğimiz ITraceListener listener değerini null olarak mı göndermeliyiz.
methoda listener değerini null mı gönderiyoruz