Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sam 75 posts 391 karma points
    Feb 12, 2016 @ 20:59
    Sam
    0

    Problems With Surface Cotroller for sending email from form

    Hello,

    I am completely new to the Models and Surface Controllers. So I have been following a tutorial about creating a contact form that will send an email. I have followed the tutorial to the letter, but I am getting a couple errors.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Text; 
    using System.Web.Http;
    using System.Web.Mvc;
    using Umbraco.Forms.Mvc.DynamicObjects;
    using Umbraco.Web.Mvc;
    
    namespace Umbraco.Logic.Controllers
    {
    public class ContactSurfaceController : SurfaceController
    {
        [HttpPost]
        public ActionResult Contact(ContactModel model)
        {
            if (ModelState.IsValid)
            {
                var sb = new StringBuilder();
                sb.AppendFormat("<p>Name: {0}</p>", model.Name);
                sb.AppendFormat("<p>Email: {0}</p>", model.Email);
                sb.AppendFormat("<p>Phone: {0}</p>", model.Phone);
                sb.AppendFormat("<p>Company: {0}</p>", model.Company);
                sb.AppendFormat("<p>{0}</p>", model.Message);
                Library.SendMail("mail@mail.com", "mail@mail.com", "Contact Form", sb.ToString(), true);
    
                return RedirectToUmbracoPage(model.ThankYou);
            }
    
            return CurrentUmbracoPage();
        }
    }
    }
    

    Error 1: [HttpPost] - The Type or Namespace name 'HttpPost' could not be found.

    Error2: Library.SendMail - 'Umbraco.Forms.MVC.DynamicObjects.Lirary' Does not contain a deffinition for 'SendMail'

    How do I correct these errors. Thanks in advance.

  • Marc Goodson 1154 posts 7677 karma points MVP 5x c-trib
    Feb 13, 2016 @ 09:40
    Marc Goodson
    100

    Hi Sam

    These problems look like namespace resolution issues; ie the methods you are trying to use are from different libraries, and your code is trying to use the wrong one. Or at least that is my guess :-)

    HttpPost attribute, exists in both System.Web.Mvc and System.Web.Http (and you have both namespaces referenced, so there maybe some abiguity - System.Web.Mvc is for Surface Controllers and System.Web.Http is for API controllers) So if you change:

    [HttpPost]
    

    to be

    [System.Web.Mvc.HttpPost]
    

    it will remove this abiguity (or remove the using statement for System.Web.Http)

    Similarly there is a Library class in Umbraco.Forms.MVC.DynamicObjects but I'm guessing it's not responsible for sending mail.

    However there is a SendMail function in the old (legacy umbraco.library namespace - most of these have been moved to the Umbraco. Helper class - but not SendMail as far as I can see)...

    eg

    umbraco.library.SendMail("youremailstuffhere");
    

    (lowercase u and l)

    the hard bit in visual studio is typing the old umbraco namespace with the lowercase u, as it always tries to correct this to the uppercase U !! - but you can cut and paste it in to hopefully get your example to work.

    I'd consider though using the c# SMTPClient in System.Net.Mail to send emails though: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx

    regards

    Marc

  • Sam 75 posts 391 karma points
    Feb 13, 2016 @ 20:05
    Sam
    0

    Thanks for the Info. That cleared up my understanding of namespaces and such. I also took your advice and found a tutorial using the SMTP Client in System.Net.Mail.

    https://wheeler.kiwi.nz/blog/quick-and-easy-umbraco-7-razor-contact-form/ It helped me clean up my controller a little more.

    Unfortunately, I am still not emailing. On submission I get a server error.

    Server Error in '/' Application. The resource cannot be found.

    Do you have any Insight to this??? could this be an issue with my SMTP configuration in web.config?

    Here is the current control.

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Net.Mail;
    using System.Text;
    using System.Web.Http;
    using System.Web.Mvc;
    using Umbraco.Forms.Mvc.DynamicObjects;
    using Umbraco.Models;
    using Umbraco.Web.Mvc;
    
    namespace Umbraco.Controllers
    {
    public class ContactSurfaceController : SurfaceController
    {
        [ChildActionOnly]
        public ActionResult ContactForm()
        { 
            //in case you need it
            var currentNode = Umbraco.TypedContent(UmbracoContext.PageId.GetValueOrDefault());
    
            var model = new ContactModel();
            return PartialView("ContactForm", model);
        }
    
    
        [System.Web.Mvc.HttpPost]
        public ActionResult ContactForm(ContactModel model)
        {
            if (ModelState.IsValid)
            {
                var sb = new StringBuilder();
                sb.AppendFormat("<p>Name: {0}</p>", model.Name);
                sb.AppendFormat("<p>Email: {0}</p>", model.Email);
                sb.AppendFormat("<p>Phone: {0}</p>", model.Phone);
                sb.AppendFormat("<p>Company: {0}</p>", model.Company);
                sb.AppendFormat("<p>{0}</p>", model.Message);
    
                SmtpClient smtp = new SmtpClient();
                MailMessage message = new MailMessage();
    
                message.To.Add(new    MailAddress(ConfigurationManager.AppSettings["ContactAddress"]));
                message.Sender = new MailAddress(model.Email);
                message.Body = sb.ToString();
                message.IsBodyHtml = true;
    
    
                try
                {
                    smtp.Send(message);
                }
                catch (SmtpException)
                { 
                //log or manage your error here, then...
                    return RedirectToUmbracoPage(1337); // <-My published error page.
                }
    
                return RedirectToUmbracoPage(1336); // <-My published succes page.
            }
    
            return CurrentUmbracoPage();
        }
    }
    }
    
  • Marc Goodson 1154 posts 7677 karma points MVP 5x c-trib
    Feb 14, 2016 @ 01:03
    Marc Goodson
    0

    Hi Sam

    What is the markup for your form ?

    ie are you using:

    @Html.BeginUmbracoForm<ContactSurface>("ContactFrom"){
    
    
    }
    

    Can you put a breakpoint in visual studio on you post handling action, and see if it is hit when the for is posted back ?

    and if it is hit, step through each line of code until you get the error ?

    then you'll get more of a clue if it is the smtp config at fault or the posting back bit...

  • Sam 75 posts 391 karma points
    Feb 19, 2016 @ 20:11
    Sam
    0

    Marc,

    Thanks for the reply again. This was one of many issues I found since your last message. I only had ("Contact") where it should have been ("ContactFrom").

    I am no longer getting any error, which is great, but now the email is simply not sending. I get redirected to my error page. I have tired a couple SMTP accounts.

    do I need to do more to reference the smtp account settings in the web.config file?

    SmtpClient smtp = new SmtpClient();
    
  • Marc Goodson 1154 posts 7677 karma points MVP 5x c-trib
    Feb 20, 2016 @ 21:55
    Marc Goodson
    0

    Hi Sam,

    It depends on what you are using for a SMTP service

    but something like this if you were using sendgrid:

      <system.net>
        <mailSettings>
          <smtp deliveryMethod="network" from="info@yourdomain.co.uk">
            <network host="smtp.sendgrid.com" userName="info@yourdomain.co.uk" password="yourpasswordhere" />
          </smtp>
        </mailSettings>
      </system.net>
    
Please Sign in or register to post replies

Write your reply to:

Draft