Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

335 Commits

Repository files navigation

What is SmtpServer?

NuGet

SmtpServer is a lightweight yet powerful SMTP server implementation in C#.
Built entirely in .NET, it leverages the Task Parallel Library (TPL) for maximum performance.

🆕 What's New?

Check the Changelog

⚡ Supported ESMTP Extensions

SmtpServer currently supports the following extensions:

  • STARTTLS
  • SIZE
  • PIPELINING
  • 8BITMIME
  • SMTPUTF8
  • AUTH PLAIN LOGIN

Installation

The package is available on NuGet

PM>install-package SmtpServer

🚀 Getting Started

Starting a basic SMTP server requires only a few lines of code:

varoptions=newSmtpServerOptionsBuilder().ServerName("localhost").Port(25,587).Build();varsmtpServer=newSmtpServer.SmtpServer(options,ServiceProvider.Default);awaitsmtpServer.StartAsync(CancellationToken.None);

What hooks are provided?

There are three hooks that can be implemented; IMessageStore, IMailboxFilter, and IUserAuthenticator.

varoptions=newSmtpServerOptionsBuilder().ServerName("localhost").Endpoint(builder =>builder.Port(9025,true).AllowUnsecureAuthentication(false).Certificate(CreateCertificate())).Build();varserviceProvider=newServiceProvider();serviceProvider.Add(newSampleMessageStore());serviceProvider.Add(newSampleMailboxFilter());serviceProvider.Add(newSampleUserAuthenticator());varsmtpServer=newSmtpServer.SmtpServer(options,serviceProvider);awaitsmtpServer.StartAsync(CancellationToken.None);// to create an X509Certificate for testing you need to run MAKECERT.EXE and then PVK2PFX.EXE// http://www.digitallycreated.net/Blog/38/using-makecert-to-create-certificates-for-developmentstaticX509Certificate2CreateCertificate(){varcertificate=File.ReadAllBytes(@"Certificate.pfx");returnnewX509Certificate2(certificate,"P@ssw0rd");}
publicclassSampleMessageStore:MessageStore{publicoverrideasyncTask<SmtpResponse>SaveAsync(ISessionContextcontext,IMessageTransactiontransaction,ReadOnlySequence<byte>buffer,CancellationTokencancellationToken){awaitusingvarstream=newMemoryStream();varposition=buffer.GetPosition(0);while(buffer.TryGet(refposition,outvarmemory)){awaitstream.WriteAsync(memory,cancellationToken);}stream.Position=0;varmessage=awaitMimeKit.MimeMessage.LoadAsync(stream,cancellationToken);Console.WriteLine(message.TextBody);returnSmtpResponse.Ok;}}
publicclassSampleMailboxFilter:IMailboxFilter,IMailboxFilterFactory{publicTask<MailboxFilterResult>CanAcceptFromAsync(ISessionContextcontext,IMailbox@from,intsize,CancellationTokencancellationToken){if(String.Equals(@from.Host,"test.com")){returnTask.FromResult(MailboxFilterResult.Yes);}returnTask.FromResult(MailboxFilterResult.NoPermanently);}publicTask<MailboxFilterResult>CanDeliverToAsync(ISessionContextcontext,IMailboxto,IMailbox@from,CancellationTokentoken){returnTask.FromResult(MailboxFilterResult.Yes);}publicIMailboxFilterCreateInstance(ISessionContextcontext){returnnewSampleMailboxFilter();}}
publicclassSampleUserAuthenticator:IUserAuthenticator,IUserAuthenticatorFactory{publicTask<bool>AuthenticateAsync(ISessionContextcontext,stringuser,stringpassword,CancellationTokentoken){Console.WriteLine("User={0} Password={1}",user,password);returnTask.FromResult(user.Length>4);}publicIUserAuthenticatorCreateInstance(ISessionContextcontext){returnnewSampleUserAuthenticator();}}

About

A SMTP Server component written in C#

Topics

Resources

Stars

817 stars

Watchers

58 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages