{"id":115,"date":"2012-08-11T04:10:15","date_gmt":"2012-08-11T04:10:15","guid":{"rendered":"http:\/\/carla.romere.com\/?p=115"},"modified":"2012-08-11T04:10:15","modified_gmt":"2012-08-11T04:10:15","slug":"sending-a-form-to-html-email-using-c-and-asp-net-4-0","status":"publish","type":"post","link":"http:\/\/carla.romere.com\/?p=115","title":{"rendered":"Sending a Form to HTML Email Using C# and asp.net 4.0"},"content":{"rendered":"<p>I often find myself needing to set up a form to email page on a website.\u00c2\u00a0For whatever reason,\u00c2\u00a0I am not able to always use the same code. So my latest code is included here. This code sends an HTML email from an asp.net 4 website using C#.<\/p>\n<div id=\"code\" style=\"font-family: courier new;\">\n<quote><br \/>\nusing System;<br \/>\nusing System.Net;<br \/>\nusing System.Web.UI.WebControls;<br \/>\nusing System.Net.Mail;<br \/>\nusing System.Text;<\/span><\/p>\n<p>public partial class contact : System.Web.UI.Page<br \/>\n{<br \/>\nprotected void Page_Load(object sender, EventArgs e)<br \/>\n{<br \/>\ntxtFirstName.Focus();<br \/>\n}<\/p>\n<p>protected void btnSendMail_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nstring strEmail = txtEmail.Text;<br \/>\nstring strFirstName = txtFirstName.Text;<br \/>\nstring strLastName = txtLastName.Text;<br \/>\nstring strFullName = (strFirstName + &#8221; &#8221; + strLastName);<br \/>\nstring strAddress = txtAddress.Text;<br \/>\nstring strCity = txtCity.Text;<br \/>\nstring strState = txtState.Text;<br \/>\nstring strZip = txtZip.Text;<br \/>\nstring strPhone = txtPhone.Text;<br \/>\nstring strRequest = txtRequest.Text;<\/p>\n<p>MailAddress fromaddress = new MailAddress(txtEmail.Text, strFullName);<br \/>\nMailAddress toaddress = new MailAddress(&#8220;your email address here&#8221;);<br \/>\nMailMessage eMessage = new MailMessage(fromaddress, toaddress);<br \/>\neMessage.From = fromaddress;<br \/>\neMessage.To.Add(toaddress);<br \/>\n\/\/eMessage.Bcc.Add(&#8220;bcc email address here if desired&#8221;);<\/p>\n<p>StringBuilder ebody = new StringBuilder();<br \/>\nSystem.Net.Mail.SmtpClient Smtp = new System.Net.Mail.SmtpClient(&#8220;your smtp server here&#8221;);<br \/>\nSmtp.EnableSsl = false; \/\/enable if your server requires it<br \/>\nNetworkCredential credentials = new NetworkCredential(&#8220;email@yourdomain.com&#8221;, &#8220;password for email@yourdomain.com&#8221;);<br \/>\nSmtp.Credentials = credentials;<\/p>\n<p><quote>ebody.Append(&#8220;<\/p>\n<table>\n<tbody>\n<tr>\n<td>First Name<\/td>\n<p>&#8220;);<\/quote><br \/>\n<quote>ebody.Append(strFirstName); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/tr>\n<tr>\n<td>Last Name<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strLastName); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>Street Address<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strAddress); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>City<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strCity); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>State<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strState); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>Zip<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strZip); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>Daytime Phone<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strPhone); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>Email Address<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strEmail); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<tr>\n<td>Information Requested<\/td>\n<td>&#8220;); <\/quote><br \/>\n<quote>ebody.Append(strRequest); <\/quote><br \/>\n<quote>ebody.Append(&#8220;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#8220;); <\/quote><\/p>\n<p>eMessage.Body = ebody.ToString();<br \/>\neMessage.Subject = &#8220;Static Subject for Your Emails&#8221;;<br \/>\neMessage.Priority = System.Net.Mail.MailPriority.High;<br \/>\neMessage.IsBodyHtml = true;<\/p>\n<p>try<br \/>\n{<br \/>\nSmtp.Send(eMessage);<br \/>\nResponse.Redirect(&#8220;emailsent.aspx&#8221;);\/\/generic page to send users to when email is sent<br \/>\n}<\/p>\n<p>catch (Exception exc)<br \/>\n{<br \/>\nResponse.Write(&#8220;Send failure: &#8221; + exc.ToString());<br \/>\n}<br \/>\n}<br \/>\n}<\/quote>\n<\/div>\n<p>Hopefully, this will help someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often find myself needing to set up a form to email page on a website.\u00c2\u00a0For whatever reason,\u00c2\u00a0I am not able to always use the same code. So my latest code is included here. This code sends an HTML email from an asp.net 4 website using C#. using System; using System.Net; using System.Web.UI.WebControls; using System.Net.Mail; using System.Text; public partial class contact : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { txtFirstName.Focus(); } protected void btnSendMail_Click(object sender, EventArgs e) {&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/carla.romere.com\/?p=115\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":9,"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":124,"href":"http:\/\/carla.romere.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions\/124"}],"wp:attachment":[{"href":"http:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/carla.romere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}