ASP. NET -- Implement mail sending

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.