The namespace used: system. net. Mail. Note that it is not system. Web. Mail. Here, you can use IIS on your machine to send emails.
Main Code:
String from = txtfrom. text; // The sender address <br/> string to = txtto. text; // recipient address </P> <p> mailmessage mail = new mailmessage (from, to); </P> <p> mail. subjectencoding = encoding. utf8; // encoding <br/> mail. subject = txttitle. text; // mail title <br/> mail. isbodyhtml = true; // whether the content is allowed in HTML format <br/> mail. bodyencoding = encoding. utf8; <br/> mail. body = txtcontent. text; // email content </P> <p> // select an attachment using a fileupload </P> <p> If (filesend. hasfi Le & filesend. postedfile. contentlength! = 0) <br/>{< br/> mail. attachments. add (new attachment (filesend. postedfile. filename. tostring (); // Add an attachment <br/>}</P> <p> smtpclient SMTP = new smtpclient ("localhost", 25 ); <br/> SMTP. usedefaultcredentials = true; <br/> try <br/>{< br/> SMTP. send (Mail); <br/>}< br/> catch (exception ex) <br/>{< br/> response. write (ex. message); <br/>}< br/> finally <br/>{< br/> mail. attachments. dispose (); // After the email is sent, the attachment is locked <br/> mail. dispose (); // release resources used by mailmessage <br/>}< br/>
IIS configuration:
1. Right-click "Default SMTP virtual server" and choose Properties;
2. Select the "access" tab and click "Identity Authentication". The Selection box before "Anonymous Access" is hooked;
3. Click "relay" and click "add" to add the "127.0.0.1" ip address;
4. OK. The application exits.
The above code has been tested in vs2005 + iisv5.1 and can send emails without errors.