联系表单错误

时间:2014-01-11 00:01:18

标签: html asp.net forms contact-form

我的网站有联系表格:

index.html

<form id="form" action="contact.aspx" method="POST">
<p><label>Your Name</label><input name="nome" type="text"></p>
<p><label>Your Email</label><input name="email" type="text"></p>
<p><label>Your Message</label><textarea name="mensagem"></textarea></p>
<p><input name="submit" type="submit" value="Submit">
</form>

此表单请求asp.net页面(contact.aspx)

<%@ page language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>

<%
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress("myemail@mydomain.com");
objEmail.To.Add("myemail@mydomain.com");
objEmail.Priority = MailPriority.Normal;
objEmail.IsBodyHtml = true;
objEmail.Subject = "Mysubject";
objEmail.Body = "Mycontent";
SmtpClient objSmtp = new SmtpClient();
objSmtp.Host = "localhost";
objSmtp.Credentials = new NetworkCredential("myemail@mydomain.com", "mypassword");
objSmtp.Send(objEmail);
%>

当有人使用联系表单时,我会收到该电子邮件。然而,在这个主题上,它只出现在“神秘主题”中,而在身体上,它只出现“Mycontent”。 为了接收用户在表单上放置的数据,我需要更改什么?

1 个答案:

答案 0 :(得分:1)

您需要通过名称在Request.Form中找到发布的数据。您的代码应如下所示:

<%@ page language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>

<%
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress("myemail@mydomain.com");
objEmail.To.Add("myemail@mydomain.com");
objEmail.Priority = MailPriority.Normal;
objEmail.IsBodyHtml = true;
objEmail.Subject = Request.Form["subject"];
objEmail.Body = Request.Form["mensagem"];
SmtpClient objSmtp = new SmtpClient();
objSmtp.Host = "localhost";
objSmtp.Credentials = new NetworkCredential("myemail@mydomain.com", "mypassword");
objSmtp.Send(objEmail);
%>

在html中,您缺少“主题”字段。你的标记应该是这样的:

<form id="form" action="contact.aspx" method="POST">
<p><label>Your Name</label><input name="nome" type="text"></p>
<p><label>Your Email</label><input name="email" type="text"></p>
<p><label>Your Subject</label><input name="subject" type="text"></p>
<p><label>Your Message</label><textarea name="mensagem"></textarea></p>
<p><input name="submit" type="submit" value="Submit"></p>
</form>