一個意想不到的CDO.Message 錯誤

來源:互聯網
上載者:User

標籤: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 預設的參數是需要認證的呢?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.