切换到宽版
  • 11031阅读
  • 3回复

[分享]Exchange Server 2007 二次开发 (二) [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2011-02-19
关键词: exchange
能过Exchange Server 2007 二次开发(一),相信对Exchange Server 2007二次开发有了初步的了解,在本文中,是我在项目中对EWS的具体应用。
      要使用EWS,首先要设置ExchangeServiceBinding代理类,如下:
            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.Url = EWS地址;
            esb.Credentials = new NetworkCredential(用户名, 密码, 域名);
            这样我们就创建了该用户的代理,通过这个代理可以进行收发邮件、预定会议、应答会议等。
            也可以用这种方式创建esb.Credentials=CredentialCache.DefaultCredentials,这样为当前登录系统的账户创建代理。
以下是一些具体的应用示例:
创建普通邮件:
1publicstaticvoidCreateEmail(ExchangeServiceBinding esb)
2        {
3            createItemRequest =newCreateItemType();
4            createItemRequest.MessageDisposition =MessageDispositionType.SendAndSaveCopy;
5            createItemRequest.MessageDispositionSpecified =true;
6            //Specify the location of sent items. 7createItemRequest.SavedItemFolderId =newTargetFolderIdType();
8            DistinguishedFolderIdType sentitems =newDistinguishedFolderIdType();
9            sentitems.Id =DistinguishedFolderIdNameType.sentitems;
10            createItemRequest.SavedItemFolderId.Item =sentitems;
11            //Create the array of items.12createItemRequest.Items =newNonEmptyArrayOfAllItemsType();
13            //Create a single e-mail message.14MessageType message =newMessageType();
15            message.Subject ="test";
16            message.Body =newBodyType();
17            message.Body.BodyType1 =BodyTypeType.Text;    
18            message.Body.Value ="this is a test mail from exchange services";
19            message.ItemClass ="IPM.Note";
20            message.From =newSingleRecipientType();
21            message.From.Item =newEmailAddressType();
22            message.ToRecipients =newEmailAddressType[1];
23            message.ToRecipients[0] =newEmailAddressType();            
24message.ToRecipients[0].EmailAddress ="收件人地址";
25            message.Sensitivity =SensitivityChoicesType.Confidential;
26            message.Importance =ImportanceChoicesType.High;
27            //Add the message to the array of items to be created.28createItemRequest.Items.Items =newItemType[1];
29            createItemRequest.Items.Items[0] =message;
30            try31[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]            {
32[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                //Send the request to create and send the e-mail item, and get the response.33[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]CreateItemResponseType createItemResponse =esb.CreateItem(createItemRequest);
34[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                //Determine whether the request was a success.35[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]if(createItemResponse.ResponseMessages.Items[0].ResponseClass ==ResponseClassType.Error)
36[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]                {
37[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                    thrownewException(createItemResponse.ResponseMessages.Items[0].MessageText);
38[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]                }
39[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                else40[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]                {
41[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                    Console.WriteLine("Item was created");
42[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]                }
43[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
44[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]            }
45[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]            catch(Exception e)
46[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]            {
47[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]                Console.WriteLine(e.Message);
48[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]            }
49[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
50[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]        }

[ 此帖被tata在2011-02-19 14:36重新编辑 ]
分享到
只看该作者 1楼  发表于: 2011-03-09
Exchange的开发要好好学,公司也有这样的需求。
只看该作者 2楼  发表于: 2011-12-19
好东西啊,可惜我的技术还没到那个程度
只看该作者 3楼  发表于: 2013-02-20
快速回复
限60 字节
 
上一个 下一个