[英]System.ObjectDisposedException while sending email using smtpclient (fluent-email)
I am getting a System.ObjectDisposedException
when i try to send an e-mail in a .NET core 6.0 project using the fluent-email library:当我尝试使用 fluent-email 库在 .NET core 6.0 项目中发送电子邮件时,我收到了System.ObjectDisposedException
:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Mail.SmtpClient'.
at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
at FluentEmail.Smtp.SendMailEx.SendMailExImplAsync(SmtpClient client, MailMessage message, CancellationToken token)
I have tried to inject the smtpclient
transient , scoped and as a singleton but neither one of the options fixed this issue.我试图注入smtpclient
transient , scoped和作为一个单例,但没有一个选项解决了这个问题。
DI code: DI 代码:
var smtpClient = new SmtpClient(smtpSenderOptions.Host, smtpSenderOptions.Port)
{
EnableSsl = smtpSenderOptions.EnableSsl
};
services.AddSingleton(instance => smtpClient);
Usage (from the fluent-email library ( https://github.com/lukencode/FluentEmail )):用法(来自 fluent-email 库( https://github.com/lukencode/FluentEmail )):
// Taken from https://stackoverflow.com/questions/28333396/smtpclient-sendmailasync-causes-deadlock-when-throwing-a-specific-exception/28445791#28445791
// SmtpClient causes deadlock when throwing exceptions. This fixes that.
public static class SendMailEx
{
public static Task SendMailExAsync(
this SmtpClient @this,
MailMessage message,
CancellationToken token = default(CancellationToken))
{
// use Task.Run to negate SynchronizationContext
return Task.Run(() => SendMailExImplAsync(@this, message, token));
}
private static async Task SendMailExImplAsync(
SmtpClient client,
MailMessage message,
CancellationToken token)
{
token.ThrowIfCancellationRequested();
var tcs = new TaskCompletionSource<bool>();
SendCompletedEventHandler handler = null;
Action unsubscribe = () => client.SendCompleted -= handler;
handler = async (_, e) =>
{
unsubscribe();
// a hack to complete the handler asynchronously
await Task.Yield();
if (e.UserState != tcs)
tcs.TrySetException(new InvalidOperationException("Unexpected UserState"));
else if (e.Cancelled)
tcs.TrySetCanceled();
else if (e.Error != null)
tcs.TrySetException(e.Error);
else
tcs.TrySetResult(true);
};
client.SendCompleted += handler;
try
{
client.SendAsync(message, tcs);
using (token.Register(() =>
{
client.SendAsyncCancel();
}, useSynchronizationContext: false))
{
await tcs.Task;
}
}
finally
{
unsubscribe();
}
}
}
My code calling the library:我的代码调用库:
var response = await _fluentEmail
.Subject(emailContents.Subject)
.To(emailContents.To)
.Attach(attachments)
.UsingTemplate(template, emailContents)
.SendAsync(cancellationToken);
The extensions provided by fluent-email did inject these classes as singleton and I did use another lifetime and therefore this issue occured. fluent-email 提供的扩展确实将这些类作为单例注入,我确实使用了另一个生命周期,因此出现了这个问题。
So I forgot to also override the dependency injection container configuration for all dependencies using the System.ObjectDisposedException
to use singleton, since I was using a custom implementation of FluentEmail.Smtp.SendMailEx.SendMailExImplAsync
.所以我忘记了使用System.ObjectDisposedException
覆盖所有依赖项的依赖注入容器配置以使用单例,因为我使用的是FluentEmail.Smtp.SendMailEx.SendMailExImplAsync
的自定义实现。
After overridding the other dependencies in the DI extension, it worked.在覆盖 DI 扩展中的其他依赖项后,它工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.