CSharp-SMTP-Server 1.0.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharp-SMTP-Server --version 1.0.2.1                
NuGet\Install-Package CSharp-SMTP-Server -Version 1.0.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSharp-SMTP-Server" Version="1.0.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CSharp-SMTP-Server --version 1.0.2.1                
#r "nuget: CSharp-SMTP-Server, 1.0.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CSharp-SMTP-Server as a Cake Addin
#addin nuget:?package=CSharp-SMTP-Server&version=1.0.2.1

// Install CSharp-SMTP-Server as a Cake Tool
#tool nuget:?package=CSharp-SMTP-Server&version=1.0.2.1                

CSharp-SMTP-Server

Simple (receive only) SMTP server library for C#, written in .NET Standard.

This server is only returning all received emails to interface provided by the software running this library.

Supported features

  • TLS and STARTTLS
  • AUTH LOGIN and AUTH PLAIN

Compatible with

  • RFC 1869 (SMTP Service Extensions)
  • RFC 2254 (SMTP Service Extension for Authentication)
  • RFC 3463 (Enhanced Mail System Status Codes)
  • RFC 5321 (SMTP Protocol)

Basic usage

var server = new SMTPServer(new[]
{
	new ListeningParameters()
	{
		IpAddress = IPAddress.Any,
		RegularPorts = new ushort[] {25, 587},
		TlsPorts = new ushort[] {465}
	},
	new ListeningParameters()
	{
		IpAddress = IPAddress.IPv6Any,
		RegularPorts = new ushort[] {25, 587},
		TlsPorts = new ushort[] {465}
	}
}, new ServerOptions(){ServerName = "Test SMTP Server", RequireEncryptionForAuth = false}, new DeliveryInterface(), new LoggerInterface());
//with TLS:
//}, new ServerOptions() { ServerName = "Test SMTP Server", RequireEncryptionForAuth = true}, new DeliveryInterface(), new LoggerInterface(), new X509Certificate2("PathToCertWithKey.pfx"));
		
server.SetAuthLogin(new AuthenticationInterface());
server.SetFilter(new FilterInterface());
server.Start();
class LoggerInterface : ILogger
{
	public void LogError(string text) => Console.WriteLine("[LOG] " + text);
}
class DeliveryInterface : IMailDelivery
{
	//Let's just print all emails
	public void EmailReceived(MailTransaction transaction) => Console.WriteLine(
		$"\n\n--- EMAIL TRANSACTION ---\nSource IP: {transaction.RemoteEndPoint}\nAuthenticated: {transaction.AuthenticatedUser ?? "(not authenticated)"}\nFrom: {transaction.From}\nTo: {transaction.To.Aggregate((current, item) => current + ", " + item)}\nBody: {transaction.Body}\n--- END OF TRANSACTION ---\n\n");

	//We only own "@smtp.demo" and we don't want any emails to other domains
	public UserExistsCodes DoesUserExist(string emailAddress) => emailAddress.EndsWith("@smtp.demo")
		? UserExistsCodes.DestinationAddressValid
		: UserExistsCodes.BadDestinationSystemAddress;
}
class AuthenticationInterface : IAuthLogin
{
	//123 is password for all users (NOT SECURE, ONLY FOR DEMO PURPOSES!)

	public bool AuthPlain(string authorizationIdentity, string authenticationIdentity, string password,
		EndPoint remoteEndPoint,
		bool secureConnection) => password == "123";

	public bool AuthLogin(string login, string password, EndPoint remoteEndPoint, bool secureConnection) =>
		password == "123";
}
class FilterInterface : IMailFilter
{
	//Allow all connections
	public SmtpResult IsConnectionAllowed(EndPoint ep) => new SmtpResult(SmtpResultType.Success);

	//Let's block .invalid TLD. You can do here eg. SPF validation
	public SmtpResult IsAllowedSender(string source, EndPoint ep) => source.TrimEnd().EndsWith(".invalid")
		? new SmtpResult(SmtpResultType.PermanentFail)
		: new SmtpResult(SmtpResultType.Success);

	//Let's block all emails to root at any domain
	public SmtpResult CanDeliver(string source, string destination, bool authenticated, string username,
		EndPoint ep) => destination.TrimStart().StartsWith("root@")
		? new SmtpResult(SmtpResultType.PermanentFail)
		: new SmtpResult(SmtpResultType.Success);

	//Let's blacklist word "spam"
	public SmtpResult CanProcessTransaction(MailTransaction transaction) => transaction.Body.ToLower().Contains("spam")
		? new SmtpResult(SmtpResultType.PermanentFail)
		: new SmtpResult(SmtpResultType.Success);
}

Generating PFX from PEM keys

You can generate PFX from PEM certificate and PEM private key using openssl:

openssl pkcs12 -export -in public.pem -inkey private.pem -out CertWithKey.pfx
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.6 2,480 12/23/2023
1.1.5 228 10/12/2023
1.1.4 198 6/9/2023
1.1.3 145 6/9/2023
1.1.2 142 6/8/2023
1.1.1 136 6/8/2023
1.1.0 452 9/16/2022
1.0.2.1 851 5/1/2019