Modifiche in Startup.cs



In Initialization/Startup.cs nella funzione ConfigureServices aggiungere (o modificare se già esistente):

 

services.AddSingleton<IEmailSender, TUOPROGETTO.Web.Modules.Common.EmailSender>();

 




Aggiunta classe EmailSender.cs



In Modules/Common aggiungere un nuovo file chiamato EmailSender.cs e scrivere nel file la seguente classe:

 

using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using MimeKit;
using Serenity.Extensions;
using System;
using System.Globalization;
using System.IO;

namespace TUOPROGETTO.Web.Modules.Common
{
    public class EmailSender : IEmailSender
    {
        private readonly IWebHostEnvironment host;
        private readonly IEmailQueue emailQueue;
        private readonly SmtpSettings settings;

        public EmailSender(IWebHostEnvironment host, IOptions<SmtpSettings> settings,
            IEmailQueue emailQueue = null)
        {
            this.host = host ?? throw new ArgumentNullException(nameof(host));
            this.settings = new SmtpSettings();
            this.settings.Host = "nomehost"; //esempio: smtps.aruba.it
            this.settings.Port = numPorta; //esempio: 465 (porta utilizzata da aruba smtps)
            this.settings.SecureSocket = SecureSocketOptions.SslOnConnect;
            this.settings.Username = "tuausername@tuodominio";
            this.settings.Password = "tuapassword";
            this.settings.From = "tuaemail@tuodominio";
            this.settings.PickupPath = "pathInCuiSalvareMailsComeFile"; //In genere è AppData/Mail
            this.settings.AutoUseQueue = true;
            this.emailQueue = emailQueue;
        }

        public void Send(MimeMessage message, bool skipQueue = true)
        {
            if (message == null)
                throw new ArgumentNullException(nameof(message));

            if (message.From.Count == 0 && !string.IsNullOrEmpty(settings.From))
                message.From.Add(MailboxAddress.Parse(settings.From));

            if (!skipQueue && settings.AutoUseQueue && emailQueue != null)
            {
                emailQueue.Enqueue(message);
            }
            else if (!string.IsNullOrEmpty(settings.Host))
            {
                using var client = new SmtpClient();
                client.Connect(settings.Host, settings.Port, settings.SecureSocket);
                if (!string.IsNullOrEmpty(settings.Username))
                    client.Authenticate(settings.Username, settings.Password);

                client.Send(message);
                client.Disconnect(true);
            }
            else
            {
                var pickupPath = string.IsNullOrEmpty(settings.PickupPath) ?
                    Path.Combine(host.ContentRootPath, "App_Data", "Mail") :
                    Path.Combine(host.ContentRootPath, settings.PickupPath);
                if (!Directory.Exists(pickupPath))
                    Directory.CreateDirectory(pickupPath);
                message.WriteTo(Path.Combine(pickupPath, DateTime.Now.ToString("yyyyMMdd_HHmmss_fff",
                    CultureInfo.InvariantCulture) + ".eml"));
            }
        }

    }
}

 

N.B.: se la username è quella principale del server smtp, il from può essere qualsiasi indirizzo dello stesso dominio. Se invece è la username di una delle caselle, il from deve essere uguale alla username.




Modifiche in appsettings.json



In appsettings.json, modificare la sezione "SmtpSettings" con i propri dati come di seguito

 

"SmtpSettings": {
    "Host": "nomehost", //esempio: smtps.aruba.it
    "Port": numPorta, //esempio: 465 (porta utilizzata da aruba smtps)
    "SecureSocket": true,
    "Username": "tuausername@tuodominio",
    "Password": "tuapassword",
    "From": "tuaemail@tuodominio",
    "AutoUseQueue": true,
    "PickupPath": "pathInCuiSalvareMailsComeFile"; //In genere è AppData/Mail
  }

 

N.B.: se la username è quella principale del server smtp, il from può essere qualsiasi indirizzo dello stesso dominio. Se invece è la username di una delle caselle, il from deve essere uguale alla username.