2013年10月17日 星期四

Exchange Service Binding

使用Exchange Service Send Mail

C#

public void SendMessage(ExchangeServiceBinding ewsServiceBinding, ItemIdType iiCreateItemid)
        {
            SendItemType siSendItem = new SendItemType();
            siSendItem.ItemIds = new BaseItemIdType[1];
            siSendItem.SavedItemFolderId = new TargetFolderIdType();
            DistinguishedFolderIdType siSentItemsFolder = new DistinguishedFolderIdType();
            siSentItemsFolder.Id = DistinguishedFolderIdNameType.sentitems;
            siSendItem.SavedItemFolderId.Item = siSentItemsFolder;
            siSendItem.SaveItemToFolder = true;


            siSendItem.ItemIds[0] = (BaseItemIdType)iiCreateItemid;
            SendItemResponseType srSendItemReponseMessage = ewsServiceBinding.SendItem(siSendItem);
            if (srSendItemReponseMessage.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
            {
                Console.WriteLine("Error Occured");
                Console.WriteLine(srSendItemReponseMessage.ResponseMessages.Items[0].MessageText);
            }
            else
            {
                Console.WriteLine("Message Sent");
            }
        }

        public string SendMail_ex2007(String Sender, String reciver, String subject, String content)
        {

            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.Url = "https://mail.xxx.com/EWS/Exchange.asmx";
            servicea = esb;
            //System.Net.ServicePointManager.ServerCertificateValidationCallback =
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (Object obj, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors) => true;
            esb.Credentials = new NetworkCredential("USERNAME", "PASSWORD", "SMTP");
            MessageType emailMessage = new MessageType();
            emailMessage.From = new SingleRecipientType();
            //set up a single sender
            //寄件者
            emailMessage.From.Item = new EmailAddressType();
            emailMessage.From.Item.EmailAddress = Sender;
            //標題
            emailMessage.Subject = subject;
            emailMessage.Body = new Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.Exchange2007.BodyType();
            emailMessage.Body.BodyType1 = BodyTypeType.Text;
            //本文
            emailMessage.Body.BodyType1 = BodyTypeType.HTML;
            emailMessage.Body.Value = content;
            //將email寄出
            emailMessage.Sender = new SingleRecipientType();
            emailMessage.Sender.Item = new EmailAddressType();
            emailMessage.Sender.Item.EmailAddress = Sender;
            emailMessage.ToRecipients = new EmailAddressType[1];
            emailMessage.ToRecipients[0] = new EmailAddressType();
            emailMessage.ToRecipients[0].EmailAddress = reciver;
            emailMessage.Sensitivity = SensitivityChoicesType.Normal;
            //建立附件
            ItemIdType iiCreateItemid = CreateDraftMessage(esb, emailMessage);
            //iiCreateItemid = CreateAttachment(esb, iiCreateItemid);
            SendMessage(esb, iiCreateItemid);
            return "sendok";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
SendMail_ex2007("SenderName", "xxx@gmail.com", "smtp.xxx.com", "subject", "Content");
        }

沒有留言:

張貼留言