Exception when sending multiple mails using dependency injection
See original GitHub issueI’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:
- Created 2 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, that makes sense. As my SMTP configuration is a bit more complex the Func overload would be more appropriate:
I’ll give it a try and see what happens.
Hello ,i have the same bug.so does this commit [https://github.com/lukencode/FluentEmail/pull/188/commits/567a7b6d48c3c0ebead0a6635c6b36da50ba34c9] resolve this problem?