ASP.NET Core 5.0 - New EmailSettings

This article will demonstrate the implementation of the EmailSettings class and adding the TOpions of EmailSettings to the ServiceCollection with the Configuration. GetSection("EmailSettings"). I will assume you have downloaded the ASP.NET Core 5.0 - SMTP Settings Tester Project. Registered users can download the source code for free at Manage > Assets.

SMTP Settings Tester Project and Article Series

This project implements Bootstrap v5, Bootstrap Native, and the KenHaggerty. Com. SingleUser NuGet package which provides log in and log out pages for a single user to access the Admin pages. jQuery has been removed. I created a topic, ASP.NET Core 5.0 - SMTP Settings Tester Project for discussions. More details and screenshots at ASP.NET Core 5.0 - SMTP Settings Tester Project. The details page includes the version change log.

The ASP.NET Core 2.2 - SMTP EmailSender Implementation article, published over 2 1/2 years ago, introduced the EmailSettings class. The ASP.NET Core 3.1 - SMTP EmailSender article added properties to EmailSettings. The EmailSettings properties have grown with the implementation a customizable html banner. KenHaggerty.Com and projects which implement AesCrypto added a PasswordEncrypted property. Developing and testing the SMTP Settings Tester has combined and refined the EmailSettings. I have updated KenHaggerty.Com with these new EmailSettings.

Models > EmailSettings.cs
public class EmailSettings
    public bool Configured { get; set; }
    public int Timeout { get; set; }
    public string MailServer { get; set; }
    public int MailPort { get; set; }
    public string SenderName { get; set; }
    public string SenderEmail { get; set; }
    public string Password { get; set; }
    public bool PasswordEncrypted { get; set; }
    public string BannerBackcolor { get; set; }
    public string BannerColor { get; set; }
    public string BannerText { get; set; }
    public string EmailSignature { get; set; }
    public string SupportName { get; set; }
    public string SupportEmail { get; set; }
    public string AdminEmail { get; set; }

The new Configured property throws an InvalidOperationException("The EmailSettings' Configured property is not true.") to fail quickly on demo settings. The new Timeout property overrides the MailKit. IMailService Timeout which defaults to 120000 or 2 minutes, too long for testing. The MailKit. Security. SecureSocketOptions. Auto option which allows the MailKit. IMailService decide which SSL or TLS options to use (default), obsoleted the UseSsl property. If the server does not support SSL or TLS, then the connection will continue without any encryption. The new PasswordEncrypted property and AesCrypto supports password encryption. Edit appsettings. json, add an EmailSettings section with your email banner and server settings.

Production Example
"EmailSettings": {
  "Configured": false,
  "Timeout": 30000, // default 120000 = 2 minutes
  "MailServer": "",
  "MailPort": 8889,
  "SenderName": "Your Domain Support",
  "SenderEmail": "",
  "Password": "EncryptedPassword",
  "PasswordEncrypted": true,
  "BannerBackcolor": "#FFFF00",
  "BannerColor": "#FFA500",
  "BannerText": "Your Domain",
  "EmailSignature": "Support",
  "SupportName": "Support",
  "SupportEmail": "",
  "AdminEmail": ""
Gmail Example
"EmailSettings": {
  "Configured": false,
  "Timeout": 30000, // default = 120000 = 2 minutes
  "MailServer": "",
  "MailPort": 465,
  "SenderName": "Sender Name",
  "SenderEmail": "",
  "Password": "GmailPassword",
  "PasswordEncrypted": false,
  "BannerBackcolor": "#00C89F",
  "BannerColor": "#285F8C",
  "BannerText": "SMTP Settings Tester",
  "EmailSignature": "Email Signature",
  "SupportName": "Support Name",
  "SupportEmail": "",
  "AdminEmail": ""

If you want to use Gmail for SMTP, you must allow less secure app access to the Google account.

Google Account Security.
Allow Less Secure Apps.

Add the TOpions of EmailSettings to the ServiceCollection with the Configuration. GetSection("EmailSettings").

Startup.cs > ConfigureServices

To access the configured EmailSettings, inject the IOptions<EmailSettings> to the PageModel or Service.

Ken Haggerty
Created 08/02/21
Updated 08/07/21 01:18 GMT

Log In or Reset Quota to read more.

Article Tags:

Email Json Model
Successfully completed. Thank you for contributing.
Contribute to enjoy content without advertisments.
Something went wrong. Please try again.



Not accepting new comments.

Submit your comment. Comments are moderated.

User Image.
DisplayedName - Member Since ?