Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2017
    Posts
    51
    Thanks
    2
    Thanked 0 Times in 0 Posts

    collecting data from a form and sending it to an email

    I am trying to allow a user to fill out a form and then when they are done submit it, which will email the data to the appropriate person. This is an ASP.NET MVC project and is using a Content Management System (Contentful). From what I have read, it looks like I need to use the MailTo Action in the <form> tag. But the problem is that I can't just put an email address in there because it will be a different address depending on the form that is loaded (We have one OnlineForms View that is loaded dynamically by using the CMS). So I need to put something like mailto: @Model.MailTo but it gives me an error trying to do it this way. Are there other options better suited for an ASP.net project?


    UPDATE: So after doing some more research, I found that the new code below is probably where I need to be looking at for this. I'm just not entirely sure how to finish the method. Any help would be appreciated.

    Code:
    [HttpPost]
            public async Task<IActionResult> Insert(IFormCollection data)
            {
                //loop through the IFormCollection and put all data into an email and send it
                foreach (var input in data)
                {
                    
                }
            }
    Last edited by ethancodes; 10-31-2017 at 03:51 PM.

  2. #2
    New Coder
    Join Date
    Jan 2013
    Posts
    88
    Thanks
    2
    Thanked 6 Times in 6 Posts
    OK so youre on the right track, you want to use your controller to send your mail.

    I use a tool called MvcMailer to manage all of my email within a web application: https://github.com/smsohan/MvcMailer

    Im not on my PC right now so can't send the exact code, there are loads of examples on StackOverflow and other tutorials,

    Here is an example using a contact form: https://www.mikesdotnetting.com/arti...in-asp-net-mvc
    I would personally not store your SMTP credentials within the controller as this firstly isn't the most secure and wouldnt scale well as the application gets larger, instead use a service as demonstrated in this example: https://code.msdn.microsoft.com/ASPN...l-and-09e6cf76

    StackOverflow example: https://stackoverflow.com/questions/...-5-application
    Last edited by Exodus_AU; 11-13-2017 at 08:46 AM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •