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.
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:
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:
This is my model:
And this is my controller:
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.
Where are your credentials for username (email address), password ? Have you deliberatly hidden them or have you missed them out of the code?
The user is not required to login to send a mail using this form. No username or password is needed.
ok, I have never heard of that before
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:
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:
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?
Yes, there are several options available for mailsettings.
See: https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/smtp-element-network-settings
is working on a reply...