Send mail in C #, including HTML code

Source: Internet
Author: User

C # code:

     <summary>//e-mail///</summary>//<param name= "context" ></param>            <returns></returns> public String SendMail (HttpContext context) {try {if (!string.                IsNullOrEmpty (Cookieshelper.getcookie ("Send_mail_limit")) {return "-5";//can only be sent once per minute } string email = context.                request["email"]; if (string. IsNullOrEmpty (email) | | ! Commonhelper.isvalidemail (email)) {return "-1";//value is null}/                /Generate Send content by template string sendText = ""; String TempPath = context.                Server.MapPath ("~/emailtemp/modifypwd.txt"); using (StreamReader sr = new StreamReader (TempPath)) {SendText = Sr.                ReadToEnd ();      } SendText = Sendtext.replace ("{username_ch}", "star");          SendText = Sendtext.replace ("{username_en}", "star");                SendText = Sendtext.replace ("{Vcode}", "Abks");                Commonhelper.sendemail (email, sendText, Resource.Lang.RetrievePassword);                Cookieshelper.setcookie ("Send_mail_limit", "SendMail", 1.00);        return "1";//Success} catch (Exception) {return "-4";//exception} }

Mail Template:

Dear <b>{username_ch}</b>, Hello! <br/> you have submitted a request to change your password on this platform. <br/> Verification Code: <B>{VCODE}</B>, pay attention to case-sensitive! <br/> follow the page prompts to complete the password modification. <br/> (System mail, do not reply) <br/><br/><br/> Dear <b>{UserName_EN}</b>, <br/>  You had submitted a request to change the password on the platform.  <br/>  Verificationcode is <b>{VCode}</b>, please note that the code was case sensitive!  <br/>  Enjoy your time!  <br/> (reply.)

C # Send code:

     <summary>//Email 1///</summary>//<param name= "Acceptemail" ></para m>//<param name= "SendText" ></param> public static void SendEmail (String acceptemail, Strin         G SendText, string title) {Sendsmtpemail (MAIL_SMTP, Mail_main, Mail_pwd, Acceptemail, title, SendText); }//<summary>//E-mail 2///</summary>//<param name= "Strsmtpserver "></param>//<param name=" strfrom "></param>//<param name=" Strfrompass "></        param>//<param name= "Strto" ></param>//<param name= "Strsubject" ></param> <param name= "strbody" ></param> public static void Sendsmtpemail (String strsmtpserver, String St Rfrom, String Strfrompass, String Strto, String strsubject, String strbody) {SmtpClient client = new Sm Tpclient (Strsmtpserver);            Client.            useDefaultCredentials = false; Client.            Credentials = new System.Net.NetworkCredential (strfrom, Strfrompass); Client.            Deliverymethod = Smtpdeliverymethod.network; Client.            Port = Mail_port; Client.            Enablessl = Mail_ssl = = "Yes";            MailMessage message = new MailMessage (strfrom, Strto, Strsubject, strbody); Message.            bodyencoding = System.Text.Encoding.UTF8; Message.            Isbodyhtml = true; Client.        Send (message); }

C # Configuration Code:

Mail configuration public        static string mail_smtp = system.configuration.configurationmanager.appsettings["Mail_smtp"];        public static string mail_main = system.configuration.configurationmanager.appsettings["Mail_main"];        public static string mail_pwd = system.configuration.configurationmanager.appsettings["Mail_pwd"];        public static int mail_port = Convert.ToInt32 (system.configuration.configurationmanager.appsettings["Mail_port"]);        public static string Mail_ssl = system.configuration.configurationmanager.appsettings["Mail_ssl"];

Web. config:

<!--mail Configuration-    <add key= "mail_smtp" value= "smtp.ym.163.com"/> <add    key= "Mail_main" value= "[ Email protected] "/>    <add key=" mail_pwd "value=" xxxxxx/> "<add" key= "Mail_port [value=]/>    <add key= "Mail_ssl" value= "no"/>

Send mail in C #, including HTML code

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.