2012-09-05 17 views
3

我下面的代碼是通過閱讀模板html發送電子郵件,它工作正常。但現在我的問題是如何在運行時將我的.net代碼中的Salutation customerName傳遞給模板。運行時在.net代碼中編輯HTML電子郵件模板

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(); 

謝謝。

回答

4

這是sendemail按鈕的代碼

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("[email protected]", "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("[email protected]", "password"); 
     Mailing.Credentials = MyCredential; Mailing.Send(Message); 
    } 
+0

謝謝普拉迪普 –