NullReferenceException on SmtpClient.Send with TokenProvider

I’m receiving a nullreference exception:

System.NullReferenceException: Referencia a objeto no establecida como instancia de un objeto.
   at #=zb250QYMCPFuX71Wdi3zOw2HXP4Y1.#=zI6Sdilw=(IAsyncResult #=zqu3CpHA=)
   at #=zb250QYMCPFuX71Wdi3zOw2HXP4Y1.#=ziJegObM=()
   at #=zJ2ASEgD1PFw7CI38cgZaSfyhdu7ZyZMFtENewa4=.#=zy$FAiq0=(#=zP33T804NtGHSJkOnva7ng8jSuRinqjPcVA== #=zGgYws8U=)
   at #=zLoTxgDOe4nZ1O0NwPBMvkO_ByuO$.#=zSN1W8p6rJO4v(Int32 #=zZ8_dxw4=, #=zP33T804NtGHSJkOnva7ng8jSuRinqjPcVA== #=zrM6YvnA=, List`1 #=zsVKDiy8=, Int32 #=zrer2EM8=)
   at #=zLoTxgDOe4nZ1O0NwPBMvkO_ByuO$.#=zSN1W8p6rJO4v(Int32 #=zrer2EM8=, #=zP33T804NtGHSJkOnva7ng8jSuRinqjPcVA== #=zrM6YvnA=, #=zacAZdRbBI2n_WnMwiQ0JPWw= #=zoO9U$v1xZ6DU, List`1 #=zsVKDiy8=)
   at #=zLoTxgDOe4nZ1O0NwPBMvkO_ByuO$.#=zvROYOsukSu5F(Int32 #=zrer2EM8=, #=zP33T804NtGHSJkOnva7ng8jSuRinqjPcVA== #=zrM6YvnA=)
   at #=zb250QYMCPFuX71Wdi3zOw2HXP4Y1.#=zCKovfytCK2z5()
   at #=zb250QYMCPFuX71Wdi3zOw2HXP4Y1.#=z_Kqst7s=(AsyncCallback #=zNho5yt8=, Object #=zhbhiHAM=)
   at Aspose.Email.Clients.Smtp.SmtpClient.#=zsr4fTl8=(IConnection #=ztuHMKMw=, IEnumerable`1 #=zBEflwmE=, EventHandler`1 #=zwZ_uldqlCDW5, AsyncCallback #=zNho5yt8=, Object #=zhbhiHAM=)
   at Aspose.Email.Clients.Smtp.SmtpClient.#=zsr4fTl8=(IConnection #=ztuHMKMw=, IEnumerable`1 #=zBEflwmE=, EventHandler`1 #=zwZ_uldqlCDW5, AsyncCallback #=zNho5yt8=)
   at Aspose.Email.Clients.Smtp.SmtpClient.Send(IConnection connection, IEnumerable`1 messages)
   at Aspose.Email.Clients.Smtp.SmtpClient.Send(MailMessage message)

when I try to send an email with a SmtpClient with a custom TokenProvider. I’ve found that the problem is in CurrentConnection property of SmtpClient, when I execute the code in an ASPNETMVC app, but when I execute the same code in a WCF Soap appplication it works fine.

Debugging both smtpclients in console, both are equals except on the CurrentConnectionProperty:

image.png (34.5 KB)

I’m working with Aspose.Email 22.12 but I’ve tried 25.8 and the same behaviour is reproduced.

Also debugging, when the connection is null, my tokenprovider class is never executed.

Hello @mtomas,

Thank you for the detailed description.
To investigate this properly, could you please share a minimal reproducible ASP.NET MVC project (or a simplified sample) that demonstrates the issue? That would help us test the exact scenario on our side and confirm whether it’s related to environment specifics, connection initialization, or something else.

Hi!

I’ve created a demo ASPNETMVC web app to reproduce it but it’s working there. So I’ve investigated more and I’ve found that in the past it worked fine, but in some upgrade of our software it fails. I can’t find any changes related to the send mail flow, and the most remarkable thing that we changed in that version was an upgrade of some third-party libraries trough .net:

  • Antlr3.Runtime
  • Common.Logging
  • Iesi.Collectoins
  • log4net
  • Microsoft.Bcl.HashCode
  • Microsoft.Win32.Registry
  • Newtonsoft.Json
  • NHibernate
  • NVelocity
  • Spring.*
  • System.Security.*
  • System.ServiceProcess
  • System.Transactions

Do you know if could be any problem related to that libraries?

Hello @mtomas,

The updated third-party libraries could indirectly influence this behavior.
We can only suggest what might have an effect, in order to narrow down the root cause:

System.Security.*, changes here may impact TLS/SSL negotiation or token authentication processes.

Microsoft.Win32.Registry, if your SMTP configuration relies on registry lookups, an update could affect this.

Spring / NHibernate / Common.Logging / log4net, these libs could affect threading, synchronization contexts, or error handling in ASP.NET MVC, which in turn may prevent the connection from being properly initialized.

Ok, I’ve just confirmed that the problem was in the moment we upgrade all those dll’s. Now I must to check them to detect which is/are the problematic.

But meanwhile, is there any way to trace your dll in order to get more info about the error?

By other way, other thing I’ve copied all third dll’s on the aspnetmvc demo app and it’s keep on working there with those dlls.

If the same set of upgraded DLLs works in your demo MVC project, then it’s possible the issue is related not to the libs themselves but to how they interact with your full application’s configuration (e.g., threading model, security policies, global exception handling).

What we can suggest:

  • You can enable SMTP activity logging as described here: Enable Activity Logging. However, since the connection doesn’t occur at all, this is unlikely to produce useful information in this case.

  • Run your app with “Break on all CLR exceptions” enabled. This may reveal an inner exception before the NullReferenceException that could indicate why connection is not being created.

  • Optionally, you can use Wireshark or a similar tool to capture network traffic.

I’ve upgraded the following nuget packages to the latest version and it finally works for me:

  • Microsoft.Identity.Client: 4.71.1.0 → 4.77.0.0
  • Microsoft.IdentityModel.Abstractions: 7.2.0.0 → 8.14.0.0

(In the aspnetmvc demo app, the old libraries were working, but it’s clear that something in my app was problematic with that packages)

Thanks,

Marc.

@mtomas,

Thanks for sharing the details. It could be helpful for others facing a similar situation.