Exchange2010-開發-建立郵件範例
一、繫結服務
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
//識別連入的使用者
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
esb.Credentials=new NetworkCredential("user1","user1","mt");
esb.Url= @"https://exjh.mt.com.tw/EWS/exchange.asmx";
二、建立CreateItem要求
// CreateItemType 代表一個SOAP訊息,將被傳遞到伺服器上以建立項目
CreateItemType createMailRequest = new CreateItemType();
// MessageDisposition 該屬性只適用於 MessageType的物件
createMailRequest.MessageDisposition = MessageDispositionType.SaveOnly;
createMailRequest.MessageDispositionSpecified = true;
createMailRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType sendItems = new DistinguishedFolderIdType();
sendItems.Id = DistinguishedFolderIdNameType.sentitems;
createMailRequest.SavedItemFolderId.Item = sendItems;
createMailRequest.Items = new NonEmptyArrayOfAllItemsType();
MessageType message = new MessageType();
message.Subject = "每日工作報表";
message.Body = new BodyType();
message.Body.BodyType1 = BodyTypeType.Text;
message.Body.Value = " 1.10/20 數學總複習班開課 \n 2.10/30 大安高中招生說明會”;
message.Sender = new SingleRecipientType(); // 寄件人
message.Sender.Item = new EmailAddressType();
message.Sender.Item.EmailAddress = "user1@exjh.mt.com.tw";
message.ToRecipients = new EmailAddressType[1];// 收件人
message.ToRecipients[0] = new EmailAddressType();
message.ToRecipients[0].EmailAddress = "user3@exjh.mt.com.tw";
message.Sensitivity = SensitivityChoicesType.Normal;
createMailRequest.Items.Items = new ItemType[1];
createMailRequest.Items.Items[0] = message;
三、送出要求並取得回應
CreateItemResponseType createItemReponse = esb.CreateItem(createMailRequest);
ArrayOfResponseMessagesType responses = createItemReponse.ResponseMessages;
ResponseMessageType[] responseMessages = responses.Items; // 回應為陣列型態
四、檢查每個回應的狀態,並轉換為Message型態。
foreach( ResponseMessageType respMsg in responseMessages ){
if( respMsg.ResponseClass == ResponseClassType.Error ){
msg.Text+= "Error:" + respMsg.MessageText + "\n";
}else if( respMsg.ResponseClass == ResponseClassType.Warning ){
msg.Text += "Warning:" + respMsg.MessageText + "\n";
}
// 檢查回應物件的型態
if( respMsg is ItemInfoResponseMessageType ){
ItemInfoResponseMessageType createItemResp = ( respMsg as ItemInfoResponseMessageType );
ArrayOfRealItemsType ary = createItemResp.Items;
// 把回應轉為正確型態
// 由於是屬於 e-mail 的 messages,因此必須把 ItemType 轉為 MessageType
foreach( ItemType item in ary.Items ){
if( item is MessageType ){
MessageType myMessage = (item as MessageType );
msg.Text+= "Item id:G" + myMessage.ItemId.Id + "\n";
}
}
}
}
五、完整程式碼
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
// 識別連入的使用者
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
esb.Credentials=new NetworkCredential("user1","user1","mt");
esb.Url= @"https://exjh.mt.com.tw/EWS/exchange.asmx";
// 提出在Exchange上建立CreateItem要求
// CreateItemType 代表一個SOAP訊息,將被傳遞到伺服器上以建立項目
CreateItemType createMailRequest = new CreateItemType();
// 指示這個e-mail項目將如何被處理
// MessageDisposition 該屬性只適用於 MessageType的物件
createMailRequest.MessageDisposition = MessageDispositionType.SaveOnly;
createMailRequest.MessageDispositionSpecified = true;
//指定這個項目將會被存放在寄件備份裡
createMailRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType sendItems = new DistinguishedFolderIdType();
sendItems.Id = DistinguishedFolderIdNameType.sentitems;
createMailRequest.SavedItemFolderId.Item = sendItems;
// 指定存放項目的陣列。 單一個 Item Request 可以容納多個 Item 建立的需求
createMailRequest.Items = new NonEmptyArrayOfAllItemsType();
// 建立 e-mail 的 message 並設定相關屬性
MessageType message = new MessageType();
message.Subject = "每日工作報表";
message.Body = new BodyType();
message.Body.BodyType1 = BodyTypeType.Text;
message.Body.Value = "1.10/20 數學總複習班開課 \n 2.10/30 大安高中招生說明會";
message.Sender = new SingleRecipientType(); // 寄件人
message.Sender.Item = new EmailAddressType();
message.Sender.Item.EmailAddress = "wenjen@exjh.mt.com.tw";
message.ToRecipients = new EmailAddressType[1];//收件人
message.ToRecipients[0] = new EmailAddressType();
message.ToRecipients[0].EmailAddress = "user3@exjh.mt.com.tw";
message.Sensitivity = SensitivityChoicesType.Normal;
// 把 message 指定給 ItemRequest 裡的每個 Item
createMailRequest.Items.Items = new ItemType[1];
createMailRequest.Items.Items[0] = message;
try{
//送出要求並取得回應
CreateItemResponseType createItemReponse = esb.CreateItem(createMailRequest);
ArrayOfResponseMessagesType responses = createItemReponse.ResponseMessages;
ResponseMessageType[] responseMessages = responses.Items; // 回應為陣列型態
//檢查回應中是否有錯誤或警告
foreach( ResponseMessageType respMsg in responseMessages ){
if( respMsg.ResponseClass == ResponseClassType.Error ){
msg.Text+= "Error:" + respMsg.MessageText + "\n";
}else if( respMsg.ResponseClass == ResponseClassType.Warning ){
msg.Text += "Warning:" + respMsg.MessageText + "\n";
}
// 檢查回應物件的型態
if( respMsg is ItemInfoResponseMessageType ){
ItemInfoResponseMessageType createItemResp = ( respMsg as ItemInfoResponseMessageType );
ArrayOfRealItemsType ary = createItemResp.Items;
//把回應轉為正確型態
// 由於是屬於 e-mail 的 messages,因此必須把 ItemType 轉為 MessageType
foreach( ItemType item in ary.Items ){
if( item is MessageType ){
MessageType myMessage = (item as MessageType );
msg.Text+= "Item id:" + myMessage.ItemId.Id + "\n";
}
}
}
}
}catch( Exception ex ){
msg.Text += ex.Message + "\n";
}