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