title: HTML based email not working properly in asp.net web form
body: <p>I am working on web form which collects certain user information from users and sends confirmation by email.  I am trying to user the following approach to send the HTML email as it make managing HTML based email easy.</p>
  
  <p><a href="https://gist.github.com/1668751" rel="nofollow">https://gist.github.com/1668751</a></p>
  
  <p>I made necessary changes to the code but it is not working. I read other related article to make it work but i am doing something wrong.</p>
  
  <p>Following line of  code generates error <code>The replacements dictionary must contain only strings.</code></p>
  
  <p><code>MailMessage msgHtml = mailDef.CreateMailMessage(to, replacements, MessageBody, new System.Web.UI.Control());</code></p>
  
  <p>I have made many change to the code but it doesnt seem to work for me. I would appreciate help to make this code work.</p>
  
  <p>If i comment the above line of code with some other changes then i can send email but i can't replace the token.  Any easy approach to replace token is also welcome.</p>
  
  <p>Below is the Complete code i am using right now</p>
  
  <pre><code>String to, subject, Name;
  subject = "Booking Confirmation";
  Name = txtName.text;
  
  
  ListDictionary replacements = new ListDictionary();
  replacements.Add("&lt;%Name%&gt;", Name);
  replacements.Add("&lt;%Email%&gt;", objVR.Email);
  replacements.Add("&lt;%CompanyName%&gt;", objVR.CompanyName);
  replacements.Add("&lt;%BookingDate%&gt;", objVR.BookingDate);
  replacements.Add("&lt;%BookingTime%&gt;", objVR.TimeSlot);
  replacements.Add("&lt;%NoOfVisitors%&gt;", objVR.NoOfVisitors);
  replacements.Add("&lt;%BookingCode%&gt;", objVR.BookingUniqueID);
  
  
  MailDefinition mailDef = new MailDefinition();
  
  string MessageBody = String.Empty;
  string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
  
  using (StreamReader sr = new StreamReader(filePath + @"\en\VREmailEnglish.htm"))
  {
      MessageBody = sr.ReadToEnd();
  }
  
  MailMessage msgHtml = mailDef.CreateMailMessage(to, replacements, MessageBody, new System.Web.UI.Control());
  </code></pre>
  
  <p>string message = msgHtml.Body.ToString();</p>
  
  <pre><code>Helper.SendTokenEmail(to, subject, msgHtml, isHtml);
  
  
  
  
  public static void SendTokenEmail(string to, string subject, string mailMessage, bool isHtml)
          {
              try
              {
                  // Create a new message
                  var mail = new MailMessage();
  
                  // Set the to and from addresses.
                  mail.From = new MailAddress("noreply@somedomain.net");
                  mail.To.Add(new MailAddress(to));
  
                  // Define the message
                  mail.Subject = subject;
                  mail.IsBodyHtml = isHtml;
                  mail.Body = mailMessage.ToString();
                  //Object userState = mailMessage;
  
                  // Create a new Smpt Client using Google's servers
                  var mailclient = new SmtpClient();
                  mailclient.Host = "mail.XYZ.net";
                  //mailclient.Port = 587; //ForGmail
                  mailclient.Port = 2525;
  
                  mailclient.EnableSsl = false;
                  mailclient.UseDefaultCredentials = true;
  
                  // Specify your authentication details
  
                  mailclient.Credentials = new System.Net.NetworkCredential("noreply@somedomain.net", "XYZPassword");
  
                  mailclient.Send(mail);
                  mailclient.Dispose();
              }
              catch (Exception ex)
              {
              }
  
          }
  </code></pre>