c# - 在 .net 代码中在运行时编辑 HTML 电子邮件模板

标签 c# asp.net html system.net.mail

我下面的代码是通过阅读模板 html 来发送电子邮件,它工作正常。但现在我的问题是如何在运行时将 Salutation customerName 从我的 .net 代码传递到模板。

 StringBuilder strBlr = new StringBuilder();
            string strHTML = string.Empty;
            string strTempalteHtmlpath = string.Empty;

            //create the mail message
            MailMessage mail;

 string strFrom = ConfigurationSettings.AppSettings["fromAddressForBT"];
                string strSubject = "Thanks for choosing Email contact preference";
                mail = new MailMessage(strFrom, customerDetails.EmailId);

                mail.Subject = strSubject;

                //Read Html Template File Path
                strTempalteHtmlpath = Convert.ToString(ConfigurationSettings.AppSettings["TemplatePath"]);
                strHTML = File.ReadAllText(strTempalteHtmlpath);
                strBlr = strBlr.Append(strHTML);
                mail.Body = strBlr.ToString();
                mail.IsBodyHtml = true;


                //first we create the Plain Text part
                AlternateView plainView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/plain");
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(strBlr.ToString(), null, "text/html");

 mail.AlternateViews.Add(plainView);
                mail.AlternateViews.Add(htmlView);

                //send the message
                SmtpClient smtpMail = new SmtpClient(ConfigurationSettings.AppSettings["smtpClient"]);
                smtpMail.Send(mail);

                mail.Dispose();

谢谢。

最佳答案

这是发送邮件按钮的代码

    StreamReader sr = new StreamReader(Server.MapPath("Sendpage.htm"));
    string body = sr.ReadToEnd();         
    sr.Close();          
    body = body.Replace("#NameFamily#", txtNameFamily.Text);     
    body = body.Replace("#Email#", txtEmail.Text);        
    body = body.Replace("#Tellphone#", txtTellphone.Text);   
    body = body.Replace("#Text#", txtText.Text);       
    body = body.Replace("#Date#", DateTime.Now);        
    string Time = Convert.ToString(DateTime.Now.ToShortTimeString());    
    body = body.Replace("#Time#", Time);        
    SendMail("email that you want to send to it", body); 

这是sendmail函数代码:

 private void SendMail(string To, string Body)
    {
        SmtpClient Mailing = new SmtpClient("mail.domain.com");
        MailMessage Message = new MailMessage();
        Message.From = new MailAddress("mail@domain.com", "Your name or company name");
        Message.Subject = "Subject";
        Message.SubjectEncoding = Encoding.UTF8;
        Message.IsBodyHtml = true;
        Message.BodyEncoding = Encoding.UTF8;
        Message.Body = Body;
        Message.To.Add(new MailAddress(To));
        Mailing.UseDefaultCredentials = false;
        NetworkCredential MyCredential = new NetworkCredential("mail@domain.com", "password");
        Mailing.Credentials = MyCredential; Mailing.Send(Message);
    }

关于c# - 在 .net 代码中在运行时编辑 HTML 电子邮件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12281374/

相关文章:

asp.net - 您对依赖于数据库的 ASP.NET 应用程序使用什么缓存策略?

javascript - 如何防止图像离开html中的区域

html - CSS:如何使另一个 Div 下方的文本 Div 可选?

C# 确定 SQLite 数据库文件是否经过密码加密

c# - 如何使用 asp.net 澄清现有的 json 数据

c# - 在 Unity 中使用 GestureRecogniser 双击全息镜头

ASP.NET MVC 3 数据注释 : Add validation dynamically

javascript - 添加了不触发脚本的自定义属性

c# - 远程服务器返回错误 : (407) Proxy Authentication Required

c# - 使用 AssemblyInstaller 安装 Windows 服务时出现问题