This article mainly discusses using the mailmessage and smtpmail classes in the system. Web. Mail namespace to simply implement the mail sending function.
Procedure andCodeAs follows: (the example is implemented using winform)
1. Add the reference to the project -->. Net --> Find system. Web. dll, OK, and add it.
2. Add the using code:UsingSystem. Web. mail;
3. Write the core sending code: Private Void Sendmail ( String Server, String From, String To, String Subject, String Body, String Username, String Password)
{
Try
{
Mailmessage mail = New Mailmessage ();
Mail. From = From;
Mail. = To;
Mail. Subject = Subject;
Mail. Body = Body;
Mail. bodyformat = Mailformat. html;
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , Username );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , Password );
Smtpmail. smtpserver = Server;
Smtpmail. Send (Mail );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message+Ex. innerexception. Message );
}
}
Note that the Sendmail method has the following three sentences: Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , Username );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , Password );
Verification information. If no information is written or the user name and password are incorrect, a prompt is displayed.
"Failed to access the CDO. Message object"
This is the most common error on the Internet. You can set a breakpoint to capture the error point and check the cause.
Here are several solutions to common CDO errors:
Jun. Net: http://blog.joycode.com/ghj/archive/2004/10/22/36274.aspx
Systemwebmail: http://www.systemwebmail.com/faq/4.2.3.aspx
4. You can call this method as needed. Remember to input the appropriate parameters.