Exchage 2007 Client Application Functions(1) -- 关于ExchangeServer2007
首先,因为一直工作比较累,回来也没什么劲来发帖子,所以这是我的博客园上的第一篇文章。
Exchage 2007是工作中一点一点熟悉的,和大家分享一下。希望这篇文章对你有帮助。
下面直接进入主题。
Exchage 2007 的客户端程序,对Exchage Server的操作方式为OWA,就是outlook web access。下图为浏览器访问 https://mail.abc.com/owa/ 截图:
登录后:
至于怎么操作使用,相信大家都会。下面我会用简单介绍用代码实现用户操作。
看看微软给的api。在 ExchangeWebService 下面所有的类/枚举/结构等,多是用Type结尾。。。。。。。汗一个先。。
Exchange2007使用的是https,所以,客户端登录的时候,须使用身份验证:
public class EWS
{
public string m_UserID = string.Empty;
public string m_Password = string.Empty;
public string m_Domain = string.Empty;
public string m_ServerUrl = string.Empty;
public string m_UserEmailAddress = string.Empty;
public string m_ItemId = string.Empty;
public string m_ItemChangeKey = string.Empty;
public BaseFolderType ServerEmailFolder;
public ExchangeServiceBinding m_esb;
public EWS(string UserId, string Password, string DoMain, string ServerUrl)
{
this.m_UserID = UserId;
this.m_Password = Password;
this.m_Domain = DoMain;
this.m_ServerUrl = ServerUrl;
m_esb = new ExchangeServiceBinding();
m_esb.Credentials = new System.Net.NetworkCredential(this.m_UserID, this.m_Password, this.m_Domain);
m_esb.Url = this.m_ServerUrl;
}
}
{
public string m_UserID = string.Empty;
public string m_Password = string.Empty;
public string m_Domain = string.Empty;
public string m_ServerUrl = string.Empty;
public string m_UserEmailAddress = string.Empty;
public string m_ItemId = string.Empty;
public string m_ItemChangeKey = string.Empty;
public BaseFolderType ServerEmailFolder;
public ExchangeServiceBinding m_esb;
public EWS(string UserId, string Password, string DoMain, string ServerUrl)
{
this.m_UserID = UserId;
this.m_Password = Password;
this.m_Domain = DoMain;
this.m_ServerUrl = ServerUrl;
m_esb = new ExchangeServiceBinding();
m_esb.Credentials = new System.Net.NetworkCredential(this.m_UserID, this.m_Password, this.m_Domain);
m_esb.Url = this.m_ServerUrl;
}
}
以上代码m_esb.Credentials = new System.Net.NetworkCredential(this.m_UserID, this.m_Password, this.m_Domain);这句话,就是为程序创建了一个许可证。
初始化之后,你就可以使用 变量 m_esb( ExchangeServiceBinding )了 :)。
说道邮件服务器,发邮件是一项非常重要的操作。看下面代码:
public bool SendMail(string AddressTo, string AddressFrom, string Subject, string Body, string Bcc, string Cc,string BodyType)
{
string[] address ={ };
// Create the CreateItem request
CreateItemType createItemRequest = new CreateItemType();
ItemIdType iiAttachmentItemid = new ItemIdType();
// Specifiy how the created items are handled
createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
createItemRequest.MessageDispositionSpecified = true;
// Specify the location of sent items
createItemRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
sentitems.Id = DistinguishedFolderIdNameType.sentitems;
createItemRequest.SavedItemFolderId.Item = sentitems;
// Create the array of items
createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
//the body of email
MessageType message = new MessageType();
message.Subject = Subject;
//message.ImportanceSpecified = true;
message.Importance = ImportanceChoicesType.Normal;
message.Body = new BodyType();
if (BodyType.ToLower().ToLower() == "text")
message.Body.BodyType1 = BodyTypeType.Text;
else
message.Body.BodyType1 = BodyTypeType.HTML;
message.Body.Value = Body;
message.ItemClass = "IPM.Note";
if (AddressFrom != "" && AddressFrom != null)
{
message.Sender = new SingleRecipientType();
message.Sender.Item = new EmailAddressType();
message.Sender.Item.EmailAddress = AddressFrom;
}
// message.Sensitivity = SensitivityChoicesType.Personal;
if (AddressTo.Contains(","))
{
if (AddressTo.EndsWith(","))
{
AddressTo = AddressTo.Substring(0, AddressTo.Length - 1);
}
address = AddressTo.Split(',');
message.ToRecipients = new EmailAddressType[address.Length];
for (int i = 0; i < address.Length; i++)
{
message.ToRecipients[i] = new EmailAddressType();
message.ToRecipients[i].EmailAddress = address[i];
}
}
if (AddressTo.Contains(";"))
{
if (AddressTo.EndsWith(";"))
{
AddressTo = AddressTo.Substring(0, AddressTo.Length - 1);
}
address = AddressTo.Split(';');
message.ToRecipients = new EmailAddressType[address.Length];
for (int i = 0; i < address.Length; i++)
{
message.ToRecipients[i] = new EmailAddressType();
message.ToRecipients[i].EmailAddress = address[i];
}
}
if (!AddressTo.Contains(";") && !AddressTo.Contains(","))
{
message.ToRecipients = new EmailAddressType[1];
message.ToRecipients[0] = new EmailAddressType();
message.ToRecipients[0].EmailAddress = AddressTo;
}
if (Bcc != null && Bcc != "" && Bcc != string.Empty)
{
message.BccRecipients = new EmailAddressType[1];
message.BccRecipients[0] = new EmailAddressType();
message.BccRecipients[0].EmailAddress = Bcc;
}
if (Cc != null && Cc != "" && Cc != string.Empty)
{
message.CcRecipients = new EmailAddressType[1];
message.CcRecipients[0] = new EmailAddressType();
message.CcRecipients[0].EmailAddress = Cc;
}
message.Sensitivity = SensitivityChoicesType.Normal;
createItemRequest.Items.Items = new ItemType[1];
createItemRequest.Items.Items[0] = message;
try
{
CreateItemResponseType response = m_esb.CreateItem(createItemRequest);
ArrayOfResponseMessagesType responseMessages = response.ResponseMessages;
if (responseMessages.Items[0].ResponseClass == ResponseClassType.Success)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw new Exception("Error in SendMail():" + e.Message);
}
}
{
string[] address ={ };
// Create the CreateItem request
CreateItemType createItemRequest = new CreateItemType();
ItemIdType iiAttachmentItemid = new ItemIdType();
// Specifiy how the created items are handled
createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
createItemRequest.MessageDispositionSpecified = true;
// Specify the location of sent items
createItemRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
sentitems.Id = DistinguishedFolderIdNameType.sentitems;
createItemRequest.SavedItemFolderId.Item = sentitems;
// Create the array of items
createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
//the body of email
MessageType message = new MessageType();
message.Subject = Subject;
//message.ImportanceSpecified = true;
message.Importance = ImportanceChoicesType.Normal;
message.Body = new BodyType();
if (BodyType.ToLower().ToLower() == "text")
message.Body.BodyType1 = BodyTypeType.Text;
else
message.Body.BodyType1 = BodyTypeType.HTML;
message.Body.Value = Body;
message.ItemClass = "IPM.Note";
if (AddressFrom != "" && AddressFrom != null)
{
message.Sender = new SingleRecipientType();
message.Sender.Item = new EmailAddressType();
message.Sender.Item.EmailAddress = AddressFrom;
}
// message.Sensitivity = SensitivityChoicesType.Personal;
if (AddressTo.Contains(","))
{
if (AddressTo.EndsWith(","))
{
AddressTo = AddressTo.Substring(0, AddressTo.Length - 1);
}
address = AddressTo.Split(',');
message.ToRecipients = new EmailAddressType[address.Length];
for (int i = 0; i < address.Length; i++)
{
message.ToRecipients[i] = new EmailAddressType();
message.ToRecipients[i].EmailAddress = address[i];
}
}
if (AddressTo.Contains(";"))
{
if (AddressTo.EndsWith(";"))
{
AddressTo = AddressTo.Substring(0, AddressTo.Length - 1);
}
address = AddressTo.Split(';');
message.ToRecipients = new EmailAddressType[address.Length];
for (int i = 0; i < address.Length; i++)
{
message.ToRecipients[i] = new EmailAddressType();
message.ToRecipients[i].EmailAddress = address[i];
}
}
if (!AddressTo.Contains(";") && !AddressTo.Contains(","))
{
message.ToRecipients = new EmailAddressType[1];
message.ToRecipients[0] = new EmailAddressType();
message.ToRecipients[0].EmailAddress = AddressTo;
}
if (Bcc != null && Bcc != "" && Bcc != string.Empty)
{
message.BccRecipients = new EmailAddressType[1];
message.BccRecipients[0] = new EmailAddressType();
message.BccRecipients[0].EmailAddress = Bcc;
}
if (Cc != null && Cc != "" && Cc != string.Empty)
{
message.CcRecipients = new EmailAddressType[1];
message.CcRecipients[0] = new EmailAddressType();
message.CcRecipients[0].EmailAddress = Cc;
}
message.Sensitivity = SensitivityChoicesType.Normal;
createItemRequest.Items.Items = new ItemType[1];
createItemRequest.Items.Items[0] = message;
try
{
CreateItemResponseType response = m_esb.CreateItem(createItemRequest);
ArrayOfResponseMessagesType responseMessages = response.ResponseMessages;
if (responseMessages.Items[0].ResponseClass == ResponseClassType.Success)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw new Exception("Error in SendMail():" + e.Message);
}
}
现在,我们可以发送邮件了:)
EWS ews = new EWS("username","1234567","abc","https://mail.abc.com/owa/");
ews.SendMail("AddressTo","AddressFrom","标题。。。。","内容。。。。","text");
这样,不用smtp,不用Jmail,自制邮件发送功能便实现了:)
第一篇文章先写到这里。晚上再写第二篇。