詳解C#調(diào)用Outlook API
在我上一篇博文中曾提到了 SharePoint 中調(diào)用傳出電子郵件中的郵件服務(wù)器及地址發(fā)送郵件
但是,里面的方法只能用于發(fā)送普通電子郵件。如果要發(fā)起會議之類的特殊郵件的話,可以C#調(diào)用Outlook API,自身的API。
創(chuàng)建項(xiàng)目后,為它添加.NET引用:“Microsoft.Office.Interop.Outlook"的引用,即可調(diào)用,需要注意的是,在添加的時候,注意一下OFFICE版本號。
在C#調(diào)用Outlook API發(fā)起會議的過程中,遇到了一個問題:
創(chuàng)建完一個約會條目后,找了很久沒找到如何為這一約會指定“發(fā)件人”,后來一想,Window CF 中,查找人員信息有個OutlookSession的東東,
那這Outlook會不會有同樣的方式呢,經(jīng)過測試,還真的找到方法,原來,它的API指定的發(fā)件人是和你機(jī)上運(yùn)行的Outlook的帳戶設(shè)置直接相關(guān)的。
通過 ApplicationClass.Session.Accounts即可找到您設(shè)置的帳戶集合,需要特別特別注意的是,在這里,取某個人員時,集合的索引是從1開始,而不是
從0開始。 找到相關(guān)的帳戶后,可以通過 AppointmentItem.SendUsingAccount 屬性來指定約會的發(fā)件人。但是,如果我不使用Outlook里帳戶設(shè)置的帳戶集合,而要指定其它的郵件帳戶來發(fā)送郵件時該怎么弄?到現(xiàn)在也沒有找到或發(fā)現(xiàn)辦法,希望知道的達(dá)人們能
指點(diǎn)一下門路,拜謝先~~~~
下面是測試的代碼,在WIN2003+OFFICE12下運(yùn)行通過,成功創(chuàng)建會議:
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 - using Microsoft.Office.Interop.Outlook;
 - ////////////////////
 - /* 調(diào)用Outlook api 發(fā)起會議
 - /* mcjeremy@cnblogs.com
 - ////////////////////
 - namespace OutlookAPI
 - {
 - class Program
 - {
 - static void Main(string[] args)
 - {
 - try
 - {
 - ApplicationClass oApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
 - //會議是約會的一種
 - AppointmentItem oItem = (AppointmentItem)oApp.CreateItem(OlItemType.olAppointmentItem);
 - oItem.MeetingStatus = OlMeetingStatus.olMeeting;
 - oItem.Subject = "主題";
 - oItem.Body = "內(nèi)容";
 - oItem.Location = "地點(diǎn)";
 - //開始時間
 - oItem.Start = DateTime.Now.AddDays(1);
 - //結(jié)束時間
 - oItem.End = DateTime.Now.AddDays(2);
 - //提醒設(shè)置
 - oItem.ReminderSet = true;
 - oItem.ReminderMinutesBeforeStart = 5;
 - //是否全天事件
 - oItem.AllDayEvent = false;
 - oItem.BusyStatus = OlBusyStatus.olBusy;
 - //索引從1開始,而不是從0
 - //發(fā)件人的帳號信息
 - oItem.SendUsingAccount = oApp.Session.Accounts[2];
 - //添加必選人
 - Recipient force = oItem.Recipients.Add("mailuser2@mailserver.com");
 - force.Type = (int)OlMeetingRecipientType.olRequired;
 - //添加可選人
 - Recipient opt = oItem.Recipients.Add("mailuser3@p.mailserver.com");
 - opt.Type = (int)OlMeetingRecipientType.olOptional;
 - //添加會議發(fā)起者
 - Recipient sender = oItem.Recipients.Add("mailuser1@mailserver.com");
 - sender.Type = (int)OlMeetingRecipientType.olOrganizer;
 - oItem.Recipients.ResolveAll();
 - //oItem.SaveAs("d:/TEST.MSG", OlSaveAsType.olMSG);
 - oItem.Send();
 - //MailItem mItem = (MailItem)oApp.CreateItem(OlItemType.olMailItem);
 - //Recipient rTo = mItem.Recipients.Add("****");
 - //rTo.Type = (int)OlMailRecipientType.olTo;
 - //Recipient rCC=mItem.Recipients.Add("****");
 - //rCC.Type = (int)OlMailRecipientType.olCC;
 - //Recipient rBC = mItem.Recipients.Add("****");
 - //rBC.Type = (int)OlMailRecipientType.olBCC;
 - Console.WriteLine("OK");
 - }
 - catch (System.Exception ex)
 - {
 - Console.WriteLine(ex.Message);
 - }
 - Console.ReadLine();
 - }
 - }
 - }
 
以上介紹C#調(diào)用Outlook API
【編輯推薦】















 
 
 
 
 
 
 