我现在正在尝试使用ASP.NET和Razor,我试图采用一种简单的HTML形式,并使其将数据传递给POST上的变量,并使用这些变量来构建由SMTP发送的简单电子邮件。但是,似乎没有将数据传递给这些变量,因为发送电子邮件时它们为空白。如果我将值硬编码到这些变量中,它们将正确显示。这是我正在使用的代码的一小段。忽略WebMail.SmtpServer下的localhost,我正在使用SMTP4dev在本地测试电子邮件功能。第一部分是剃刀代码:
@{
var errorMessage = "";
if (IsPost)
{
var fromEmail = "user@localhost";
var body = Request["requestDate"];
var requestorName = Request["requestorName"];
var subject = requestorName;//( "IT Account Request for "+ @requestorName +" ." );
try {
WebMail.SmtpServer = "localhost";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "user@localhost";
WebMail.Password = "smtppassword";
WebMail.From = fromEmail;
WebMail.Send(to: "test@localhost",
subject: subject,
body: body
);
}
catch (Exception ex ) {
errorMessage = ex.Message;
}
}
}
<div>Error: @errorMessage </div>
<form action="" method="post" enctype="text/plain">
<table id="requestor_information_form">
<tr>
<td class="top"><img src="images\logo.png"></img></td>
<td class="top">Account Request</td>
<td class="top" colspan ="2"></td>
</tr>
<tr>
<td class="tableHeading" colspan="4">Requestor Information</td>
</tr>
<tr>
<td>Requestor:</td>
<td><input type="text" name="requestorName"></td>
<td>Request Date:</td>
<td><input type="date" name="requestDate"></td>
</tr>
<tr>
<td>Requestor Phone:</td>
<td><input type="text" name="requestorPhoneNum"></td>
</tr>
</form>
您需要enctype
从表单中删除或将其设置application/x-www-form-urlencoded
为默认值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句