Asp.net uses HTML templates to send emails

Source: Internet
Author: User

When creating an ASP. NET-based project, you need to use an HTML template to send an email. The following is a summary:

HTML Template

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The string wrapped with the $ symbol must be dynamically replaced in the program.

Send email

/// <Summary> /// send an email /// </Summary> Public void sendemail (string email_from, string email_to, string email_cc, string username, string name, string myname) {try {// create a mail entity mailaddress from = new mailaddress (email_from); mailaddress to = new mailaddress (email_to); mailmessage message = new mailmessage (from, ); string strbody = replacetext (username, name, myname); If (email_cc.tostring ()! = String. empty) {foreach (string CCS in email_cc.split (';') {mailaddress cc = new mailaddress (CCS); message. cc. add (CC) ;}} message. isbodyhtml = true; message. bodyencoding = system. text. encoding. utf8; message. priority = mailpriority. high; message. body = strbody; message. subject = "subject"; smtpclient SMTP = new smtpclient (); SMTP. host = configuration. mailhost; SMTP. port = configuration. mailhostport; SMTP. credentials = new system. net. networkcredential (email_from, "emailpassword"); SMTP. send (Message) ;}catch (exception ex) {Throw ex ;}}

Replace the field value in the HTML Template

/// <Summary> /// Replace the field value in the template /// </Summary> Public String replacetext (string username, string name, string myname) {string Path = string. empty; Path = httpcontext. current. server. mappath ("email_template \ email.html"); If (Path = string. empty) {return string. empty;} system. io. streamreader sr = new system. io. streamreader (PATH); string STR = string. empty; STR = sr. readtoend (); STR = Str. replace ("$ user_name $", username); STR = Str. replace ("$ name $", name); STR = Str. replace ("$ my_name $", myname); Return STR ;}

The end

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.