Email sending in Umbraco 13

Umbraco
Backoffice
C#
April 2024

Sending custom emails is a common requirement for many websites, whether for contact forms, order confirmations, or other notifications. With Umbraco 13, leveraging its flexible architecture and the .NET Core ecosystem, implementing custom email functionality is straightforward and powerful. In this blog, we’ll walk you through the process of setting up and sending custom emails in Umbraco 13.

 

Step 1: Configure SMTP Settings

To send emails, you need to configure the SMTP settings in your appsettings.json file. Add the following section:

"Smtp": {
  "Host": "smtp.your-email-provider.com",
  "Port": 587,
  "EnableSsl": true,
  "Username": "your-email@example.com",
  "Password": "your-email-password"
}

Step 2: Create an Email Sending Service

Create a new service class to handle email sending. Here’s an example:

using System.Net;
using System.Net.Mail;
using Microsoft.Extensions.Configuration;

namespace YourNamespace.Services
{
    public interface IEmailService
    {
        Task SendEmailAsync(string to, string subject, string body);
    }

    public class EmailService : IEmailService
    {
        private readonly IConfiguration _configuration;

        public EmailService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async Task SendEmailAsync(string to, string subject, string body)
        {
            var smtpSettings = _configuration.GetSection("Smtp");
            var smtpClient = new SmtpClient
            {
                Host = smtpSettings["Host"],
                Port = int.Parse(smtpSettings["Port"]),
                EnableSsl = bool.Parse(smtpSettings["EnableSsl"]),
                Credentials = new NetworkCredential(smtpSettings["Username"], smtpSettings["Password"])
            };

            var mailMessage = new MailMessage
            {
                From = new MailAddress(smtpSettings["Username"]),
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            };

            mailMessage.To.Add(to);

            await smtpClient.SendMailAsync(mailMessage);
        }
    }
}

Step 3: Register the Service in Dependency Injection

Add the email service to the dependency injection container in Startup.cs or Program.cs:

builder.Services.AddScoped<IEmailService, EmailService>();

Step 4: Use the Email Service in a Controller or Surface Controller

Use the email service in a controller or Surface Controller to send emails. Here’s an example of a simple contact form Controller:

using Microsoft.AspNetCore.Mvc;
using YourNamespace.Services;

namespace YourNamespace.Controllers
{
    public class ContactController : Umbraco.Cms.Web.Common.Controllers.UmbracoSurfaceController
    {
        private readonly IEmailService _emailService;

        public ContactController(IEmailService emailService)
        {
            _emailService = emailService;
        }

        [HttpPost]
        public async Task<IActionResult> SubmitContactForm(string name, string email, string message)
        {
            try
            {
                var subject = $"New Contact Form Submission from {name}";
                var body = $"<p>Name: {name}</p><p>Email: {email}</p><p>Message: {message}</p>";

                await _emailService.SendEmailAsync("recipient@example.com", subject, body);

                TempData["Success"] = "Your message has been sent successfully!";
                return RedirectToCurrentUmbracoPage();
            }
            catch (Exception ex)
            {
                TempData["Error"] = "There was an error sending your message. Please try again later.";
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
}

start the
conversation
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo
Code Logo