Original article: an unexpected CDO. Message Error
A few months ago, I wrote a program for the service to retrieve and send emails from MSMQ. I haven't had time to test the program. I tried it today and encountered an error when I sent an email. The exception is as follows:
"System. Web. httpexception: failed to access the" CDO. Message "object. ---> System. reflection. targetinvocationexception: The call Target has an exception. ---> System. runtime. interopservices. comexception (0x80040211): the email cannot be sent to the SMTP server. The transfer error code is 0x80040217. The server response is not available \ r \ n \ r \ N --- end of the internal exception stack trace --- \ r \ n
The related code is as follows:
Smtpmail. smtpserver = smtpserver;
Mailmessage = new mailmessage ();
Mailmessage. Subject = mailsubject;
Mailmessage. From = mailfrom;
Mailmessage. bodyformat = mailformat. html;
Mailmessage. Body = mailbody;
Smtpmail. Send (mailmessage );
Will such a simple code also go wrong? I immediately wrote a webform test and the Code passed the test. Well. Isn't it possible to write services? Immediately Google a solution prepared by: when sending an email, it may cause an exception: failed to access "CDO. message object, which can be sorted out in several situations... and me! After unremitting efforts, I got the following answer:
After the process is done, the problem is still as old as possible. The reason for this problem still needs to be studied in person! :(
As far as I know, this smtpserver does not have a verification mechanism, and webform has passed. The permissions assigned to the entire system have been granted, and it has been tested in multiple environments (Win2000, win2003, I did not try it. Simply find a domain account and try again. Add the following three lines of code:
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", "×× ");
Test result: pass! Boring ....
The smtpserver does not have a verification mechanism, but the error "CDO. Message" is reported without verification? Unreasonable. modify the code again, remove the user and password, and specify the default SMTP authentication parameter as "0". The Code is as follows:
Mailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0 ");
Test result: pass!
Although the Code passes, I still don't quite understand why the default parameters of CDO. Message sent via winform require authentication?