為了要可以使用Exchage server寄信,必須從NuGet下載Exchange Web Services (EWS)
後來測試,怎麼連不都連不上,才發現原來有相依.Net的版本(好像要3.5以上)
另外一個要注意的是網址,用的是exchange.asmx,不是原本的xxx.com/owa/auth/logon.aspx
這個環節也試了很久,後來看到別人寫的範例都是.asmx,想說試看看就可以連了
Sample Code:
http://blog.oscarscode.com/dot-net/get-started-with-exchange-web-services-ews/
https://stackoverflow.com/questions/13517323/exchange-web-service-api-and-401-unauthorized-exception
https://blog.miniasp.com/post/2007/11/01/The-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);//版本預設值最新版
service.Credentials = new WebCredentials(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
service.Url = new Uri("https://xxxx.com/ews/exchange.asmx"); // Server路徑
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add(contact.Name, contact.Email); //收件者
email.CcRecipients.Add("AAA", "test1@mail.com"); //CC
email.BccRecipients.Add("BBB", "test2@mail.com "); //密件副本
email.Subject = mailJob.Subject; //主旨
email.Body = mailJob.Content; //內容
email.Body.BodyType = BodyType.HTML; //格式
email.Send();
Ref:http://blog.oscarscode.com/dot-net/get-started-with-exchange-web-services-ews/
https://stackoverflow.com/questions/13517323/exchange-web-service-api-and-401-unauthorized-exception
https://blog.miniasp.com/post/2007/11/01/The-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx