title: how to send email with alternate views either plain text or HTml templates.?
body: <p>I want to send email with alternate views either plain text or html template, i have used code from the following link
  <a href="http://www.search-this.com/2009/02/05/aspnet-sending-email-both-in-html-and-plain-text/" rel="nofollow">aspnet-sending-email-both-in-html-and-plain-text</a> but i dont know why its not working for me.. my code is:.</p>
  
  <pre><code>  public void EmailSendingWithBothHTMLAndPlainText()
  {
      // CREATE EMAIL
      // first we create a plain text version and set it to the AlternateView
      // then we create the HTML version
      System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
  
      msg.From = new MailAddress("visionstask@gmail.com");
      msg.Subject = "Event: testing";
      msg.To.Add("ram@tsgdev.net");
  
      // create a string to hold all email addresses
      StringBuilder sbEmailTo = new StringBuilder();
      sbEmailTo.Append("visionstask@gmail.com");
  
      //if (!String.IsNullOrEmpty(TextBoxEmail2.Text)) { msg.To.Add(TextBoxEmail2.Text); sbEmailTo.Append(", " + TextBoxEmail2.Text); }
      //if (!String.IsNullOrEmpty(TextBoxEmail3.Text)) { msg.To.Add(TextBoxEmail3.Text); sbEmailTo.Append(", " + TextBoxEmail3.Text); }
      //if (!String.IsNullOrEmpty(TextBoxEmail4.Text)) { msg.To.Add(TextBoxEmail4.Text); sbEmailTo.Append(", " + TextBoxEmail4.Text); }
      //if (!String.IsNullOrEmpty(TextBoxEmail5.Text)) { msg.To.Add(TextBoxEmail5.Text); sbEmailTo.Append(", " + TextBoxEmail5.Text); }
  
  
  
      //now create the HTML version
      MailDefinition message = new MailDefinition();
      message.BodyFileName = "RegisterUser.html";
      message.IsBodyHtml = true;
      message.From = "visionstask@gmail.com";
      message.Subject = "Zoo Event: " + "testing for HTML";
  
      //embed images for the email
      //EmbeddedMailObject emo = new EmbeddedMailObject();
      //emo.Path = @"~\Images\email\hdr_roar.gif";
      //emo.Name = "hdr";
  
      //EmbeddedMailObject emo2 = new EmbeddedMailObject();
      ////emo2.Path = @"~\Images\email\box_top.gif";
      //emo2.Name = "box_top";
  
      //message.EmbeddedObjects.Add(emo);
      //message.EmbeddedObjects.Add(emo2);
  
      //Build replacement collection to replace fields in email.htm file
      ListDictionary replacements = new ListDictionary();
      replacements.Add("@Name", "Ram Singh");
  
  
      //now create mail message using the mail definition object
      //the CreateMailMessage object takes a source control object as the last parameter,
      //if the object you are working with is webcontrol then you can just pass "this",
      //otherwise create a dummy control as below.
      //System.Net.Mail.MailMessage msgHtml = message.CreateMailMessage(sbEmailTo.ToString(), replacements, new LiteralControl());
  
      //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
  
  
      // msg.AlternateViews.Add(htmlView);
  
  
      String plainEmail = "&lt;table width='100%' border='0' cellspacing='0' cellpadding='0'&gt;&lt;tr&gt;&lt;td align='center'&gt;&lt;table width='600' bgcolor='#f3f3f3' border='0' cellpadding='0' cellspacing='0'&gt;&lt;tr&gt;&lt;td&gt;&lt;img src='http://localhost:4823/WishIsDone/Images/emaillogo.png' style='margin-left: 50px;'alt='' /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 5px;' cellpadding='0' cellpadding='0'&gt;&lt;tr&gt;&lt;td style='font-size: 18px; font-family: Verdana, Geneva, sans-serif; color: #008fbb;'&gt;Registration&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 20px;'&gt;&lt;tr&gt;&lt;td&gt;Hello @Name!&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif;'&gt;Welcome to WishIsDone Website &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 12px; color: #008fbb; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;p&gt;You have successfully registrated.&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif; color: #999;'&gt;&lt;p&gt;Verify your e-mail address to complete your registration by clicking the link:&lt;/p&gt; &lt;p&gt;&lt;a href='#'&gt;@link&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong style='color: #000; font-size: 10px; font-weight: bold;'&gt;Note:&lt;/strong&gt;&lt;span style='color: #333;'&gt;please change your password after login.&lt;/span&gt;&lt;/p&gt;&lt;table style='border-top: 1px dotted #999; margin: auto;' width='450'&gt;&lt;tr&gt;&lt;td style='font-size: 10px; color: #333;'&gt;&lt;p&gt;Regards,&lt;br /&gt; Wish Is Done Team&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style='font-size: 10px; color: #333; padding: 10px 50px; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;p&gt; For any queries or difficulties Contact:&lt;br /&gt; Email:&lt;a href='#'&gt;info@wishisdone.com&lt;/a&gt;&lt;br /&gt; Toll free: XXX-XXX-XXXX&lt;br /&gt; &lt;a href='#'&gt;www.wishisdone.com&lt;/a&gt;&lt;/p&gt; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table style='border-top: 1px dotted #999; width: 500px; color: #333; margin: auto; font-size: 9px; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;tr&gt;&lt;td&gt;Copyright &amp;copy; 2012 wishisdone&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;";
  
      //first we create the Plain Text part
      AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainEmail, null, "text/plain");
      msg.AlternateViews.Add(plainView);
      msg.IsBodyHtml = true;
      SmtpClient emailClient = new SmtpClient("smtp.gmail.com");
      System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("test@gmail.com", "test");
      emailClient.UseDefaultCredentials = false;
      emailClient.Credentials = SMTPUserInfo;
      emailClient.Port = 587;
      emailClient.EnableSsl = true;
      //SmtpClient emailClient = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString());
      //emailClient.UseDefaultCredentials = false;
      //emailClient.EnableSsl = true;
  
      emailClient.Send(msg);
  }
  </code></pre>
  
  <p>and i am getting the following outout in mail :</p>
  
  <pre><code>&lt;table width='100%' border='0' cellspacing='0' cellpadding='0'&gt;&lt;tr&gt;&lt;td align='center'&gt;&lt;table width='600' bgcolor='#f3f3f3' border='0' cellpadding='0' cellspacing='0'&gt;&lt;tr&gt;&lt;td&gt;&lt;img src='http://localhost:4823/WishIsDone/Images/emaillogo.png' style='margin-left: 50px;'alt='' /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 5px;' cellpadding='0' cellpadding='0'&gt;&lt;tr&gt;&lt;td style='font-size: 18px; font-family: Verdana, Geneva, sans-serif; color: #008fbb;'&gt;Registration&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table bgcolor='#FFFFFF' border='0' width='500' align='center' style='margin: auto; border: 1px solid #999; padding: 20px;'&gt;&lt;tr&gt;&lt;td&gt;Hello @Name!&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif;'&gt;Welcome to WishIsDone Website &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 12px; color: #008fbb; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;p&gt;You have successfully registrated.&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt;&lt;td style='font-size: 11px; font-family: Verdana, Geneva, sans-serif; color: #999;'&gt;&lt;p&gt;Verify your e-mail address to complete your registration by clicking the link:&lt;/p&gt; &lt;p&gt;&lt;a href='#'&gt;@link&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong style='color: #000; font-size: 10px; font-weight: bold;'&gt;Note:&lt;/strong&gt;&lt;span style='color: #333;'&gt;please change your password after login.&lt;/span&gt;&lt;/p&gt;&lt;table style='border-top: 1px dotted #999; margin: auto;' width='450'&gt;&lt;tr&gt;&lt;td style='font-size: 10px; color: #333;'&gt;&lt;p&gt;Regards,&lt;br /&gt; Wish Is Done Team&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style='font-size: 10px; color: #333; padding: 10px 50px; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;p&gt; For any queries or difficulties Contact:&lt;br /&gt; Email:&lt;a href='#'&gt;info@wishisdone.com&lt;/a&gt;&lt;br /&gt; Toll free: XXX-XXX-XXXX&lt;br /&gt; &lt;a href='#'&gt;www.wishisdone.com&lt;/a&gt;&lt;/p&gt; &lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;table style='border-top: 1px dotted #999; width: 500px; color: #333; margin: auto; font-size: 9px; font-family: Verdana, Geneva, sans-serif;'&gt; &lt;tr&gt;&lt;td&gt;Copyright &amp;copy; 2012 wishisdone&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;
  </code></pre>
  
  <p>Please help me..i want to display it the well formatted way, because of that i want to use alternate views.</p>