设为首页 收藏本站
查看: 796|回复: 0

[经验分享] Programming Outlook with C#

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-13 08:09:09 | 显示全部楼层 |阅读模式
  Microsoft Office provides a powerful component model to automate from another program. Using this object model, you can access Mail Items, Calendar Items, Journal Entries, and any other item that Outlook normally exposes. Using COM Interop you can automate Outlook from a .NET application. In this article we'll look at different approaches to working with Outlook folders, and walk through creating items in Outlook with C#.
Article Contents


  
System Requirements and Assumptions
The Outlook Object Model
Creating the Interop Assemblies
Starting a Session
Working with Folders
Outlook Items
Creating an Email
Creating a Contact
Conclusion
  
System Requirements and Assumptions
  You will need the following software to use the code in this article.

  • Windows 2000 or XP
  • Office 2000
  • .Net Framework SDK
  Im going to assume that you have a fair grasp of C#.
The Outlook Object Model
  The Outlook object model provides a few key objects described in the table below.

  
Application ObjectThis is the root object
Namespace ObjectThis object controls Sessions, Folders, and Items among other things
Explorer ObjectThe window displaying a folder
Inspector ClassThe window displaying an item
  
Creating the Interop Assemblies
  In Visual Studio .NET you can just click add reference, then browse to the libraries you want to import. Specifically MSOUTL9.OLB for Outlook. If youre using the SDK then you will do this from the command line using the Type Library Import Tool tblimp.exe.

To use the Type Library Importer from the command line. Create a new directory in a location of your choosing called OfficeSample. From the command prompt change to the C:\Program Files\Microsoft.NET\FrameworkSDK\bin directory and then type: tlbimp "C:\Program Files\Microsoft Office\Office\msoutl9.olb" /out:"C:\Office Sample\msoutl9.dll"
    This will create an interop dll for Outlook.
Starting a Session
  The first step we need to take to automate Outlook is to create an Outlook session. To do this you need to create a reference to the Outlook Interop assembly, and select the profile you wish to work with.

  
  msoutl9.Application objOutlook = new msoutl9.ApplicationClass();
msoutl9.NameSpace objNS = objOutlook.GetNamespace("MAPI");
objNS.Logon ("exchtest","net",false,true);
  

  
  msoutl9.Application objOutlook = new msoutl9.ApplicationClass();
  
  This line creates the Outlook object, next you need to call getNamespace, the only parameter you can pass to it is MAPI.

  
msoutl9.NameSpace objNS = objOutlook.GetNamespace("MAPI");
  
  Finally, we call the logon Method. This will open the profile we want to work with.

  
objNS.Logon ("exchtest","net",false,true);
  
  The Following table describes the arguments of the logon method.

  
NameDataType  Description
ProfilestringThis is the name of the profile you want to log into. Leaving it blank uses the default profile.
PasswordstringThe password for the profile, leaving it blank uses the default profiles password.
ShowDialogBooleanDetermines whether the Outlook profile dialog will be displayed
NewSessionBooleanDetermines if a new session will be created or an existing one used
  
  You can also bypass the logon method, and the current profile and session will be used. However, you can not have multiple sessions in Outlook.
Working with Folders
  Outlook stores everything in folders, and each folder can be set to hold only a certain kind of data. They can be accessed through the GetDefaultFolders Method of the Namespace object. There are several types of folder listed below with there constant values.

  
Folder NameConstant
CalendarOlDefaultFolders.olFolderCalendar
Contacts OlDefaultFolders.olFolderContacts
Deleted ItemsOlDefaultFolders.olFolderDeletedItems
DraftsOlDefaultFolders.olFolderDrafts
InboxOlDefaultFolders.olFolderInbox
Journal OlDefaultFolders.olFolderJournal
NotesOlDefaultFolders.olFolderNotes
Outbox OlDefaultFolders.olFolderOutbox
Sent ItemsOlDefaultFolders.olFolderSentItems
TasksOlDefaultFolders.olFolderTasks
  
  To get to the Inbox Folder the code would be.

  
  msoutl9.Explorer objExplorer = objOutlook.Explorers.Add(objFolder.GetDefaultFolder(OlDefaultFolders.olFolderInbox), OlFolderDisplayMode.olFolderDisplayNormal);
objExplorer.Activate();
  
  We can also navigate folders using the index value of the Folders Collection.

  
for (int i=1; i <= objFolders.Count; i++)
{
Console.WriteLine(objFolders.Item(i).Name);
}
  
  Or you could use foreach, this comes in handy for user created folders, and exchange public folders. Here is an example of a simple search function using foreach that will return true if a folder exists, and false otherwise.

  
public bool FindFolder(string strFolderName)
{
     msoutl9.MAPIFolder objFolder;
     foreach (objfolder in objNS.Folders)
     {
          if (objfolder.Name == strFolderName)
          {
               return true;
           }
     }
     return false;
}
  
Outlook Items
  Outlook offers several different types of items that correlate to the default Outlook folders. As with the description of folders above, following is a table containing Outlook Item names, and their constants.

  
ItemConstant
Appointment OlItemType.olAppointmentItem
ContactOlItemType.olContactItem
Distribution ListOlItemType.olDistributionListItem
Journal ItemOlItemType.olJournalItem
MailOlItemType.olMailItem
NoteOlItemType.olNoteItem
PostOlItemType.olPostItem
TaskOlItemType.olTaskItem
  
  You can create Items using the CreateItem method of the Application Object, or by using the Add method of the of the Items Collection of a given folder.
  Using the CreateItem method the code would look like the following.

  
msoutl9.MailItem objMail = (msoutl9.MailItem) objOutlook.CreateItem(OlItemType.olMailItem);
  
  Or using the Add method it would look like this.

  
msoutl9.MailItem objMail;
ObjInbox.Items.Add(objMail);
  
  I'll use CreateItem for the remainder of the article.
Creating an Email
  The most common task in Outlook is creating an email. You can automate this task from a .NET application easily. You can also automate the process of adding attachments. Here's an excerpt of the sample code.

  
msoutl9.MailItem objMail = (msoutl9.MailItem) objOutlook.CreateItem(OlItemType.olMailItem);
objMail.To = "user@localhost";
objMail.Subject = "new email";
objMail.Body = "I am your new email message";
  
  You can either save the email to your drafts folder using the save method.

  
objMail.Save();
  
  Or you can put it directly in the Outbox using the Send method.

  
objMail.Send();
  
  If you want to save the email to a user created folder, create a reference to the folder you wish to work with, and use the Add method.
Creating a Contact
  Just like in the email example above, we create a new Outlook item. This time it as a Contact.

  
msoutl9.ContactItem objContact = (msoutl9.ContactItem)
objOutlook.CreateItem(OlItemType.olContactItem);
  
  Then we fill in the Contact information.

  
objContact.FirstName = "Joe";
objContact.LastName = "Smith";
objContact.MailingAddressStreet = "123 Some St.";
objContact.MailingAddressCity = "Anytown";
objContact.MailingAddressState = "CA";
objContact.MailingAddressPostalCode = "12345";
objContact.MailingAddressCountry = "USA";
objContact.CompanyName = "Acme Inc.";
objContact.Email1Address = "user@localhost.com";
objContact.Email1AddressType = "SMTP";
< /FONT >
  
  And finally we save the information to our contacts folder.

  
objContact.Save();
  
Conclusion
  Aside from those discussed in this article, there are many other properties and methods that are exposed by the object availble through the Outlook programming model some of which you will find in the articles example code.  C# and COM interop allow us as developers to work with Outlook and many other existing programs in familiar ways. For more information of programming with the Outlook object model, see the documentation on http://msdn.microsoft.com/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112831-1-1.html 上篇帖子: Outlook Express备份信件 下篇帖子: 利用Outlook發送郵件(l轉)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表