C#中发送邮件,包含Html代码

Posted 邢帅杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中发送邮件,包含Html代码相关的知识,希望对你有一定的参考价值。

C#代码:

     /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public string SendMail(HttpContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(CookiesHelper.getCookie("send_mail_limit")))
                {
                    return "-5";//每分钟只能发送一次
                }
                string email = context.Request["email"];
                if (string.IsNullOrEmpty(email) || !CommonHelper.IsValidEmail(email))
                {
                    return "-1";//传值为空
                }

                //依据模板生成发送内容
                string sendText = "";
                string tempPath = context.Server.MapPath("~/EmailTemp/ModifyPwd.txt");

                using (StreamReader sr = new StreamReader(tempPath))
                {
                   sendText = sr.ReadToEnd();
                }
                sendText = sendText.Replace("{UserName_CH}", "星辰");
                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";//成功
            }
            catch (Exception)
            {
                return "-4";//异常
            }
        }

邮件模板:

亲爱的 <b>{UserName_CH}</b>,您好!
<br/>
您在本平台上提交了修改密码的请求。
<br/>
 验证码为:<b>{VCode}</b>,注意区分大小写!
 <br/>
 请按照页面提示完成密码的修改。
 <br/>
 (系统邮件,请勿回复)
<br/>
<br/>
<br/>
 Dear <b>{UserName_EN}</b> ,
 <br/>
  You have submitted a request to change the password on the platform.
  <br/>
  Verificationcode is <b>{VCode}</b> ,please note that the code is case sensitive!
  <br/>
  Enjoy your time !
  <br/>
 (Please do not reply.)

C#发送代码:

     /// <summary>
        /// 发送邮件1
        /// </summary>
        /// <param name="AcceptEmail"></param>
        /// <param name="sendText"></param>
        public static void SendEmail(string AcceptEmail, string sendText, string title)
        {
            SendSMTPEMail(mail_smtp, mail_main, mail_pwd, AcceptEmail, title, sendText);
        }
        /// <summary>
        /// 发送邮件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 strFrom, string strFromPass, string strto, string strSubject, string strBody)
        {
            SmtpClient client = new SmtpClient(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#配置代码:

     //邮件配置
        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:

  <!--邮件配置-->
    <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="25"/>
    <add key="mail_ssl" value="no"/>

 

以上是关于C#中发送邮件,包含Html代码的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何使用 Outlook 发送代码片段?

使用 SmtpClient 通过 C# 发送 HTML 电子邮件

C#发送邮件

如何使用电子邮件模板c#发送电子邮件

如何发送带有 HTML 标签的电子邮件,而不是 C# 中的 HTML 纯文本

从c#代码发送电子邮件[重复]