Perhaps it is an easy question but I am new in developing in c#, asp.net. I am building a website and I have some troubles on the Contact page. I've been searching for a long time but I couldn't find a suitabe answer. My controller code is the following:
namespace SITEEXEMPLO.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name, string EmailId, string PhoneNo, string Subject, string Message)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(EmailId);
mail.From = new MailAddress("xoinas23@gmail.com");
mail.Subject = Subject;
string userMessage = "";
userMessage = "<br/>Name :" + Name;
userMessage = userMessage + "<br/>Email Id: " + EmailId;
userMessage = userMessage + "<br/>Phone No: " + PhoneNo;
userMessage = userMessage + "<br/>Message: " + Message;
string Body = "Hi, <br/><br/> A new enquiry by user. Detail is as follows:<br/><br/> " + userMessage + "<br/><br/>Thanks";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
//SMTP Server Address of gmail
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("xoinas23@gmail.com", "xxxxx");
// Smtp Email ID and Password For authentication
smtp.EnableSsl = true;
smtp.Send(mail);
ViewBag.Message = "Thank you for contacting us.";
}
catch
{
ViewBag.Message = "Error............";
}
return View();
}
And the HTML code is the following:
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table width="100%;" border="1">
<tr>
<td align="right" colspan="2" style="text-align: center"><strong>SEND EMAIL USING GMAIL ACCOUNT IN MVC</strong></td>
</tr>
<tr>
<td align="right"> </td>
<td align="left">
@ViewBag.Message
</td>
</tr>
<tr>
<td align="right">Name :</td>
<td align="left">
<input id="txtName" name="Name" width="250px;" />
</td>
</tr>
<tr>
<td align="right">Your Email id :</td>
<td align="left">
<input id="txttoaddress" name="EmailId" width="250px" />
</td>
</tr>
<tr>
<td align="right">Phone No :</td>
<td align="left">
<input id="txtPhoneno" name="PhoneNo" width="250px" />
</td>
</tr>
<tr>
<td align="right">Subject :</td>
<td align="left">
<input id="txtsubject" name="Subject" width="250px"></input>
</td>
</tr>
<tr>
<td align="right">Message :</td>
<td align="left">
<textarea rows="4" cols="50" name="Message" id="txtmessage"></textarea>
</td>
</tr>
<tr>
<td align="right">Attachment :</td>
<td align="left">
<input type="file" name="file" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" text="Send Message" />
</td>
</tr>
</table>
This works but not the way I want. By running this code, I insert the information and I receive it on the email inserted on the form, not the email in the code. I want the users to write their own email on the form, and by clicking the button I receive the information on my email. Can someone help me?