Copied to clipboard

Flag this post as spam?

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


  • ThomasBrunbjerg 90 posts 182 karma points
    Aug 31, 2017 @ 08:18
    ThomasBrunbjerg
    0

    Contact Form won't send mail using host SMTP client

    I have created a contact form using the guide found here:

    https://our.umbraco.org/documentation/getting-started/Code/Creating-Forms/

    This is my partial form for the form:

    @model LoevbjergRema1000Umbraco.Models.ContactFormViewModel
    
    @using (Html.BeginUmbracoForm("Submit", "Contact"))
    {
        <div class="col-sm-8">
            <div class="row">
                <div class="col-sm-4">
                    <form class="form-inline" id="kontaktForm">
                        <div class="form-group">
                            <label for="">Navn</label>
                            @Html.TextBoxFor(m => m.Navn, new { @class = "form-control", @placeholder = "Indtast navn..." })
                            @Html.ValidationMessageFor(m => m.Navn)
                            <label for="">Addresse</label>
                            @Html.TextBoxFor(m => m.Addresse, new { @class = "form-control", @placeholder = "Indtast addresse..." })
                            <label for="">Postnr. + by</label>
                            @Html.TextBoxFor(m => m.PostNrBy, new { @class = "form-control", @placeholder = "Indtast postnr & by..." })
                            <label for="">Telefon</label>
                            @Html.TextBoxFor(m => m.Telefon, new { @class = "form-control", @placeholder = "Indtast telefonnr..." })
                            <label for="">Email</label>
                            @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @placeholder = "Indtast email..." })
                            @Html.ValidationMessageFor(m => m.Email)
                        </div>
                    </form>
                </div>
                <div class="col-sm-8">
                    <label for="">Skriv din besked</label>
                    @Html.TextAreaFor(m => m.Message, new { @class ="form-control", @id = "kontaktBesked", @rows = "11", @placeholder = "Indtast din besked her..."})
                    @Html.ValidationMessageFor(m => m.Message)
                    <button class="btn_1 pull-right contact-btn" name="submit" type="submit">Afsend Besked</button>
                    <button class="btn_1 contact-btn">Ryd felter</button>
                </div>
            </div>
        </div>
    }
    

    This is my model:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace LoevbjergRema1000Umbraco.Models
    {
        public class ContactFormViewModel
        {
            [Required(ErrorMessage = "Navn påkrævet")]
            public string Navn { get; set; }
    
            public string Addresse { get; set; }
            public string PostNrBy { get; set; }
            public string Telefon { get; set; }
    
            [EmailAddress(ErrorMessage = "Ugyldig email-addresse")]
            public string Email { get; set; }
    
            [Required(ErrorMessage = "Besked påkrævet")]
            public string Message { get; set; }
        }
    }
    

    And this is my controller:

    using LoevbjergRema1000Umbraco.Models;
    using System.Net.Mail;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace LoevbjergRema1000Umbraco.Controllers
    {
        public class ContactController : SurfaceController
        {
            [HttpPost]
            public ActionResult Submit(ContactFormViewModel model)
            {
                if (!ModelState.IsValid) //validation
                    return CurrentUmbracoPage();
    
                // Send mail using SMTP
                MailMessage mailMessage = new MailMessage();
                mailMessage.To.Add("[email protected]");
                mailMessage.From = new MailAddress(model.Email);
                mailMessage.Subject = "Kontakt mail";
                mailMessage.Body = "Navn: " + model.Navn + "\nAddresse: " + model.Addresse + ", " +
                    model.PostNrBy + "\nTelefon: " + model.Telefon + "\n" + model.Message;
    
                SmtpClient smtpClient = new SmtpClient("smtp.curanet.dk");
                smtpClient.Send(mailMessage);
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    But when i submit the form, it only refreshes the page, no mail is being sent. Is the guide outdated? Is there a better way to achieve this? The contact form is really simple and should just clear the fields and perhaps give a small notification saying the mail was sent to the user. I don't have any client side validation at the moment, but i plan on implementing that later on. The server side validation I have now doesn't seem to work either.

  • Richard Hamilton 79 posts 169 karma points
    Aug 31, 2017 @ 09:59
    Richard Hamilton
    0

    Where are your credentials for username (email address), password ? Have you deliberatly hidden them or have you missed them out of the code?

  • ThomasBrunbjerg 90 posts 182 karma points
    Aug 31, 2017 @ 10:02
    ThomasBrunbjerg
    0

    The user is not required to login to send a mail using this form. No username or password is needed.

  • Richard Hamilton 79 posts 169 karma points
    Aug 31, 2017 @ 13:56
    Richard Hamilton
    0

    ok, I have never heard of that before

  • Micha Somers 134 posts 597 karma points
    Aug 31, 2017 @ 11:49
    Micha Somers
    1

    It is likely that your hosting provider blocks this email and requires an email address that matches your domain.

    And even if it is not blocked by the provider, in order to prevent misuse, it is also not recommended to use the visitor's e-mail address as from address this way. So it's better to remove:

    mailMessage.From = new MailAddress(model.Email);
    

    Instead use an e-mail address as sender that matches your domain: something like: [email protected]. These specific email account settings can be configured in web.config:

    <system.net>
            <mailSettings>
            <smtp from="[email protected]">
            <network host="smtp.curanet.dk" userName="xxxxxx" password="xxxxxx" />
          </smtp>
            </mailSettings>
    </system.net>
    
  • ThomasBrunbjerg 90 posts 182 karma points
    Aug 31, 2017 @ 12:09
    ThomasBrunbjerg
    0

    On the host site it says that "Username and Password" are not required to use this server". Can I delete the attributes from the network host?

  • Micha Somers 134 posts 597 karma points
    Aug 31, 2017 @ 12:31
Please Sign in or register to post replies

Write your reply to:

Draft