Reputation: 105
I have a website which has a 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>
This forms requests a asp.net page (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("[email protected]");
objEmail.To.Add("[email protected]");
objEmail.Priority = MailPriority.Normal;
objEmail.IsBodyHtml = true;
objEmail.Subject = "Mysubject";
objEmail.Body = "Mycontent";
SmtpClient objSmtp = new SmtpClient();
objSmtp.Host = "localhost";
objSmtp.Credentials = new NetworkCredential("[email protected]", "mypassword");
objSmtp.Send(objEmail);
%>
When someone uses the contact form, I can receive the email. However, on the subject, it only appears "Mysubject" and, on the body, it only appears "Mycontent". What do I have to change in order to receive the data that the user put on the form?
Upvotes: 0
Views: 132
Reputation: 7943
You need to find the posted data in Request.Form by their name. Your code should look like this:
<%@ page language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<%
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress("[email protected]");
objEmail.To.Add("[email protected]");
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("[email protected]", "mypassword");
objSmtp.Send(objEmail);
%>
In the html you are missing the "subject" field. Your markup should be like this:
<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>
Upvotes: 1