# What is SmtpServer?
[![NuGet](https://img.shields.io/nuget/v/SmtpServer.svg)](https://www.nuget.org/packages/SmtpServer/)
SmtpServer is a simple, but highly functional SMTP server implementation. Written entirely in C# it takes full advantage of the .NET TPL to achieve maximum performance.
SmtpServer is available via [NuGet](https://www.nuget.org/packages/SmtpServer/)
# Whats New?
See [here](https://github.com/cosullivan/SmtpServer/blob/master/Version8.md) for whats new in Version 8.
# What does it support?
SmtpServer currently supports the following ESMTP extensions:
- STARTTLS
- SIZE
- PIPELINING
- 8BITMIME
- AUTH PLAIN LOGIN
# How can it be used?
At its most basic, it only takes a few lines of code for the server to be listening to incoming requests.
```cs
var options = new SmtpServerOptionsBuilder()
.ServerName("localhost")
.Port(25, 587)
.Build();
var smtpServer = new SmtpServer.SmtpServer(options, ServiceProvider.Default);
await smtpServer.StartAsync(CancellationToken.None);
```
# What hooks are provided?
There are three hooks that can be implemented; IMessageStore, IMailboxFilter, and IUserAuthenticator.
```cs
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);
// 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-development
static X509Certificate2 CreateCertificate()
{
var certificate = File.ReadAllBytes(@"Certificate.pfx");
return new X509Certificate2(certificate, "P@ssw0rd");
}
```
```cs
public class SampleMessageStore : MessageStore
{
public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)
{
await using var stream = new MemoryStream();
var position = buffer.GetPosition(0);
while (buffer.TryGet(ref position, out var memory))
{
await stream.WriteAsync(memory, cancellationToken);
}
stream.Position = 0;
var message = await MimeKit.MimeMessage.LoadAsync(stream, cancellationToken);
Console.WriteLine(message.TextBody);
return SmtpResponse.Ok;
}
}
```
```cs
public class SampleMailboxFilter : IMailboxFilter, IMailboxFilterFactory
{
public Task<MailboxFilterResult> CanAcceptFromAsync(ISessionContext context, IMailbox @from, int size, CancellationToken cancellationToken)
{
if (String.Equals(@from.Host, "test.com"))
{
return Task.FromResult(MailboxFilterResult.Yes);
}
return Task.FromResult(MailboxFilterResult.NoPermanently);
}
public Task<MailboxFilterResult> CanDeliverToAsync(ISessionContext context, IMailbox to, IMailbox @from, CancellationToken token)
{
return Task.FromResult(MailboxFilterResult.Yes);
}
public IMailboxFilter CreateInstance(ISessionContext context)
{
return new SampleMailboxFilter();
}
}
```
```cs
public class SampleUserAuthenticator : IUserAuthenticator, IUserAuthenticatorFactory
{
public Task<bool> AuthenticateAsync(ISessionContext context, string user, string password, CancellationToken token)
{
Console.WriteLine("User={0} Password={1}", user, password);
return Task.FromResult(user.Length > 4);
}
public IUserAuthenticator CreateInstance(ISessionContext context)
{
return new SampleUserAuthenticator();
}
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
A SMTP Server component written in C#.zip
共133个文件
cs:113个
eml:6个
csproj:5个
需积分: 5 0 下载量 28 浏览量
2024-01-05
14:15:28
上传
评论
收藏 5.07MB ZIP 举报
温馨提示
A SMTP Server component written in C#
资源推荐
资源详情
资源评论
收起资源包目录
A SMTP Server component written in C#.zip (133个子文件)
SmtpParser.cs 69KB
SmtpParserTests.cs 22KB
SmtpServerTests.cs 19KB
TokenReader.cs 12KB
SmtpSession.cs 8KB
AuthCommand.cs 8KB
EndpointDefinitionBuilder.cs 8KB
SmtpServerOptionsBuilder.cs 7KB
SmtpReplyCode.cs 7KB
PipeReaderExtensions.cs 6KB
SmtpServer.cs 6KB
BuffersExtension.cs 6KB
CustomEndpointListenerExample.cs 5KB
DelegatingMailboxFilter.cs 5KB
ServiceProvider.cs 5KB
Token.cs 5KB
SmtpSessionContext.cs 4KB
SmtpCommandVisitor.cs 4KB
TracingSmtpCommandVisitor.cs 4KB
MailCommand.cs 4KB
SmtpCommandFactory.cs 4KB
SmtpStateTable.cs 4KB
SmtpSessionManager.cs 4KB
EhloCommand.cs 4KB
TokenReaderTests.cs 3KB
SecurableDuplexPipe.cs 3KB
SessionContextExample.cs 3KB
SessionTracingExample.cs 3KB
ISmtpCommandFactory.cs 3KB
DelegatingUserAuthenticator.cs 3KB
SecureServerExample.cs 3KB
CompositeMailboxFilter.cs 3KB
EndpointListener.cs 3KB
MailClient.cs 3KB
PipeWriterExtensions.cs 2KB
PipeReaderTests.cs 2KB
DataCommand.cs 2KB
SampleMailboxFilter.cs 2KB
SmtpResponse.cs 2KB
ServerShutdownExample.cs 2KB
ThroughputBenchmarks.cs 2KB
RcptCommand.cs 2KB
CommonPortsExample.cs 2KB
ServerCancellingExample.cs 2KB
TaskExtensions.cs 2KB
EndpointListenerFactory.cs 2KB
SmtpStateMachine.cs 2KB
SmtpState.cs 2KB
SmtpResponseException.cs 2KB
MessageStore.cs 2KB
QuitCommand.cs 2KB
ProxyCommand.cs 2KB
MailboxFilter.cs 2KB
HeloCommand.cs 2KB
StringUtil.cs 2KB
UserAuthenticator.cs 2KB
ISessionContext.cs 2KB
SampleMailClient.cs 2KB
SampleMessageStore.cs 2KB
TokenKind.cs 2KB
DependencyInjectionExample.cs 2KB
IMailboxFilter.cs 1KB
ConsoleMessageStore.cs 1KB
SimpleServerExample.cs 1KB
StartTlsCommand.cs 1KB
MockMessageStore.cs 1KB
IEndpointDefinition.cs 1KB
Program.cs 1KB
ServiceProviderExtensions.cs 1KB
ISmtpServerOptions.cs 1KB
Program.cs 1KB
Mailbox.cs 1KB
SmtpMessageTransaction.cs 1KB
IMailbox.cs 1KB
RsetCommand.cs 1KB
ByteArraySegmentList.cs 1KB
NoopCommand.cs 1KB
ISecurableDuplexPipe.cs 1KB
SmtpCommand.cs 1KB
AuthenticationContext.cs 960B
SampleUserAuthenticator.cs 946B
CompositeMailboxFilterFactory.cs 939B
SmtpServerDisposable.cs 898B
SimpleExample.cs 896B
IMessageStore.cs 894B
TokenizerBenchmarks.cs 882B
EndPointEventArgs.cs 863B
DisposableContainer.cs 844B
SmtpStateTransition.cs 796B
DelegatingUserAuthenticatorFactory.cs 782B
Program.cs 767B
DelegatingMailboxFilterFactory.cs 751B
DelegatingMessageStoreFactory.cs 745B
IUserAuthenticator.cs 718B
SmtpResponseExceptionEventArgs.cs 657B
IEndpointListener.cs 640B
IMessageTransaction.cs 631B
SmtpCommandEventArgs.cs 608B
SessionFaultedEventArgs.cs 599B
ByteArraySegment.cs 559B
共 133 条
- 1
- 2
资源评论
Lei宝啊
- 粉丝: 2001
- 资源: 1330
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功