Skip to content

Commit 1fcbd67

Browse files
author
cosullivan
committed
added version8 doco
1 parent 7dfdce9 commit 1fcbd67

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

Examples/WorkerService/ConsoleMessageStore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public sealed class ConsoleMessageStore : MessageStore
2222
public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)
2323
{
2424
await using var stream = new MemoryStream();
25-
25+
2626
var position = buffer.GetPosition(0);
2727
while (buffer.TryGet(ref position, out var memory))
2828
{

Examples/WorkerService/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2525
{
2626
var options = new SmtpServerOptionsBuilder()
2727
.ServerName("SMTP Server")
28-
.Port(9025)
28+
.Port(25)
2929
.Build();
3030

3131
return new SmtpServer.SmtpServer(options, provider.GetRequiredService<IServiceProvider>());

README.md

+27-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ SmtpServer is a simple, but highly functional SMTP server implementation. Writte
66

77
SmtpServer is available via [NuGet](https://www.nuget.org/packages/SmtpServer/)
88

9+
# Whats New?
10+
See [here](https://github.com/cosullivan/SmtpServer/blob/master/Version8.md) for whats new in Version 8.
11+
912
# What does it support?
1013
SmtpServer currently supports the following ESMTP extensions:
1114
* STARTTLS
@@ -24,7 +27,7 @@ var options = new SmtpServerOptionsBuilder()
2427
.Port(25, 587)
2528
.Build();
2629

27-
var smtpServer = new SmtpServer.SmtpServer(options);
30+
var smtpServer = new SmtpServer.SmtpServer(options, ServiceProvider.Default);
2831
await smtpServer.StartAsync(CancellationToken.None);
2932
```
3033

@@ -36,27 +39,37 @@ var options = new SmtpServerOptionsBuilder()
3639
.Port(25, 587)
3740
.Port(465, isSecure: true)
3841
.Certificate(CreateX509Certificate2())
39-
.MessageStore(new SampleMessageStore())
40-
.MailboxFilter(new SampleMailboxFilter())
41-
.UserAuthenticator(new SampleUserAuthenticator())
4242
.Build();
4343

44-
var smtpServer = new SmtpServer.SmtpServer(options);
44+
var serviceProvider = new ServiceProvider();
45+
serviceProvider.Add(new SampleMessageStore());
46+
serviceProvider.Add(new SampleMailboxFilter());
47+
serviceProvider.Add(new SampleUserAuthenticator());
48+
49+
var smtpServer = new SmtpServer.SmtpServer(options, serviceProvider);
4550
await smtpServer.StartAsync(CancellationToken.None);
4651
```
4752

4853
```cs
4954
public class SampleMessageStore : MessageStore
5055
{
51-
public override Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, CancellationToken cancellationToken)
52-
{
53-
var textMessage = (ITextMessage)transaction.Message;
54-
55-
var message = MimeKit.MimeMessage.Load(textMessage.Content);
56-
Console.WriteLine(message.TextBody);
57-
58-
return Task.FromResult(SmtpResponse.Ok);
59-
}
56+
public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)
57+
{
58+
await using var stream = new MemoryStream();
59+
60+
var position = buffer.GetPosition(0);
61+
while (buffer.TryGet(ref position, out var memory))
62+
{
63+
await stream.WriteAsync(memory, cancellationToken);
64+
}
65+
66+
stream.Position = 0;
67+
68+
var message = await MimeKit.MimeMessage.LoadAsync(stream, cancellationToken);
69+
Console.WriteLine(message.TextBody);
70+
71+
return SmtpResponse.Ok;
72+
}
6073
}
6174
```
6275

Src/SmtpServer.sln

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.28803.352
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{A965DCEA-280B-447A-B5C5-4089C444C2B7}"
7-
EndProject
86
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmtpServer", "SmtpServer\SmtpServer.csproj", "{0A7CFC3D-305C-4018-9052-3A7A8B5DD104}"
97
EndProject
108
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmtpServer.Benchmarks", "SmtpServer.Benchmarks\SmtpServer.Benchmarks.csproj", "{7AE1F3B4-2C00-4BAA-A13C-5EBD43EDE81A}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmtpServer.Tests", "SmtpServer.Tests\SmtpServer.Tests.csproj", "{4957B054-F07E-402D-A3EC-7EBA0B3018B7}"
1311
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "..\Examples\WorkerService\WorkerService.csproj", "{EE0C474F-8404-4FB6-865F-A034B5DB77FE}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkerService", "..\Examples\WorkerService\WorkerService.csproj", "{EE0C474F-8404-4FB6-865F-A034B5DB77FE}"
1513
EndProject
1614
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{6BAD2430-FA6B-4929-8BD7-66663CA02207}"
1715
EndProject

Version8.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# What's new in Version 8
2-
* Big refactor to Pipeline
3-
* Utilising Span and Memory to dramatically reduce the allocations
2+
Version 8 contains substantial refactoring to take advantage of [System.IO.Pipelines](https://www.nuget.org/packages/System.IO.Pipelines/) in an effort to improve throughput performance and reduce memory allocations.
3+
4+
In addition to this there are also changes to make service resolution easier via Dependency Injection through utilizing the [IServiceProvider](https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider) interface.
45

56
## Performance
6-
The *Send* test is the resul of having a basic SmtpServer configuration and sending a 143kb email that was take from the Enron Corpus.
7+
The throuhput performance and memory allocation of version 7.2 and version 8 were benchmarked using [BenchmarkDotNet](https://www.nuget.org/packages/BenchmarkDotNet/). The benchmark consisted of a single *Send* test which is the result of having a basic SmtpServer configuration and sending a 143kb email that was take from the Enron Corpus dataset.
78

89
### Version 7.2
910

@@ -17,7 +18,29 @@ The *Send* test is the resul of having a basic SmtpServer configuration and send
1718
|------- |---------:|----------:|----------:|--------:|------:|------:|----------:|
1819
| Send | 2.204 ms | 0.0307 ms | 0.0287 ms | 11.7188 | - | - | 41.91 KB |
1920

20-
## Extensibility
21-
ISmtpCommandFactory
22-
- add Sample for overriding Greeting
21+
## Breaking Changes
22+
There are a number of breaking changes from v7.
23+
24+
### SmtpServerOptionsBuilder
25+
The SmtpServerOptionsBuilder is now used to configure the SMTP Server options only and the extensibility services such as IMessageStore, IMailboxFilter, IUserAuthenticator, and IEndpointListener have now been removed from the SmtpServerOptionsBuilder.
26+
27+
### Extensibility Services
28+
29+
#### Service Configuration
30+
The extensibility services such as IMessageStore, IMailboxFilter, IUserAuthenticator, and IEndpointListener are now resolved internally via an instance of [IServiceProvider](https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider). This makes it easier to support Dependency Injection, however, there is a default implementation of the [IServiceProvider](https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider) included that will just resolve the specific instances required for the SmtpServer. The [IServiceProvider](https://docs.microsoft.com/en-us/dotnet/api/system.iserviceprovider) instance is a constructor parameter on the SmtpServer instance.
31+
32+
#### Service Resolution
33+
The IMessageStore, IMailboxFilter, and IUserAuthenticator instances were previously resolved by calling the appropriate CreateInstance method on the instance factory, for example, IMessageStoreFactory, IMailboxFilterFactory, and IUserAuthenticatorFactory.
34+
35+
#### IMessageStore
36+
The IMessageStore interface has a breaking change to the SaveAsync method. The message contents is now included as a [ReadOnlySequence<byte>](https://docs.microsoft.com/en-us/dotnet/api/system.buffers.readonlysequence-1) buffer parameter on the method.
37+
38+
``` csharp
39+
Task<SmtpResponse> SaveAsync(
40+
ISessionContext context,
41+
IMessageTransaction transaction,
42+
ReadOnlySequence<byte> buffer,
43+
CancellationToken cancellationToken);
44+
```
2345

46+
This is the exact buffer that is read from the input stream so care must be taken when using it. It must not be referenced and used outside of the scope of this method.

0 commit comments

Comments
 (0)