Today, A. NET project involves sending emails. In fact, sending emails in. NET is very simple. You only need a few lines of code to send a simple text email. For example:
Public void sendemail (string smtpserver, string fromaddr, string toaddr, string ccaddr, string bccaddr, string subject, string message, out int errnum, out string errtext)
{
Mailmessage message = new mailmessage ();
Message. To = toaddr;
Message. From = fromaddr;
Message. Cc = ccaddr;
Message. bcc = bccaddr;
Message. Subject = subject;
Message. Body = message;
Try
{
SmtpMail. SmtpServer = smtpServer;
SmtpMail. Send (Message );
ErrNum = 0;
ErrText = "";
}
Catch (HttpException ex)
{
ErrNum = ex. ErrorCode;
ErrText = ex. Message;
}
}
However, a simple piece of code left me alone for one morning, because the error message "cocould not access 'cdo. message' object." is always prompted. Because it is a relatively large project, it is difficult to test, and the company's SMTP server is used, so I wrote a separate program for testing, and the results are all normal. What's strange?
By carefully comparing the Code, there is no difference. Only the comparison input is used, and the results show that the input is indeed different. In the project, message. CC and message. BCC are assigned a cc.address@xxx.com and a bcc.address@xxx.com, respectively, and neither of these values in the test program is assigned any value. Maybe this is the reason. After another test, it is true. It turns out that after we assign values to these two items, the company's email program will verify their validity, but if they are not assigned values, they will not be verified. The reason for ignoring this is that these two items are optional. If you enter a value randomly, the result will be stuck.
A test of message. From is generated, but it does not validate this required item. Alas, it's all about the mail server of the company. When I use localhost to send an email, it doesn't matter whether you send the email or what the address is.
So if you write email programs in the company and plan to use the company's SMTP server, this error should not be surprising. You just need to take a good look at your inputs.
In addition, I checked this error online for many reasons, such:
1. SMTP server is not set correctly or is unavailable
2. Use localhost or "127.0.0.1" as the SMTP server, but you do not have the permission to use the iis smtp service for transit. To add this permission, open IIS management, right-click SMTP virtual server, choose Properties> access tab> relay, add 127.0.0.1 to the relay restrictions dialog box, and restart IIS.
3, it is best to use valid mail addresses, do not use some asdf@asdf.com or something.