question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception when sending multiple mails using dependency injection

See original GitHub issue

I’m using Hangfire hosted in an ASP.NET core site as a background job processor. The email service gets a IFluentEmail instance from the DI container. When multiple email jobs are being processed at the same time I get an exception with the following stack trace:

 ---> System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.InvalidOperationException: An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
   at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
   at FluentEmail.Smtp.SendMailEx.SendMailExImplAsync(SmtpClient client, MailMessage message, CancellationToken token)
   at FluentEmail.Smtp.SmtpSender.SendAsync(IFluentEmail email, Nullable`1 token)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at FluentEmail.Smtp.SmtpSender.Send(IFluentEmail email, Nullable`1 token)
   at FluentEmail.Core.Email.Send(Nullable`1 token)

The problem is that the AddFluentEmail method seems to be registering the service with a scope other than Transient. The result is that the same service instance is being returned each time with the same underlying SmtpClient. The fix would be to change the registration to Transient so that a new instance is created for each request with a separate SmtpClient.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
IanLedzioncommented, Jul 27, 2021

Thanks, that makes sense. As my SMTP configuration is a bit more complex the Func overload would be more appropriate:

services.AddFluentEmail("noreply@example.local")
    .AddSmtpSender(() => new SmtpClient{/* Options */});

I’ll give it a try and see what happens.

0reactions
mohamedbenlassouedcommented, Nov 17, 2022

Hello ,i have the same bug.so does this commit [https://github.com/lukencode/FluentEmail/pull/188/commits/567a7b6d48c3c0ebead0a6635c6b36da50ba34c9] resolve this problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Throwing Exception while sending Email to multiple ...
I am trying to send an email to multiple recipients. I have two recipients in my to list e.g "aman@gmail.com,abc@xyz.com". and I am...
Read more >
Send Emails Using ASP.NET Core [With Code Examples]
Learn to send emails with ASP.NET Core using the built-in SMTP library, Mailkit, & an email API (plain text, HTML, with attachments &...
Read more >
Send Emails with ASP.NET Core in 5 EASY Steps - Guide
Go to MailController and add an Action Method. Do not forget to to inject the mailService object to the Constructor.
Read more >
Services and dependency injection in Drupal 8+
In Drupal 8 speak, a service is any object managed by the services container.
Read more >
Contexts and Dependency Injection
There is no standard concurrency control mechanism for CDI beans. Nevertheless, a bean instance can be shared and accessed concurrently from multiple threads....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found