SmtpServer icon indicating copy to clipboard operation
SmtpServer copied to clipboard

Certain errors that bubble up from MailerMessageStore.SaveAsync are swallowed

Open lachlann562 opened this issue 10 months ago • 0 comments

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
   }
}

lachlann562 avatar Apr 26 '24 15:04 lachlann562