cosullivan / SmtpServer

A SMTP Server component written in C#
MIT License
690 stars 163 forks source link

Adding a custom EndpointListener via Dependency Injection #195

Closed idblew closed 11 months ago

idblew commented 1 year ago

Related to #140

How do you add a custom EndpointListener when using Dependency Injection?

Also I see that IEndpointListener now requires GetPipeAsync rather than GetStreamAsync, is there a simple example of filtering on IP address using the new interface?

cosullivan commented 11 months ago

Hi,

There are two ways you can do this now.

Firstly, you can listen for the SessionCreated event on the SmtpServer instance and check the IP address from there. The default EndpointListener will add the IP address to the session context (check out the SessionTracingExample). If you get an IP address that you want to block, you can simply throw an exception here and the client will be disconnected and closed.

The second option is similar to before whereby you can create a custom endpoint listener. From within the custom endpoint listener, get the underlying Pipe and you can dispose of that and return null.

Thanks, Cain.