https://3gstudent.github.io/3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/ EWS是郵箱的一個開放的介面服務,可以取到郵箱的各種信息,郵件收發、會議、日期 ...
https://3gstudent.github.io/3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/
EWS是郵箱的一個開放的介面服務,可以取到郵箱的各種信息,郵件收發、會議、日期安排
首先需要引入 Microsoft.Exchange.WebServices.dll 引用
class Program { /// <summary> /// exchange服務對象 /// </summary> //private static ExchangeService _exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1); static void Main(string[] args) { #region 需要依賴 Microsoft.Exchange.WebServices 讀取未郵件箱 //ExchangeService版本為2007SP1 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); //參數是用戶名,密碼,域 service.Credentials = new WebCredentials("email.qq.com", "Zh.123456", "qq.com"); //給出Exchange Server的URL https://xxxxxxx service.Url = new Uri("https://mail.xxxx.com/ews/exchange.asmx"); //你自己的郵件地址 [email protected] service.AutodiscoverUrl("email.com", RedirectionCallback); //創建過濾器, 條件為郵件未讀. SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true); //查找Inbox,加入過濾器條件,結果10條 FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, new ItemView(10)); foreach (Item item in findResults.Items) { EmailMessage email = EmailMessage.Bind(service, item.Id); if (!email.IsRead) { Console.WriteLine(email.Body); //標記為已讀 email.IsRead = true; //將對郵件的改動提交到伺服器 email.Update(ConflictResolutionMode.AlwaysOverwrite); } } #endregion #region 不需要依賴 Microsoft.Exchange.WebServices 郵件發送 String user = "[email protected]"; String password = "Zh.123456"; ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { return true; }; StreamReader sendData = new StreamReader("ews.xml", Encoding.Default); byte[] sendDataByte = Encoding.UTF8.GetBytes(sendData.ReadToEnd()); sendData.Close(); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mail.xxxxx.com/ews/exchange.asmx"); request.Method = "POST"; request.ContentType = "text/xml"; request.ContentLength = sendDataByte.Length; request.AllowAutoRedirect = false; request.Credentials = new NetworkCredential(user, password); Stream requestStream = request.GetRequestStream(); requestStream.Write(sendDataByte, 0, sendDataByte.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { throw new WebException(response.StatusDescription); } Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); String receiveString = readStream.ReadToEnd(); response.Close(); readStream.Close(); StreamWriter receiveData = new StreamWriter("out.xml"); receiveData.Write(receiveString); receiveData.Close(); } catch (WebException e) { Console.WriteLine("[!]{0}", e.Message); Environment.Exit(0); } Console.WriteLine("[+]Done"); #endregion } static bool RedirectionCallback(string url) { // Return true if the URL is an HTTPS URL. return true; } public static void SendByExChange(string[] toemails, string[] Ctoemails, string title, string body, string[] fileName) { try { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };//至關重要的一句 否則會報錯:The Autodiscover service couldn't be located. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.Credentials = new NetworkCredential("[email protected]", "Zh.123456"); service.Url = new Uri(@"https://mail.xxxxx.com/ews/exchange.asmx");//exchange伺服器上介面地址 service.TraceEnabled = false; service.AutodiscoverUrl("[email protected]"); EmailMessage message = new EmailMessage(service); message.Subject = title; message.Body = body; if (fileName != null) { foreach (var str in fileName) message.Attachments.AddFileAttachment(str); } if (Ctoemails != null) { foreach (var email in Ctoemails) message.CcRecipients.Add(email); } if (toemails != null) { foreach (var email in toemails) message.ToRecipients.Add(email); } message.SendAndSaveCopy(); } catch (Exception ex) { //Utility.LogHelper.WriteLog("發送郵件失敗", ex); } } }