最近做了一个项目,负责的主要模块,是web系统跟 OutLook 数据相互同步,就是从web发送邮件,会议邀请到 OutLook,然后把OutLook客户端发送的Meeting同步到web系统中
首先是读取OutLook
1 用有权限的账号登陆EWS服务 读取指定邮箱 的Meeting。这个账号是一个权限比较大的账号,可以模拟别的邮箱,读取被模拟邮箱的邮件,如果是普通用户邮箱,要读取自己的邮件信息,只需要开通访问EWS服务的权限。
1 static void TestEws() 2 { 3 try 4 { 5 ExchangeService service = new ExchangeService(); 6 service.Credentials = new WebCredentials(WebConfig.GetString("Usercount"), WebConfig.GetString("pwd"));//有访问EWS 权限的账号 密码 7 service.Url = new Uri(WebConfig.GetString("ExchangeUrl"));//已知EWS URL 8 9 var folderView = new FolderView(1000) 10 { 11 Traversal = FolderTraversal.Deep, 12 PropertySet = new PropertySet(FolderSchema.FolderClass, 13 FolderSchema.DisplayName, 14 FolderSchema.TotalCount, 15 FolderSchema.ArchiveTag, 16 FolderSchema.ManagedFolderInformation, 17 FolderSchema.ParentFolderId) 18 }; 19 20 service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, WebConfig.GetString("calendarEmail"));//模拟邮箱,读取被模拟邮箱的Meeting
21 FindFoldersResults folders = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderView); 22 23 var calendarlist = folders.Folders.Where(x => x.DisplayName.ToLower().Trim() == "calendar"); 26 27 28 var CalendarList = new List<Invitation>(); 29 foreach (Folder myFolder in calendarlist) 30 { 31 var calendar = (myFolder as CalendarFolder); 32 CalendarView cView = new CalendarView(DateTime.Now, DateTime.Now.AddDays(90), 99999); 33 cView.PropertySet = new PropertySet(BasePropertySet.IdOnly); 34 FindItemsResults<Appointment> findResults = calendar.FindAppointments(cView); 35 36 if (findResults.TotalCount <= 0) 37 continue; 38 //load properties, like attendees, textbody and so on 39 service.LoadPropertiesForItems( 40 (from item in findResults select item).Distinct(), 41 new PropertySet( 42 BasePropertySet.IdOnly, 43 AppointmentSchema.Subject, 44 AppointmentSchema.Start, 45 AppointmentSchema.ConversationId, 46 AppointmentSchema.End, 47 AppointmentSchema.Organizer, 48 AppointmentSchema.TextBody, 49 AppointmentSchema.RequiredAttendees, 50 AppointmentSchema.AppointmentState, 51 AppointmentSchema.AppointmentType, 52 AppointmentSchema.Location, 53 AppointmentSchema.LastModifiedTime, 54 AppointmentSchema.Body, 55 AppointmentSchema.ICalUid 56 )); 57 58 int count = 0; 59 var itemlist = new List<string>(); 60 foreach (Appointment a in findResults) 61 { 62 if (itemlist.Contains(a.ICalUid)) 63 { 64 continue; 65 } 66 else 67 { 68 itemlist.Add(a.ICalUid); 69 } 70 71 if (a.Location == null) 72 { 73 count++; 74 break; 75 } 76 77 CalendarList.Add(new Invitation() 78 { 79 OutlookID = a.ICalUid.ToString(), 80 Subject = a.Subject.ToString(), 81 Organizer = a.Organizer.Address.ToString(), 82 Attendees = GetAttendees(a.RequiredAttendees), 83 Start = a.Start, 84 End = a.End, 85 AppointmentState = a.AppointmentState, 86 LastModifiedTime = a.LastModifiedTime, 87 Location = a.Location.ToString(), 88 TextBody = a.Body.ToString() 89 }); 90 count++; 91 92 } 93 94 } 110 121 Console.Read(); 122 123 } 124 catch (Exception ex) 125 { 126 Console.WriteLine(ex.Message); 135 136 } 137 }
2.订阅制定邮箱,当该邮箱收到新的有件时,能直接收到EWS的推送信息
订阅分三种方式 EWS拉通知 EWS流式处理通知 推送通知
参考资料
https://msdn.microsoft.com/zh-cn/library/office/dn458791(v=exchg.150).aspx
https://www.cnblogs.com/wolf-sun/p/6483415.html