標籤:winform style blog http color os io for
原文:一個意想不到的CDO.Message 錯誤
幾個月之前,寫了一個服務從MSMQ取訊息發群發郵件的程式,一直也沒時間測試,今日一試,出現發送郵件時報錯,異常情況如下:
"System.Web.HttpException: 未能訪問“CDO.Message”對象。 ---> System.Reflection.TargetInvocationException: 調用的目標發生了異常。 ---> System.Runtime.InteropServices.COMException (0x80040211): 郵件無法發送到 SMTP 伺服器。傳輸錯誤碼為 0x80040217。伺服器響應為 not available\r\n\r\n --- 內部異常堆疊追蹤的結尾 ---\r\n
相關代碼如下:
SmtpMail.SmtpServer = smtpServer;
MailMessage mailMessage = new MailMessage();
mailMessage.Subject = mailSubject;
mailMessage.From = mailFrom;
mailMessage.BodyFormat = MailFormat.Html;
mailMessage.Body = mailBody;
SmtpMail.Send(mailMessage);
這麼簡單的代碼也會出錯?馬上寫了個WebForm測試,代碼運行通過。嗯。難道寫成服務就不行了?立即GOOGLE到一份蟈蟈俊整理的解決方案:發送Email時候,可能導致異常:未能訪問“CDO.Message”對象,的幾種情況整理 ,嘿...天助我也!經一番不懈努力,得到以下答案:
如法炮製之後,問題依然如舊;究竟是何原由,仍需親自研究!:(
據我所知,這個smtpServer並無驗證機制,webform也通過了,整個系統該給的許可權也給過了,也在多個環境(WIN2000,WIN2003)下測試過,真是沒招了,索性找個域帳號加個驗證一試,添加三行代碼如下:
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", @"××\××");
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "××");
測試結果:通過! 哐鏜….
smtpServer並無驗證機制,但不加驗證就報“CDO.Message”錯?不合理,再次修改代碼,去掉使用者和密碼,指定預設的smtp認證參數為“0”,代碼如下:
mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0");
測試結果:通過!
代碼雖然通過,但還是不太明白,為什麼通過winform發郵件 CDO.Message 預設的參數是需要認證的呢?