Sending Email from ASP.NET and Kentico Without Hiding Delivery Failure

Editorial illustration.

Originally published 26 June 2023; rewritten and reviewed on 15 October 2026.

A visitor fills in a “Contact Us” form, clicks Submit, and sees a reassuring green banner: Thank you, we will get back to you shortly. Behind the scenes, the SMTP relay rejected the message with an authentication error or a blocked port. Because the code-behind wrapped Send() in an empty try/catch—or assumed sending succeeded—nobody realises enquiries are vanishing until a client asks why the inbox has been quiet for two weeks.

When you build email dispatch in ASP.NET or Kentico, the goal is not just getting a test email to arrive on localhost. The goal is making delivery observable and recoverable when the mail server inevitably hiccups in production.

Separate form acceptance from email delivery

Treat “we saved the user’s submission” and “the SMTP server accepted the email” as two distinct events. If a form submission matters to the business—a sales lead, a support ticket, or an order notification—persist the record to your database first. Only after that write succeeds should you hand the notification off to an email queue or delivery service.

In Kentico (CMS 11, 12, or Xperience 13), avoid bypassing the built-in email engine with a raw SmtpClient call unless you have a specific architectural reason. Kentico’s CMS.EmailEngine writes messages to the CMS_Email queue table, tracks send attempts, and exposes failed emails in the Email queue application inside the admin interface:

using CMS.EmailEngine;
using CMS.MacroEngine;

var email = new EmailMessage
{
    EmailFormat = EmailFormatEnum.Html,
    From = "no-reply@yourdomain.com.my",
    Recipients = "sales@yourdomain.com.my",
    ReplyTo = validatedUserEmail,
    Subject = "New website enquiry"
};

var template = EmailTemplateProvider.GetEmailTemplate("Custom.ContactEnquiry", SiteContext.CurrentSiteName);
var resolver = MacroContext.CurrentResolver.CreateChild();
resolver.SetNamedSourceData("CustomerName", sanitizedName);
resolver.SetNamedSourceData("MessageBody", WebUtility.HtmlEncode(rawMessage));

// Queues into CMS_Email for reliable background dispatch and retry
EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, email, template, resolver, true);

Notice ReplyTo vs From. A common mistake in legacy ASP.NET forms is setting From to the visitor’s Gmail or Yahoo address. Modern mail providers (Microsoft 365, Google Workspace, Amazon SES, Postmark, and SendGrid) enforce SPF, DKIM, and DMARC strictly; spoofing the visitor’s address in From will get your message rejected or dumped straight into spam. Always send from a verified domain address you control, and put the visitor’s email in ReplyTo.

Keep transport credentials out of code-behind

In standard ASP.NET WebForms or MVC applications without Kentico’s queue, keep hostnames, ports, SSL flags, and credentials in configuration rather than instantiating new SmtpClient("smtp.example.com", 587) with hardcoded passwords inside an .aspx.cs event handler.

For .NET Framework (Web.config), <system.net><mailSettings> lets SmtpClient pick up environment-specific settings via Web.Release.config transforms:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network" from="no-reply@yourdomain.com.my">
      <network host="smtp.postmarkapp.com"
               port="587"
               userName="SMTP_TOKEN_KEY"
               password="SMTP_TOKEN_SECRET"
               enableSsl="true" />
    </smtp>
  </mailSettings>
</system.net>

For newer .NET projects, Microsoft recommends MailKit over System.Net.Mail.SmtpClient (which is marked obsolete for new development). Whichever client you maintain, dispose of MailMessage and client instances properly, and if you are on ASP.NET WebForms or MVC, prefer SendMailAsync where the page or controller supports async execution so a slow SMTP handshake does not tie up an IIS worker thread.

Log failures without leaking secrets

Never swallow SmtpException silently. Log the exception type, SmtpStatusCode, target template or workflow name, and the local database record ID so support can trace which enquiry failed and resend it. At the same time, never log SMTP passwords, API tokens, or sensitive user payloads in plain text logs.

Before deploying a release, test the unhappy paths intentionally:

  • Invalid credentials or expired API key: Confirm the error is logged clearly and the user’s submission is still safely stored in the database.
  • Blocked outbound port: Many cloud VMs (AWS, Azure, DigitalOcean) block outbound port 25 by default; verify your production host can reach port 587 or 465, or use an HTTPS-based transactional email API instead of raw SMTP.
  • Malformed recipient input: Validate email addresses server-side before passing them to MailAddress or EmailMessage, otherwise a single bad address in a comma-separated list can throw FormatException and abort the whole request.

When your application distinguishes between “enquiry saved” and “notification queued or failed”, users get an honest response and your team stops discovering broken email forms weeks after launch.