In the past I have used something like this to send form fields in an E-mail form asp.net, I am trying to format more the html email but I dont know how to pass the textbox value into the message can someone help me?
//OLD WAY
message.Body += "<b>Preferred contact method: </b> " + drp_contact_method.SelectedValue + "<br/>";//
protected void btnAction_Click(object sender, EventArgs e)
{
if ((Page.IsValid))
{
if (dpAction.SelectedValue.ToString()=="Submit" )
{
// define SMTP client
SmtpClient smtp = new SmtpClient("IP HERE");
//smtp.EnableSsl = true;
// smtp.UseDefaultCredentials = true;
//create the mail message
MailMessage message = new MailMessage();
//From address will be given as a MailAddress Object
message.From = new MailAddress("myemail@domain.com");
//To address collection of MailAddress
message.To.Add("ric.gutierrez@domain.com");
message.Subject = "Pre-Registration Form";
// Change to false to not include HTML
string bodyHTML = string.Empty;
string bodyPlain = string.Empty;
message.IsBodyHtml = true;
bodyHTML = @"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<html lang='en'>
<head>
</head>
<body>
<table style='width:800px; border:1px solid #000;' cellpadding='10' cellspacing='0' align='center'>
<tr>
<td colspan='2' style='background-color:#009797; border-bottom:1px solid #000;'>
<h1 style='text-align:center; color:#FFF;'>Pre-Registration Information</h1></td>
</tr>
<td style='width:600px; vertical-align:top;'>
<h4 style='font-family:Arial, Helvetica, sans-serif; color:#000;'><u>PATIENT INFORMATION</u></h4>
<p style='font-family:Arial, Helvetica, sans-serif; font-size:.8em; color:#000; line-height:1.5em;'>Last Name:</p>
+ txtLastName.Text +
I think the cleanest way to do this is to use
string.Format. First define your HTML as you currently have inbodyHTML, but put placeholders like{0}and{1}in the string. Then, plug in your form variables like this:That will substitute
{0}withyourTextBox.Textand{1}withyourOtherTextBox.Text, with both values encoded properly for the HTML.