Implement custom hooks for SmtpServer
masterSmtpServer allows you to extend its behavior by implementing three primary hooks via a ServiceProvider. You must register these implementations in your ServiceProvider before passing it to the SmtpServer constructor.
Supported hooks:
IMessageStore: Handles saving or processing incoming messages.IMailboxFilter: Determines if a sender is allowed or if a message can be delivered to a specific mailbox.IUserAuthenticator: Handles user authentication logic.
Note: For hooks that require factory patterns (like IMailboxFilter or IUserAuthenticator), you should also implement the corresponding factory interface (e.g., IMailboxFilterFactory) to allow the server to create new instances per session.
var options = new SmtpServerOptionsBuilder()
.ServerName("localhost")
.Endpoint(builder =>
builder
.Port(9025, true)
.AllowUnsecureAuthentication(false)
.Certificate(CreateCertificate()))
.Build();
var serviceProvider = new ServiceProvider();
serviceProvider.Add(new SampleMessageStore());
serviceProvider.Add(new SampleMailboxFilter());
serviceProvider.Add(new SampleUserAuthenticator());
var smtpServer = new SmtpServer.SmtpServer(options, serviceProvider);
await smtpServer.StartAsync(CancellationToken.None);