过去我使用过这样的东西来发送电子邮件表单asp.net中的表单字段,我试图格式化更多的html电子邮件,但我不知道如何将文本框值传递给消息可以有人帮帮我?
//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 +
答案 0 :(得分:0)
我认为最简单的方法是使用string.Format
。首先在bodyHTML
中定义您当前的HTML,但在字符串中添加{0}
和{1}
等占位符。然后,插入您的表单变量,如下所示:
var body = string.Format(
bodyHTML,
HttpUtility.HtmlEncode(yourTextBox.Text),
HttpUtility.HtmlEncode(yourOtherTextBox.Text));
这会将{0}
替换为yourTextBox.Text
,将{1}
替换为yourOtherTextBox.Text
,并为HTML正确编码这两个值。