net4.5 outlook 插件开发
需求 做到企业邮箱和outlook自动或者手动同步,同步日历和通讯录。
手动设置登录
程序运行是创建2个自定义文件夹 一个日历和一个通讯录。并且在每次保存后触发事件
private void ThisAddIn_Startup(object sender, System.EventArgs e) { _iCreatePesrsonCalendar = new CreatePesrsonCalendar(); _ICalenda = new CreatePesrsonCalendar(); _IContacts = new ContactServeice(); _httpInvoke = new HttpInvoke(); CreateCustomCalendars(); CreateContacts(); #region 自动登录 LoginInfo loginInfo = LoginInfo.CreateInstance(); loginInfo.AccountID = Settings.Default.UserID.Trim(); loginInfo.AccountPassWord = Settings.Default.Pwd; // _httpInvoke.MD5( this.txt_pwd.Text.Trim()); loginInfo.AccountLoginTime = Common.GetTimeStamp(); loginInfo.ASecretkey = _httpInvoke.CreateASecretkey(loginInfo); string b = _httpInvoke.Login(loginInfo); _httpInvoke.Writelog($"{loginInfo.AccountID}结果:b{b}"); #endregion inspectors = this.Application.Inspectors; inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector); #region 日历触发条件 { Outlook.MAPIFolder primaryCalendar = (Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); foreach (Outlook.Folder personalCalendar in primaryCalendar.Folders) { var name = personalCalendar.Name.ToString(); if (name == ConstKey.foderKey) { personalCalendar_items = personalCalendar.Items; personalCalendar_items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ItemsEvents_ItemAddCalendar); } } } #endregion #region 通讯录触发条件 { Outlook.MAPIFolder primaryContacts = (Outlook.MAPIFolder)this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); foreach (Outlook.Folder personalContacts in primaryContacts.Folders) { var name = personalContacts.Name.ToString(); if (name == ConstKey.foderContactsKey) { personalContactsr_items = personalContacts.Items; personalContactsr_items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ItemsEvents_ItemAddContacts); } } } #endregion #region 初始化系统自带的日历能触发 { //_MyAppointmentsFolder = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); //_Appointments = _MyAppointmentsFolder.Items; //_Appointments.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ItemsEvents_ItemAdd); } #endregion }
回调方法如下
/// 日历回调 /// </summary> /// <param name="Item"></param> public void ItemsEvents_ItemAddCalendar(object Item) { //InboxFolderItemAdded invoked by thread LoginInfo loginInfo = LoginInfo.CreateInstance(); if (loginInfo.Aresult) { Console.WriteLine("自动同步*************之前先检查自己的日历数据********************日历"); #region setp1 先检查outlook 本地的日历数据 var outlooklists = _iCreatePesrsonCalendar.GetCalendalFolder(ConstKey.foderKey); #endregion outlooklists.Except(outlooklists);///交叉并补 #region setp 2 webapi 的数据 #endregion #region setp3 开始同步 PersonalCalendarInfro p = new PersonalCalendarInfro() { start = DateTime.Now.AddMinutes(2), end = DateTime.Now.AddMinutes(2.25), description = "测试数据描述1111", title = "标题1111", location = "上海黄埔111111" }; _iCreatePesrsonCalendar.SaveFolder(ConstKey.foderKey, p); #endregion } } /// <summary> /// 通讯录回调 /// </summary> /// <param name="Item"></param> public void ItemsEvents_ItemAddContacts(object Item) { //InboxFolderItemAdded invoked by thread LoginInfo loginInfo = LoginInfo.CreateInstance(); if (loginInfo.Aresult) { Console.WriteLine("自动同步*************之前先检查自己的通讯录数据********************通讯录"); Console.WriteLine("2222"); } }