C#中调用Outlook API 发起会议

OutLook是微软Office的邮箱,相比于普通邮箱,多了一个发起会议的功能,但因此使用起来比普通邮箱复杂一些,下面请看使用方式:

创建项目后,为它添加.NET引用:“Microsoft.Office.Interop.Outlook"的引用,即可调用,需要注意的是,在添加的时候,注意一下OFFICE版本号。由于我使用ApplicationClass,需要在引用上点击右键属性,把嵌入互操作类型改为false才可以正常使用

下面是win7+office2016创建成功的例子

 1 public bool Send()
 2             {
 3                 try
 4                 {
 5                     ApplicationClass oApp = new ApplicationClass();
 6                     //会议是约会的一种
 7                     AppointmentItem oItem = (AppointmentItem)oApp.CreateItem(OlItemType.olAppointmentItem);
 8                     oItem.MeetingStatus = OlMeetingStatus.olMeeting;
 9                     oItem.Subject = Title;
10                     oItem.Body = Content;
11                     oItem.Location = Position;
12                     //开始时间 
13                     oItem.Start = StartTime;
14                     //结束时间
15                     oItem.End = EndTime;
16                     //提醒设置
17                     oItem.ReminderSet = true;
18                     oItem.ReminderMinutesBeforeStart = RemindMinute;
19                     //是否全天事件
20                     oItem.AllDayEvent = false;
21                     oItem.BusyStatus = OlBusyStatus.olBusy;
22                     //索引从1开始,而不是从0
23                     //发件人的帐号信息
24                     oItem.SendUsingAccount = oApp.Session.Accounts[1];
25 
26                     Recipient force = null;
27                     for (int i = 0; i < Requireds.Count; i++)
28                     {
29                         if (!string.IsNullOrWhiteSpace(Requireds[i]))
30                         {
31                             force = oItem.Recipients.Add(Requireds[i]);
32                         }
33                     }
34                     if (force != null)
35                     {
36                         force.Type = (int)OlMeetingRecipientType.olRequired;
37 
38                     }
39 
40                     Recipient opt = null;
41                     for (int i = 0; i < Optionals.Count; i++)
42                     {
43                         if (!string.IsNullOrWhiteSpace(Optionals[i]))
44                         {
45                             //添加可选人
46                             opt = oItem.Recipients.Add(Optionals[i]);
47                         }
48                     }
49                     if (opt != null)
50                     {
51                         opt.Type = (int)OlMeetingRecipientType.olOptional;
52                     }
53 
54                     oItem.Recipients.ResolveAll();
55                     oItem.Send();
56                     Thread.Sleep(3000);
57 
58                     return true;
59                 }
60                 catch (System.Exception e)
61                 {
62                     return false;
63                 }
64             }

值得一提的是,在Web中使用该代码,实例化ApplicationClass对象时发生错误,com组件报错权限不够,dcomcnfg.exe中没找到相应CLASID,尝试了N种方法没解决,希望有大神可以看看原因

在C/S程序或控制台程序运行没出现问题,不知道是不是个人邮箱的缘故,每天发一定的条数就不能再发了

猜你喜欢

转载自www.cnblogs.com/JessieR/p/9015403.html