cosullivan / SmtpServer

A SMTP Server component written in C#
MIT License
675 stars 159 forks source link

Certain errors that bubble up from MailerMessageStore.SaveAsync are swallowed #218

Open lachlann562 opened 2 months ago

lachlann562 commented 2 months ago

While implementing MailerMessageStore.SaveAsync, When i deployed the service to a server. All emails were failing. Eventually to diagnose the issue I cloned and built the project locally. The DataCommand.ExecuteAsync can swallow an exception if the ReadDotBlockAsync delegate returns an exception. In my case, due to an assembly issue it was raising System.MissingMethodException: Method not found. This is despite the problem command being wrapped in a try/catch in my code.

the current SmtpServer code does:

catch (Exception)
{
    await context.Pipe.Output.WriteReplyAsync(new SmtpResponse(SmtpReplyCode.TransactionFailed), cancellationToken).ConfigureAwait(false);
}

It should capture the exception and either log it somewhere (e.g. eventlog) or call an event or some other method so the caller can identify this exception occurred and decide how to handle it.

Example of my implementation:

public override async Task<SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, ReadOnlySequence<byte> buffer, CancellationToken cancellationToken)
{
   try {
      // call  class.method in problem assembly
   } catch (Exception ex) {
     // do something with exception
     // this is NOT triggered, it is caught in the DataCommand.ExecuteAsync
   }
}