非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services,最近的项目中为了使用短信网关提供邮件短信提醒先,打开 VS2008 -> “Add Web Reference” -> ???? ,那个是Exchange2007的Service,我的是90天试用版本的Pro,直接输入:http://mailsrv 提示验证失败,找不到任何东西,烦了半天,还以为VS装坏了,在mailsrv的IIS里头发现了:
http(s)://MyServer/ews/services.wsdl
http(s)://MyServer/EWS/exchange.asmx?wsdl
嘿嘿,找到了,后面的地址最后都会跳到/ews/services.wsdl,这份契约里会列出所有的实用方法,读写邮件,日历,约会,会议等等,这下好办了,取出Inbox里头最新的未读邮件,顺便到AD里头把电话号码捞过来,再顺便给短信猫的代理:
CMS.dll ,OK,手机呼拉呼拉,收到邮件提醒,内容大致为邮件标题和发送时间和重要性!你不习惯邮件也不行啊,流程催办,否则年终不发奖金,最后都习惯用Email了,我们的生意又来了。。。。。
顺便看看我最初的版本,很烂很适用:
….
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
ICredentials creds = new NetworkCredential(“pccai”, “password”, “abc”);
exchangeServer.Credentials = creds;
exchangeServer.Url = @”https://mailsrv/ews/exchange.asmx”;
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
pteftComment.PropertyTag = “0x3004”; // PR_COMMENT
pteftComment.PropertyType = MapiPropertyTypeType.String;
GetFolderType myfoldertype = new GetFolderType();
myfoldertype.FolderIds = folderIDArray;
myfoldertype.FolderShape = new FolderResponseShapeType();
myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
FolderInfoResponseMessageType firmtInbox =
(FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
//Response.Write(string.Format(“got folder: {0}”, firmtInbox.Folders[0].DisplayName));
//if (null != firmtInbox.Folders[0].ExtendedProperty)
//{
// Response.Write(string.Format(“Comment: {0}”, firmtInbox.Folders[0].ExtendedProperty[0].Item.ToString()));
//}
//else
//{
// Response.Write(“Comment: not found”);
//}
PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
PathToUnindexedFieldType ptuftBody= new PathToUnindexedFieldType();
ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
pteftFlagStatus.PropertyTag = “0x1090”; // PR_FLAG_STATUS
pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
findItemRequest.ItemShape = new ItemResponseShapeType();
findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
//findItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[2];
//findItemRequest.ItemShape.AdditionalProperties[0] = ptuftSubject;
//findItemRequest.ItemShape.AdditionalProperties[1] = pteftFlagStatus;
/**/////findItemRequest.ItemShape.AdditionalProperties[2] = ptuftBody;
findItemRequest.ParentFolderIds = new FolderIdType[1];
findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
{
if (firmtMessage.RootFolder.TotalItemsInView > 0)
{
foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
{
Response.Write(string.Format(“标题: {0} <br>”, it.Subject));
Response.Write(string.Format(“发件人: {0} <br>”, ((cn.cwe.mail.MessageType)(it)).From.Item.Name));
Response.Write(string.Format(“收件人: {0} <br>”, it.DisplayTo));
Response.Write(string.Format(“抄送人: {0} <br>”, it.DisplayCc));
Response.Write(string.Format(“大小: {0} <br>”, it.Size.ToString()));
Response.Write(string.Format(“重要性: {0} <br>”, it.Importance.ToString()));
Response.Write(string.Format(“是否已读: {0} <br>”, ((cn.cwe.mail.MessageType)(it)).IsRead.ToString()));
Response.Write(string.Format(“附件: {0} <br>”, it.HasAttachments.ToString()));
Response.Write(string.Format(“接收时间: {0} <br>”, it.DateTimeReceived.ToString()));
if (it.Body != null)
{
Response.Write(string.Format(“正文: {0} <br>”, it.Body.Value));
}
if (null != it.ExtendedProperty)
{
Response.Write(string.Format(“Prop 0x1090: {0}\n\n”, it.ExtendedProperty[0].Item.ToString()));
}
else
{
Response.Write(string.Format(“Prop 0x1090: not found”));
}
}
}
比起 Exchange2003那复杂的Exchange-SQL ,这个版本的Services规范多了,比如:
findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
给一个准备查询动作做限制,也可以是OnlyId,Default,我们只取我们想要的数据,类似 Select * from XXX的问题,这里不细细提出,同时还有:
PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
不同的Path,不同的“字段”,太妙了 …..
最后想题下那个短信猫代理dll,好几百RMB,不知有没有绿色版本的!当然了上面的做法其实用2007的POP3协议可以不用代码的,但如果想在MOSS的webpart中取出展示还是方便些。。。。。