Search code examples
smtpclientumbraco7

Umbraco7 SMTP Success message callback ERROR


The form is sending but the problem is the Success message not showing even though the ex.Message Value is Null it should be printing the "Form submitted successfully." but it did not.

And when the ex.Message is not Null .. the error message is working it print "Error submiting message."

need help, Thanks in advance!

Controller.cs

if (!ModelState.IsValid)
       return CurrentUmbracoPage();

MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New Contact request";
message.From = new System.Net.Mail.MailAddress(model.Email, model.Name);
message.Body = model.Message;
SmtpClient smtp = new SmtpClient("mail.domain.com", 26);
try
{
    smtp.Send(message); 
}
catch (Exception ex)
{

    if (ex.Message == null)
    {
        ViewBag.Success = "Form submitted successfully.";
    }
    else {
        ViewBag.ErrorMessage = "Error submiting message.";
    }
    return CurrentUmbracoPage();
}

Solution

  • If the email is sent successfully there is no exception to catch, which is probably why your message does not show up.

    Instead, move that message to the try block after .Send, that should do it.

    EDIT: Also, I believe that your return statement should be moved outside the try-catch block.