.NET中使用Exchange 2007 Webservice来读取邮件

很久没有更新过博客了,这几个月比较忙,人也比较懒散,今天已经是八月份,新的一个月希望要换一个状态。今天要写的文章是我四月份写的一个功能,一直拖到现在才发表。在企业内部我们一般都是使用微软的Exchange服务器来进行邮件的存储及发送,我们一般在客户端用outlook来收发邮件。工作中遇到了一个需求,需要写一个程序来抓取邮件服务器的邮件,并将抓取下来的邮件存储到数据库中。之前已经发表过一篇文章,是利用Jmail组件来抓取,可以抓取我们常用的一些邮箱的邮件,比如说163,gmail等,但是不能抓取exchange服务器中的邮件,所以需要重新写,最后决定使用Exchange 2007 中提供的webservice来抓取邮件。网络上这方面的资料很少,所以大部分的参考是来自MSDN:

http://msdn.microsoft.com/en-us/library/exchangewebservices(EXCHG.80).aspx

2007版的Exchange服务器提供了webservice,我们可以方便的在.NET程序中添加该引用,然后就可以方便的实现邮件的抓取,发送,删除等功能。我测试是在控制台程序中使用该webservice,步骤如下:

1 新建一个站点,然后添加exchange webservice 的引用,输入邮件服务器的OWA地址:http://yourmailserver/EWS/Services.wsdl,如果是在公司的内部域中,添加的过程中要求输入你的域帐号和密码。

.NET中使用Exchange 2007 Webservice来读取邮件

 

 

 

.NET中使用Exchange 2007 Webservice来读取邮件

2 添加成功以后再项目中导入该命名空间以其他必须的命名空间;

.NET中使用Exchange 2007 Webservice来读取邮件

 

3 下面就是具体的代码,具体的请看注释:

发送邮件:

 

 1.NET中使用Exchange 2007 Webservice来读取邮件        public static void CreateEmail(string userName, string passWord, string domain, string url, string mailFrom, string mailTo)
 2.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件        .NET中使用Exchange 2007 Webservice来读取邮件
{
 3.NET中使用Exchange 2007 Webservice来读取邮件            //
 Create service binding.
 4.NET中使用Exchange 2007 Webservice来读取邮件            // 建立service绑定

 5.NET中使用Exchange 2007 Webservice来读取邮件            ExchangeServiceBinding esb = new ExchangeServiceBinding();
 6.NET中使用Exchange 2007 Webservice来读取邮件            esb.Credentials = new
 NetworkCredential(userName, passWord, domain);
 7.NET中使用Exchange 2007 Webservice来读取邮件            esb.Url =
 url;
 8
.NET中使用Exchange 2007 Webservice来读取邮件
 9.NET中使用Exchange 2007 Webservice来读取邮件            // Create the CreateItem request.

10.NET中使用Exchange 2007 Webservice来读取邮件            CreateItemType createItemRequest = new CreateItemType();
11
.NET中使用Exchange 2007 Webservice来读取邮件
12.NET中使用Exchange 2007 Webservice来读取邮件            //
 Specifiy how the created items are handled
13.NET中使用Exchange 2007 Webservice来读取邮件            // 如何处理邮件

14.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
15.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.MessageDispositionSpecified = true
;
16
.NET中使用Exchange 2007 Webservice来读取邮件
17.NET中使用Exchange 2007 Webservice来读取邮件            // Specify the location of sent items. 

18.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.SavedItemFolderId = new TargetFolderIdType();
19.NET中使用Exchange 2007 Webservice来读取邮件            DistinguishedFolderIdType sentitems = new
 DistinguishedFolderIdType();
20.NET中使用Exchange 2007 Webservice来读取邮件            sentitems.Id =
 DistinguishedFolderIdNameType.sentitems;
21.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.SavedItemFolderId.Item =
 sentitems;
22
.NET中使用Exchange 2007 Webservice来读取邮件
23.NET中使用Exchange 2007 Webservice来读取邮件            // Create the array of items.

24.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
25
.NET中使用Exchange 2007 Webservice来读取邮件
26.NET中使用Exchange 2007 Webservice来读取邮件            //
 Create a single e-mail message.
27.NET中使用Exchange 2007 Webservice来读取邮件            // 新建一封邮件message对象

28.NET中使用Exchange 2007 Webservice来读取邮件            MessageType message = new MessageType();
29.NET中使用Exchange 2007 Webservice来读取邮件            message.Subject = "Tommy Test"
;
30.NET中使用Exchange 2007 Webservice来读取邮件            message.Body = new
 BodyType();
31.NET中使用Exchange 2007 Webservice来读取邮件            message.Body.BodyType1 =
 BodyTypeType.Text;
32.NET中使用Exchange 2007 Webservice来读取邮件            message.Body.Value = "This is tommy's test use exchange webservice"
;
33.NET中使用Exchange 2007 Webservice来读取邮件            message.ItemClass = "IPM.Note"
;
34.NET中使用Exchange 2007 Webservice来读取邮件            message.Sender = new
 SingleRecipientType();
35.NET中使用Exchange 2007 Webservice来读取邮件            message.Sender.Item = new
 EmailAddressType();
36.NET中使用Exchange 2007 Webservice来读取邮件            message.Sender.Item.EmailAddress =
 mailFrom;
37.NET中使用Exchange 2007 Webservice来读取邮件            message.ToRecipients = new EmailAddressType[1
];
38.NET中使用Exchange 2007 Webservice来读取邮件            message.ToRecipients[0= new
 EmailAddressType();
39.NET中使用Exchange 2007 Webservice来读取邮件            message.ToRecipients[0].EmailAddress =
 mailTo;
40.NET中使用Exchange 2007 Webservice来读取邮件            message.Sensitivity =
 SensitivityChoicesType.Normal;
41
.NET中使用Exchange 2007 Webservice来读取邮件
42.NET中使用Exchange 2007 Webservice来读取邮件            // Add the message to the array of items to be created.

43.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.Items.Items = new ItemType[1];
44.NET中使用Exchange 2007 Webservice来读取邮件            createItemRequest.Items.Items[0=
 message;
45
.NET中使用Exchange 2007 Webservice来读取邮件
46.NET中使用Exchange 2007 Webservice来读取邮件            try

47.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件            .NET中使用Exchange 2007 Webservice来读取邮件{
48.NET中使用Exchange 2007 Webservice来读取邮件                // Send the request to create and send the e-mail item, and get the response.

49.NET中使用Exchange 2007 Webservice来读取邮件                CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);
50
.NET中使用Exchange 2007 Webservice来读取邮件
51.NET中使用Exchange 2007 Webservice来读取邮件                // Determine whether the request was a success.

52.NET中使用Exchange 2007 Webservice来读取邮件                if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
53.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                .NET中使用Exchange 2007 Webservice来读取邮件
{
54.NET中使用Exchange 2007 Webservice来读取邮件                    throw new Exception(createItemResponse.ResponseMessages.Items[0
].MessageText);
55.NET中使用Exchange 2007 Webservice来读取邮件                }

56.NET中使用Exchange 2007 Webservice来读取邮件                else
57.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                .NET中使用Exchange 2007 Webservice来读取邮件{
58.NET中使用Exchange 2007 Webservice来读取邮件                    Console.WriteLine("Item was created"
);
59
.NET中使用Exchange 2007 Webservice来读取邮件                    Console.ReadLine();
60.NET中使用Exchange 2007 Webservice来读取邮件                }

61.NET中使用Exchange 2007 Webservice来读取邮件            }

62.NET中使用Exchange 2007 Webservice来读取邮件            catch (Exception e)
63.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件            .NET中使用Exchange 2007 Webservice来读取邮件
{
64
.NET中使用Exchange 2007 Webservice来读取邮件                Console.WriteLine(e.Message);
65
.NET中使用Exchange 2007 Webservice来读取邮件                Console.ReadLine();
66.NET中使用Exchange 2007 Webservice来读取邮件            }

67.NET中使用Exchange 2007 Webservice来读取邮件        }

 

抓取以及删除邮件:

 

  1.NET中使用Exchange 2007 Webservice来读取邮件 public static void GetMailMessage(string userName, string passWord, string domain, string url)
  2.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件        .NET中使用Exchange 2007 Webservice来读取邮件
{
  3.NET中使用Exchange 2007 Webservice来读取邮件            // 绑定exchange服务器

  4.NET中使用Exchange 2007 Webservice来读取邮件            ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
  5.NET中使用Exchange 2007 Webservice来读取邮件            ICredentials creds = new
 NetworkCredential(userName, passWord, domain);
  6
.NET中使用Exchange 2007 Webservice来读取邮件
  7.NET中使用Exchange 2007 Webservice来读取邮件            // 建立信任连接

  8.NET中使用Exchange 2007 Webservice来读取邮件            exchangeServer.Credentials = creds;
  9.NET中使用Exchange 2007 Webservice来读取邮件            exchangeServer.Url =
 url;
 10
.NET中使用Exchange 2007 Webservice来读取邮件
 11.NET中使用Exchange 2007 Webservice来读取邮件            // 定义邮件的收件箱

 12.NET中使用Exchange 2007 Webservice来读取邮件            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
 13.NET中使用Exchange 2007 Webservice来读取邮件            folderIDArray[0= new
 DistinguishedFolderIdType();
 14.NET中使用Exchange 2007 Webservice来读取邮件            folderIDArray[0].Id =
 DistinguishedFolderIdNameType.inbox;
 15
.NET中使用Exchange 2007 Webservice来读取邮件
 16.NET中使用Exchange 2007 Webservice来读取邮件            PathToUnindexedFieldType ptuftDisplayName = new
 PathToUnindexedFieldType();
 17.NET中使用Exchange 2007 Webservice来读取邮件            ptuftDisplayName.FieldURI =
 UnindexedFieldURIType.folderDisplayName;
 18
.NET中使用Exchange 2007 Webservice来读取邮件
 19.NET中使用Exchange 2007 Webservice来读取邮件            PathToExtendedFieldType pteftComment = new
 PathToExtendedFieldType();
 20.NET中使用Exchange 2007 Webservice来读取邮件            pteftComment.PropertyTag = "0x3004"// PR_COMMENT

 21.NET中使用Exchange 2007 Webservice来读取邮件            pteftComment.PropertyType = MapiPropertyTypeType.String;
 22
.NET中使用Exchange 2007 Webservice来读取邮件
 23.NET中使用Exchange 2007 Webservice来读取邮件            // 定义GetFolderType对象,设置相应属性

 24.NET中使用Exchange 2007 Webservice来读取邮件            GetFolderType myfoldertype = new GetFolderType();
 25.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderIds =
 folderIDArray;
 26.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderShape = new
 FolderResponseShapeType();
 27.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderShape.BaseShape =
 DefaultShapeNamesType.IdOnly;
 28.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2
];
 29.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderShape.AdditionalProperties[0=
 ptuftDisplayName;
 30.NET中使用Exchange 2007 Webservice来读取邮件            myfoldertype.FolderShape.AdditionalProperties[1=
 pteftComment;
 31
.NET中使用Exchange 2007 Webservice来读取邮件
 32.NET中使用Exchange 2007 Webservice来读取邮件            // 获取服务器中的文件夹的集合

 33.NET中使用Exchange 2007 Webservice来读取邮件            GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
 34
.NET中使用Exchange 2007 Webservice来读取邮件
 35.NET中使用Exchange 2007 Webservice来读取邮件            // 获取收件箱

 36.NET中使用Exchange 2007 Webservice来读取邮件            FolderInfoResponseMessageType firmtInbox =
 37.NET中使用Exchange 2007 Webservice来读取邮件                (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
 38
.NET中使用Exchange 2007 Webservice来读取邮件
 39.NET中使用Exchange 2007 Webservice来读取邮件            // 显示收件箱

 40.NET中使用Exchange 2007 Webservice来读取邮件            Console.WriteLine(string.Format("got folder: {0}", firmtInbox.Folders[0].DisplayName));
 41
.NET中使用Exchange 2007 Webservice来读取邮件
 42
.NET中使用Exchange 2007 Webservice来读取邮件
 43.NET中使用Exchange 2007 Webservice来读取邮件            PathToUnindexedFieldType ptuftSubject = new
 PathToUnindexedFieldType();
 44.NET中使用Exchange 2007 Webservice来读取邮件            ptuftSubject.FieldURI =
 UnindexedFieldURIType.itemSubject;
 45
.NET中使用Exchange 2007 Webservice来读取邮件
 46.NET中使用Exchange 2007 Webservice来读取邮件            PathToUnindexedFieldType ptuftBody = new
 PathToUnindexedFieldType();
 47.NET中使用Exchange 2007 Webservice来读取邮件            ptuftBody.FieldURI =
 UnindexedFieldURIType.itemAttachments;
 48
.NET中使用Exchange 2007 Webservice来读取邮件
 49.NET中使用Exchange 2007 Webservice来读取邮件            PathToExtendedFieldType pteftFlagStatus = new
 PathToExtendedFieldType();
 50.NET中使用Exchange 2007 Webservice来读取邮件            pteftFlagStatus.PropertyTag = "0x1090"// PR_FLAG_STATUS

 51.NET中使用Exchange 2007 Webservice来读取邮件            pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
 52
.NET中使用Exchange 2007 Webservice来读取邮件
 53.NET中使用Exchange 2007 Webservice来读取邮件            // 定义FindItemType对象,准备获取收件箱中的集合

 54.NET中使用Exchange 2007 Webservice来读取邮件            FindItemType findItemRequest = new FindItemType();
 55.NET中使用Exchange 2007 Webservice来读取邮件            findItemRequest.Traversal =
 ItemQueryTraversalType.Shallow;
 56.NET中使用Exchange 2007 Webservice来读取邮件            findItemRequest.ItemShape = new
 ItemResponseShapeType();
 57.NET中使用Exchange 2007 Webservice来读取邮件            findItemRequest.ItemShape.BaseShape =
 DefaultShapeNamesType.AllProperties;
 58
.NET中使用Exchange 2007 Webservice来读取邮件
 59
.NET中使用Exchange 2007 Webservice来读取邮件
 60.NET中使用Exchange 2007 Webservice来读取邮件            findItemRequest.ParentFolderIds = new FolderIdType[1
];
 61.NET中使用Exchange 2007 Webservice来读取邮件            findItemRequest.ParentFolderIds[0= firmtInbox.Folders[0
].FolderId;
 62
.NET中使用Exchange 2007 Webservice来读取邮件
 63.NET中使用Exchange 2007 Webservice来读取邮件            // 获取邮件

 64.NET中使用Exchange 2007 Webservice来读取邮件            FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
 65
.NET中使用Exchange 2007 Webservice来读取邮件
 66.NET中使用Exchange 2007 Webservice来读取邮件            // 循环迭代每一封邮件

 67.NET中使用Exchange 2007 Webservice来读取邮件            foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
 68.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件            .NET中使用Exchange 2007 Webservice来读取邮件
{
 69.NET中使用Exchange 2007 Webservice来读取邮件                // 如果包含邮件,显示出来

 70.NET中使用Exchange 2007 Webservice来读取邮件                if (firmtMessage.RootFolder.TotalItemsInView > 0)
 71.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                .NET中使用Exchange 2007 Webservice来读取邮件
{
 72.NET中使用Exchange 2007 Webservice来读取邮件                    // 循环迭代每一封邮件详细信息

 73.NET中使用Exchange 2007 Webservice来读取邮件                    foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
 74.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                    .NET中使用Exchange 2007 Webservice来读取邮件
{
 75.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("邮件标题: {0} "
, it.Subject));
 76.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("发件人: {0} "
,((MessageType)(it)).From.Item.Name));
 77.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("收件人: {0} "
, it.DisplayTo));
 78.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("抄送: {0} "
, it.DisplayCc));
 79.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("大小: {0} "
, it.Size.ToString()));
 80.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("重要性: {0} "
, it.Importance.ToString()));
 81.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("是否已读: {0} "
, ((MessageType)(it)).IsRead.ToString()));
 82.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("是否有附件: {0} "
, it.HasAttachments.ToString()));
 83.NET中使用Exchange 2007 Webservice来读取邮件                        //Console.WriteLine(string.Format("发送时间:{0}", it.DateTimeSent.ToString()));

 84.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("接收时间: {0} ", it.DateTimeReceived.ToString()));
 85
.NET中使用Exchange 2007 Webservice来读取邮件                        
 86
.NET中使用Exchange 2007 Webservice来读取邮件                        
 87.NET中使用Exchange 2007 Webservice来读取邮件                        // 通过GetItemType对象来得到邮件的正文

 88.NET中使用Exchange 2007 Webservice来读取邮件                        GetItemType getItemRequest = new GetItemType();
 89.NET中使用Exchange 2007 Webservice来读取邮件                        // 设置必要的属性

 90.NET中使用Exchange 2007 Webservice来读取邮件                        getItemRequest.ItemIds = new BaseItemIdType[1];
 91.NET中使用Exchange 2007 Webservice来读取邮件                        getItemRequest.ItemIds[0=
 (BaseItemIdType)it.ItemId;
 92.NET中使用Exchange 2007 Webservice来读取邮件                        getItemRequest.ItemShape = new
 ItemResponseShapeType();
 93.NET中使用Exchange 2007 Webservice来读取邮件                        getItemRequest.ItemShape.BaseShape =
 DefaultShapeNamesType.AllProperties;
 94.NET中使用Exchange 2007 Webservice来读取邮件                        getItemRequest.ItemShape.IncludeMimeContent = true
;
 95
.NET中使用Exchange 2007 Webservice来读取邮件
 96.NET中使用Exchange 2007 Webservice来读取邮件                        // 获得服务器的相应

 97.NET中使用Exchange 2007 Webservice来读取邮件                        GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemRequest);
 98
.NET中使用Exchange 2007 Webservice来读取邮件
 99.NET中使用Exchange 2007 Webservice来读取邮件                        // 得到邮件体

100.NET中使用Exchange 2007 Webservice来读取邮件                        ItemInfoResponseMessageType getItemResponseMessage =
101.NET中使用Exchange 2007 Webservice来读取邮件                                              getItemResponse.ResponseMessages.Items[0as
102.NET中使用Exchange 2007 Webservice来读取邮件                                              ItemInfoResponseMessageType;
103
.NET中使用Exchange 2007 Webservice来读取邮件
104.NET中使用Exchange 2007 Webservice来读取邮件                        // 显示邮件正文

105.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine("邮件格式:{0}", getItemResponseMessage.Items.Items[0].Body.BodyType1);
106.NET中使用Exchange 2007 Webservice来读取邮件                        Console.WriteLine(string.Format("正文:{0}", getItemResponseMessage.Items.Items[0
].Body.Value));
107
.NET中使用Exchange 2007 Webservice来读取邮件
108.NET中使用Exchange 2007 Webservice来读取邮件                        // 获取当前邮件的附件集合

109.NET中使用Exchange 2007 Webservice来读取邮件                        if (getItemResponseMessage.Items.Items[0].HasAttachments)
110.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                        .NET中使用Exchange 2007 Webservice来读取邮件
{
111.NET中使用Exchange 2007 Webservice来读取邮件                            AttachmentType[] attachments = getItemResponseMessage.Items.Items[0
].Attachments;
112
.NET中使用Exchange 2007 Webservice来读取邮件
113.NET中使用Exchange 2007 Webservice来读取邮件                            // 循环获取当前邮件的每一个附件

114.NET中使用Exchange 2007 Webservice来读取邮件                            for (int i = 0; i <= attachments.Length - 1;i++ )
115.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                            .NET中使用Exchange 2007 Webservice来读取邮件
{
116.NET中使用Exchange 2007 Webservice来读取邮件                                // 定义GetAttachmentType,设置相应的属性,进行模式验证来获取附件

117.NET中使用Exchange 2007 Webservice来读取邮件                                GetAttachmentType getAttachment = new GetAttachmentType();
118.NET中使用Exchange 2007 Webservice来读取邮件                                RequestAttachmentIdType[] attachmentIDArry = new RequestAttachmentIdType[1
];
119.NET中使用Exchange 2007 Webservice来读取邮件                                attachmentIDArry[0= new
 RequestAttachmentIdType();
120.NET中使用Exchange 2007 Webservice来读取邮件                                attachmentIDArry[0].Id =
 attachments[i].AttachmentId.Id;
121.NET中使用Exchange 2007 Webservice来读取邮件                                getAttachment.AttachmentIds =
 attachmentIDArry;
122.NET中使用Exchange 2007 Webservice来读取邮件                                getAttachment.AttachmentShape = new
 AttachmentResponseShapeType();
123
.NET中使用Exchange 2007 Webservice来读取邮件                                
124.NET中使用Exchange 2007 Webservice来读取邮件                                // 获取附件

125.NET中使用Exchange 2007 Webservice来读取邮件                                GetAttachmentResponseType getAttachmentResponse = exchangeServer.GetAttachment(getAttachment);
126
.NET中使用Exchange 2007 Webservice来读取邮件
127.NET中使用Exchange 2007 Webservice来读取邮件                                // 返回服务器的响应对象

128.NET中使用Exchange 2007 Webservice来读取邮件                                AttachmentInfoResponseMessageType responseMessage = getAttachmentResponse.ResponseMessages.Items[0as AttachmentInfoResponseMessageType;
129.NET中使用Exchange 2007 Webservice来读取邮件                                if (responseMessage != null
)
130.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                                .NET中使用Exchange 2007 Webservice来读取邮件
{
131.NET中使用Exchange 2007 Webservice来读取邮件                                    // 没有异常,显示附件信息

132.NET中使用Exchange 2007 Webservice来读取邮件                                    if (responseMessage.ResponseClass == ResponseClassType.Success && responseMessage.Attachments != null
133.NET中使用Exchange 2007 Webservice来读取邮件                                                           && responseMessage.Attachments.Length > 0)
134.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                                    .NET中使用Exchange 2007 Webservice来读取邮件
{
135.NET中使用Exchange 2007 Webservice来读取邮件                                        AttachmentType attachment = responseMessage.Attachments[0
];
136.NET中使用Exchange 2007 Webservice来读取邮件                                        Console.WriteLine("附件名:{0}\n"
, attachment.Name);
137.NET中使用Exchange 2007 Webservice来读取邮件                                        //Console.WriteLine("附件类型:{0}\n", attachment.ContentId);                                                    

138.NET中使用Exchange 2007 Webservice来读取邮件                                    }

139.NET中使用Exchange 2007 Webservice来读取邮件                                }

140.NET中使用Exchange 2007 Webservice来读取邮件                            }

141.NET中使用Exchange 2007 Webservice来读取邮件                        }

142.NET中使用Exchange 2007 Webservice来读取邮件
143.NET中使用Exchange 2007 Webservice来读取邮件                        // 删除当前邮件

144.NET中使用Exchange 2007 Webservice来读取邮件                        DeleteItemType deleteItem = new DeleteItemType();
145.NET中使用Exchange 2007 Webservice来读取邮件                        deleteItem.ItemIds = new BaseItemIdType[1
];
146
.NET中使用Exchange 2007 Webservice来读取邮件
147.NET中使用Exchange 2007 Webservice来读取邮件                        // 将要删除邮件的ID,changekey赋值给ItemIdType对象

148.NET中使用Exchange 2007 Webservice来读取邮件                        ItemIdType itemIDType = new ItemIdType();
149.NET中使用Exchange 2007 Webservice来读取邮件                        itemIDType.Id =
 it.ItemId.Id;
150.NET中使用Exchange 2007 Webservice来读取邮件                        itemIDType.ChangeKey =
 it.ItemId.ChangeKey;
151
.NET中使用Exchange 2007 Webservice来读取邮件
152.NET中使用Exchange 2007 Webservice来读取邮件                        deleteItem.ItemIds[0=
 itemIDType;
153
.NET中使用Exchange 2007 Webservice来读取邮件
154.NET中使用Exchange 2007 Webservice来读取邮件                        // 执行删除

155.NET中使用Exchange 2007 Webservice来读取邮件                        DeleteItemResponseType deleteResponse = exchangeServer.DeleteItem(deleteItem);
156
.NET中使用Exchange 2007 Webservice来读取邮件
157.NET中使用Exchange 2007 Webservice来读取邮件                        // 删除成功

158.NET中使用Exchange 2007 Webservice来读取邮件                        if (deleteResponse.ResponseMessages.Items.Length > 0 && deleteResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
159.NET中使用Exchange 2007 Webservice来读取邮件.NET中使用Exchange 2007 Webservice来读取邮件                        .NET中使用Exchange 2007 Webservice来读取邮件
{
160.NET中使用Exchange 2007 Webservice来读取邮件                            Console.WriteLine("删除成功"
);
161.NET中使用Exchange 2007 Webservice来读取邮件                        }

162.NET中使用Exchange 2007 Webservice来读取邮件                        
163.NET中使用Exchange 2007 Webservice来读取邮件                    }

164.NET中使用Exchange 2007 Webservice来读取邮件                }

165.NET中使用Exchange 2007 Webservice来读取邮件
166.NET中使用Exchange 2007 Webservice来读取邮件            }

167.NET中使用Exchange 2007 Webservice来读取邮件        }

转载于:https://www.cnblogs.com/chencaixia/archive/2008/08/01/1258296.html