.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,如果是在公司的内部域中,添加的过程中要求输入你的域帐号和密码。
2 添加成功以后再项目中导入该命名空间以其他必须的命名空间;
3 下面就是具体的代码,具体的请看注释:
发送邮件:
1 public static void CreateEmail(string userName, string passWord, string domain, string url, string mailFrom, string mailTo)
2 {
3 // Create service binding.
4 // 建立service绑定
5 ExchangeServiceBinding esb = new ExchangeServiceBinding();
6 esb.Credentials = new NetworkCredential(userName, passWord, domain);
7 esb.Url = url;
8
9 // Create the CreateItem request.
10 CreateItemType createItemRequest = new CreateItemType();
11
12 // Specifiy how the created items are handled
13 // 如何处理邮件
14 createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
15 createItemRequest.MessageDispositionSpecified = true;
16
17 // Specify the location of sent items.
18 createItemRequest.SavedItemFolderId = new TargetFolderIdType();
19 DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
20 sentitems.Id = DistinguishedFolderIdNameType.sentitems;
21 createItemRequest.SavedItemFolderId.Item = sentitems;
22
23 // Create the array of items.
24 createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
25
26 // Create a single e-mail message.
27 // 新建一封邮件message对象
28 MessageType message = new MessageType();
29 message.Subject = "Tommy Test";
30 message.Body = new BodyType();
31 message.Body.BodyType1 = BodyTypeType.Text;
32 message.Body.Value = "This is tommy's test use exchange webservice";
33 message.ItemClass = "IPM.Note";
34 message.Sender = new SingleRecipientType();
35 message.Sender.Item = new EmailAddressType();
36 message.Sender.Item.EmailAddress = mailFrom;
37 message.ToRecipients = new EmailAddressType[1];
38 message.ToRecipients[0] = new EmailAddressType();
39 message.ToRecipients[0].EmailAddress = mailTo;
40 message.Sensitivity = SensitivityChoicesType.Normal;
41
42 // Add the message to the array of items to be created.
43 createItemRequest.Items.Items = new ItemType[1];
44 createItemRequest.Items.Items[0] = message;
45
46 try
47 {
48 // Send the request to create and send the e-mail item, and get the response.
49 CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);
50
51 // Determine whether the request was a success.
52 if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
53 {
54 throw new Exception(createItemResponse.ResponseMessages.Items[0].MessageText);
55 }
56 else
57 {
58 Console.WriteLine("Item was created");
59 Console.ReadLine();
60 }
61 }
62 catch (Exception e)
63 {
64 Console.WriteLine(e.Message);
65 Console.ReadLine();
66 }
67 }
抓取以及删除邮件:
1 public static void GetMailMessage(string userName, string passWord, string domain, string url)
2 {
3 // 绑定exchange服务器
4 ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
5 ICredentials creds = new NetworkCredential(userName, passWord, domain);
6
7 // 建立信任连接
8 exchangeServer.Credentials = creds;
9 exchangeServer.Url = url;
10
11 // 定义邮件的收件箱
12 DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
13 folderIDArray[0] = new DistinguishedFolderIdType();
14 folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
15
16 PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
17 ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
18
19 PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
20 pteftComment.PropertyTag = "0x3004"; // PR_COMMENT
21 pteftComment.PropertyType = MapiPropertyTypeType.String;
22
23 // 定义GetFolderType对象,设置相应属性
24 GetFolderType myfoldertype = new GetFolderType();
25 myfoldertype.FolderIds = folderIDArray;
26 myfoldertype.FolderShape = new FolderResponseShapeType();
27 myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
28 myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
29 myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
30 myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
31
32 // 获取服务器中的文件夹的集合
33 GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
34
35 // 获取收件箱
36 FolderInfoResponseMessageType firmtInbox =
37 (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
38
39 // 显示收件箱
40 Console.WriteLine(string.Format("got folder: {0}", firmtInbox.Folders[0].DisplayName));
41
42
43 PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
44 ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
45
46 PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
47 ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
48
49 PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
50 pteftFlagStatus.PropertyTag = "0x1090"; // PR_FLAG_STATUS
51 pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
52
53 // 定义FindItemType对象,准备获取收件箱中的集合
54 FindItemType findItemRequest = new FindItemType();
55 findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
56 findItemRequest.ItemShape = new ItemResponseShapeType();
57 findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
58
59
60 findItemRequest.ParentFolderIds = new FolderIdType[1];
61 findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
62
63 // 获取邮件
64 FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
65
66 // 循环迭代每一封邮件
67 foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
68 {
69 // 如果包含邮件,显示出来
70 if (firmtMessage.RootFolder.TotalItemsInView > 0)
71 {
72 // 循环迭代每一封邮件详细信息
73 foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
74 {
75 Console.WriteLine(string.Format("邮件标题: {0} ", it.Subject));
76 Console.WriteLine(string.Format("发件人: {0} ",((MessageType)(it)).From.Item.Name));
77 Console.WriteLine(string.Format("收件人: {0} ", it.DisplayTo));
78 Console.WriteLine(string.Format("抄送: {0} ", it.DisplayCc));
79 Console.WriteLine(string.Format("大小: {0} ", it.Size.ToString()));
80 Console.WriteLine(string.Format("重要性: {0} ", it.Importance.ToString()));
81 Console.WriteLine(string.Format("是否已读: {0} ", ((MessageType)(it)).IsRead.ToString()));
82 Console.WriteLine(string.Format("是否有附件: {0} ", it.HasAttachments.ToString()));
83 //Console.WriteLine(string.Format("发送时间:{0}", it.DateTimeSent.ToString()));
84 Console.WriteLine(string.Format("接收时间: {0} ", it.DateTimeReceived.ToString()));
85
86
87 // 通过GetItemType对象来得到邮件的正文
88 GetItemType getItemRequest = new GetItemType();
89 // 设置必要的属性
90 getItemRequest.ItemIds = new BaseItemIdType[1];
91 getItemRequest.ItemIds[0] = (BaseItemIdType)it.ItemId;
92 getItemRequest.ItemShape = new ItemResponseShapeType();
93 getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
94 getItemRequest.ItemShape.IncludeMimeContent = true;
95
96 // 获得服务器的相应
97 GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemRequest);
98
99 // 得到邮件体
100 ItemInfoResponseMessageType getItemResponseMessage =
101 getItemResponse.ResponseMessages.Items[0] as
102 ItemInfoResponseMessageType;
103
104 // 显示邮件正文
105 Console.WriteLine("邮件格式:{0}", getItemResponseMessage.Items.Items[0].Body.BodyType1);
106 Console.WriteLine(string.Format("正文:{0}", getItemResponseMessage.Items.Items[0].Body.Value));
107
108 // 获取当前邮件的附件集合
109 if (getItemResponseMessage.Items.Items[0].HasAttachments)
110 {
111 AttachmentType[] attachments = getItemResponseMessage.Items.Items[0].Attachments;
112
113 // 循环获取当前邮件的每一个附件
114 for (int i = 0; i <= attachments.Length - 1;i++ )
115 {
116 // 定义GetAttachmentType,设置相应的属性,进行模式验证来获取附件
117 GetAttachmentType getAttachment = new GetAttachmentType();
118 RequestAttachmentIdType[] attachmentIDArry = new RequestAttachmentIdType[1];
119 attachmentIDArry[0] = new RequestAttachmentIdType();
120 attachmentIDArry[0].Id = attachments[i].AttachmentId.Id;
121 getAttachment.AttachmentIds = attachmentIDArry;
122 getAttachment.AttachmentShape = new AttachmentResponseShapeType();
123
124 // 获取附件
125 GetAttachmentResponseType getAttachmentResponse = exchangeServer.GetAttachment(getAttachment);
126
127 // 返回服务器的响应对象
128 AttachmentInfoResponseMessageType responseMessage = getAttachmentResponse.ResponseMessages.Items[0] as AttachmentInfoResponseMessageType;
129 if (responseMessage != null)
130 {
131 // 没有异常,显示附件信息
132 if (responseMessage.ResponseClass == ResponseClassType.Success && responseMessage.Attachments != null
133 && responseMessage.Attachments.Length > 0)
134 {
135 AttachmentType attachment = responseMessage.Attachments[0];
136 Console.WriteLine("附件名:{0}\n", attachment.Name);
137 //Console.WriteLine("附件类型:{0}\n", attachment.ContentId);
138 }
139 }
140 }
141 }
142
143 // 删除当前邮件
144 DeleteItemType deleteItem = new DeleteItemType();
145 deleteItem.ItemIds = new BaseItemIdType[1];
146
147 // 将要删除邮件的ID,changekey赋值给ItemIdType对象
148 ItemIdType itemIDType = new ItemIdType();
149 itemIDType.Id = it.ItemId.Id;
150 itemIDType.ChangeKey = it.ItemId.ChangeKey;
151
152 deleteItem.ItemIds[0] = itemIDType;
153
154 // 执行删除
155 DeleteItemResponseType deleteResponse = exchangeServer.DeleteItem(deleteItem);
156
157 // 删除成功
158 if (deleteResponse.ResponseMessages.Items.Length > 0 && deleteResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
159 {
160 Console.WriteLine("删除成功");
161 }
162
163 }
164 }
165
166 }
167 }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/oyjd614/archive/2008/08/01/2754104.aspx