-1

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">&nbsp;</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?

  • 1
    Have you tried anything at all? Where is your server-side code? When you did a Google search for "asp.net send email" or "c# send email", what did you find? How did you try to implement that functionality and what didn't work? Currently this is too broad for a Stack Overflow question, you're essentially asking for a tutorial. – David May 15 '18 at 14:35
  • possible duplicate https://stackoverflow.com/questions/26784366/how-to-send-email-from-mvc-5-application – Nerevar May 15 '18 at 14:35
  • `action="mailto:someone@example.com"` - That will attempt to open the clients default email program and have it open for them to send. If you want to just email the results without client intervention you need a mail server. – Jimenemex May 15 '18 at 14:36
  • 1
    You need a mail server, if you work on azure SendGrid is free for 20k mails per month or so (back when I tried it). – Nerevar May 15 '18 at 14:36
  • @David If I had found something I would not be asking right? Come on.. – Antonio Choinas May 15 '18 at 14:54
  • @AntonioChoinas: So when you searched Google for "asp.net send email" or "C# send email" you want us to believe that you found *no results*? What you're looking for are introductory tutorials on ASP.NET and basic examples of the functionality you're trying to implement. Those aren't found on Stack Overflow. A thinly veiled question pretending to have made an effort isn't going to trick this community into doing all of your work for you. – David May 15 '18 at 14:56
  • I repeat, in case you can't see well, if I had found something that suits my question I would not be asking this. – Antonio Choinas May 15 '18 at 15:06
  • @AntonioChoinas: Repeating an off-topic request doesn't make it on-topic. It would appear that you have a history of asking poorly-received questions. You are encouraged to start here: https://stackoverflow.com/tour To put it simply, you are currently asking us to do *all of your work* for you. That's not what we do. If you could demonstrate *what you've tried* and *where you're stuck* then we can help with that. Your question shows no attempt whatsoever, and no indication of any ASP.NET functionality. Start with some tutorials on ASP.NET in general. *Try something.* – David May 15 '18 at 15:09
  • Allright. I updated my question. See if you understand my problem please – Antonio Choinas May 15 '18 at 16:13

1 Answers1

1

A simple HTML page is not going to allow you to send email, even when setting the action to point to the page itself. You need to have some logic on the server to receive your form submission, process it, and send out the email.

Simply searching contact page in c# in a common search engine will provide dozens of tutorials and videos to walk you through the process. One example of How to create Contact Us Page in ASP.Net at ASPSnippets.com is a more simplified example. There is a more complicated version at Codeproject.com shows How to Implement Contact Us Page in ASP.NET MVC (ASP.NET 5 )

Edit:

Looking at what you are asking, you want to figure out how to have the user send you a message from their email address. It looks like the problem you have is a couple of parameters are flipped around.

You have:

...
mail.To.Add(EmailId);
mail.From = new MailAddress("xoinas23@gmail.com");
mail.Subject = Subject;
...

This will set the To for the email to the address the user entered. This will direct the email to them and not the address to specified in code.

But you need:

...
mail.To.Add("xoinas23@gmail.com");
mail.From = new MailAddress(EmailId);
mail.Subject = Subject;
...

This will set the email as being From the address the user entered and sending it To the address you specied in code.

Justin Pearce
  • 4,994
  • 2
  • 24
  • 37