C#. NET uses HTML templates to send full instances of e-mail

Source: Internet
Author: User
Tags mailmessage smtpclient

The examples in this article describe C #. NET using HTML templates to send e-mail messages is a very practical technique. Share to everyone for your reference. Here's how:

To send a message using an HTML template, you need to prepare the following tasks:

1) HTML templates

2) Replace function (replace the bound variable in the template)

3) Mail function (send mail)


First, HTML templates

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The three variables, user_name, NAME, and My_name, are identified with the $ symbol package, which is a string that needs to be replaced, which is replaced dynamically in the replacement function below.


Second, replace the function

<summary>///Replace the field values in the template//</summary>public string ReplaceText (String username,string name,string MyName) {  string path = string. Empty;  Path = HttpContext.Current.Server.MapPath ("emailtemplate\\emailtemplate.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;}


Third, the Mail sent

<summary>///Send mail///</summary>public void SendEmail (String email_from,string email_to, String email_cc , string userName, string name, String myName) {try {//build a mail entity mailaddress from = new MailAddress (email_from); MailAddress to = new MailAddress (email_to); MailMessage message = new MailMessage (from, to); 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; The message body content 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);  Send mail} catch (Exception ex) {throw ex; }}

In fact, no matter what way or component to send the message, to replace the content in the HTML template, just a replace function.

It is believed that this article has certain reference value for the study of C#.net program design.

In addition to the Declaration,Running GuestArticles are original, reproduced please link to the form of the address of this article
C#. NET uses HTML templates to send full instances of e-mail

This address: http://www.paobuke.com/develop/c-develop/pbk23478.html






Related content C # development Portal and Applications (2) Message handling and Response C # Writing ActiveX Web page control WinForm Simple Cache class Instance GraphicsPath Warp method usage instance in C #
C # Hide Console keyboard input methods in C # extension methods in the C # implementation method of removing the specified character at the end of a string. NET implementation of several common methods of file reading and writing

C#. NET uses HTML templates to send full instances of e-mail

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.