MailKit 连接异步。当另一个读取操作挂起时,无法调用 ReadAsync 方法
MailKit ConnectAsync. The ReadAsync method cannot be called when another read operation is pending
我使用 MailKit 按计时器发送电子邮件。
我将 smtpClient 保留在 ConcurrentDictionary 中。
有时,尝试连接异步时出现错误:当另一个读取操作挂起时,无法调用 ReadAsync 方法。
public async Task SendMessageAsync(string consumerKey, SmtpServer server, EmailMessage message)
{
SmtpClient client = new SmtpClient();
if (!_clients.TryAdd(consumerKey, client))
{
client = _clients[consumerKey];
}
if (!client.IsConnected) await ConnectSmtpClient(server, client);
...
}
private async Task ConnectSmtpClient(SmtpServer server, SmtpClient client)
{
try
{
await client.ConnectAsync(server.Address, server.Port, server.UseSSL);
await client.AuthenticateAsync(new SaslMechanismLogin(server.Credentials));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
at System.Net.Security.SslStream.[ReadAsyncInternal]d__214`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MailKit.Net.Smtp.SmtpStream.[ReadAheadAsync]d__39.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at MailKit.Net.Smtp.SmtpStream.[ReadResponseAsync]d__46.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MailKit.Net.Smtp.SmtpClient.[SendCommandAsync]d__63.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MailKit.Net.Smtp.SmtpClient.[SendEhloAsync]d__65.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MailKit.Net.Smtp.SmtpClient.[EhloAsync]d__66.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MailKit.Net.Smtp.SmtpClient.[ConnectAsync]d__74.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
您可能遇到了多个线程访问同一个 SmtpClient 对象的情况。
由于 MailKit 的 SmtpClient 不是线程安全的,如果您尝试从多个线程发送邮件(或 connect/authenticate),事情将会中断。
我使用 MailKit 按计时器发送电子邮件。 我将 smtpClient 保留在 ConcurrentDictionary 中。 有时,尝试连接异步时出现错误:当另一个读取操作挂起时,无法调用 ReadAsync 方法。
public async Task SendMessageAsync(string consumerKey, SmtpServer server, EmailMessage message)
{
SmtpClient client = new SmtpClient();
if (!_clients.TryAdd(consumerKey, client))
{
client = _clients[consumerKey];
}
if (!client.IsConnected) await ConnectSmtpClient(server, client);
...
}
private async Task ConnectSmtpClient(SmtpServer server, SmtpClient client)
{
try
{
await client.ConnectAsync(server.Address, server.Port, server.UseSSL);
await client.AuthenticateAsync(new SaslMechanismLogin(server.Credentials));
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
at System.Net.Security.SslStream.[ReadAsyncInternal]d__214`1.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MailKit.Net.Smtp.SmtpStream.[ReadAheadAsync]d__39.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at MailKit.Net.Smtp.SmtpStream.[ReadResponseAsync]d__46.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MailKit.Net.Smtp.SmtpClient.[SendCommandAsync]d__63.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MailKit.Net.Smtp.SmtpClient.[SendEhloAsync]d__65.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MailKit.Net.Smtp.SmtpClient.[EhloAsync]d__66.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MailKit.Net.Smtp.SmtpClient.[ConnectAsync]d__74.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
您可能遇到了多个线程访问同一个 SmtpClient 对象的情况。
由于 MailKit 的 SmtpClient 不是线程安全的,如果您尝试从多个线程发送邮件(或 connect/authenticate),事情将会中断。