SendSync 如何处理线程池
How SendSync deals with thread pool
问题一:
我正在使用 SendAsync 发送消息:
我正在发送大量电子邮件,但消息回拨延迟太长:client.SendCompleted
电子邮件是异步发送的,但回调似乎是串行的,似乎只有一个线程发送和接收电子邮件。
例如如果消息回调延迟 1 秒从邮件服务器返回,而我发送了 20 封电子邮件,它将延迟大约 20 秒来传递和完成所有消息。
看来只有一个线程在顺序队列中发送消息...
如果 SendAsync 在内部处理线程池,是否有办法更改此池中的线程数、最大线程数以及与线程池相关的其他内容?
问题2:
关于 SendMailAsync 的另一个相关问题:
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendmailasync?view=netframework-4.7.2
在内部,SendMailAsync 与 SendAsync 做同样的事情,尽管代码设计略有不同?
SendAsync 与 SendMailAsync 之间的真正区别是什么;它应该解决我在问题 1 中提到的问题吗?
代码
Dim counter = 0
Public Async Function SendEmail(email As String, bodystuff As String) As Task
Dim smtp As New SmtpClient() '"email-smtp.us-west-3.amazonaws.com")
Dim hostloginpass = "email-smtp.us-west-3.amazonaws.com, AKDKJDAKJ2SKJMSDKJ2,AK2KD298;kdj2kjdkjaiuwkmp2KKK2098K2la97ksjNW,Test_Site"
smtp.Host = cla_util.sGetToken(hostloginpass, 1, ",")
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential(getTokenFromArr(hostloginpass, 2, ","), getTokenFromArr(hostloginpass, 3, ","))
smtp.EnableSsl = true
Dim from As New MailAddress("contact@myserver.com", "Info", System.Text.Encoding.UTF8)
Dim [to] As New MailAddress(email)
Dim message As New MailMessage(from, [to])
message.Body = String.Format("The message I want to send is to this <b>contact: {0}{1}</b>", vbCrLf, bodystuff)
message.IsBodyHtml = True
message.BodyEncoding = System.Text.Encoding.UTF8
message.Subject = "Test email subject"
message.SubjectEncoding = System.Text.Encoding.UTF8
'commented -> Await smtp.SendMailAsync(message)
smtp.SendMailAsync(message)
counter = counter + 1
System.Console.WriteLine("Counter -> " & counter)
End Function
Dim _isCompleted
Dim _timing
Dim Tasks As New ArrayList
Public Async Sub StartEmailRun()
Try
Dim sWatch As New Stopwatch()
sWatch.Start()
For i = 0 To 50
Tasks.Add(SendEmail("a_test_email@gmail.com", "email test"))
Next
Console.WriteLine("SETP OUT OF THE SENDING")
_isCompleted = True
sWatch.Stop()
_timing = sWatch.ElapsedMilliseconds
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Async Sub StartEmailRun2()
Try
Dim sWatch As New Stopwatch()
sWatch.Start()
For i = 0 To 50
Dim thread as New Thread(
Sub()
SendEmail("a_test_email@gmail.com", "email test")
End Sub
)
thread.Start()
Next
Console.WriteLine("SETP OUT OF THE SENDING")
_isCompleted = True
sWatch.Stop()
_timing = sWatch.ElapsedMilliseconds
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
我认为的问题是 SmtpClient 已过时:
https://docs.microsoft.com/pt-br/dotnet/api/system.net.mail.smtpclient?view=netframework-4.7.2
"SmtpClient doesn't support many modern protocols. It is compat-only. It's great for one off emails from tools, but doesn't scale to modern requirements of the protocol."
"Use MailKit or other libraries."
问题一:
我正在使用 SendAsync 发送消息:
我正在发送大量电子邮件,但消息回拨延迟太长:client.SendCompleted
电子邮件是异步发送的,但回调似乎是串行的,似乎只有一个线程发送和接收电子邮件。
例如如果消息回调延迟 1 秒从邮件服务器返回,而我发送了 20 封电子邮件,它将延迟大约 20 秒来传递和完成所有消息。
看来只有一个线程在顺序队列中发送消息...
如果 SendAsync 在内部处理线程池,是否有办法更改此池中的线程数、最大线程数以及与线程池相关的其他内容?
问题2:
关于 SendMailAsync 的另一个相关问题: https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendmailasync?view=netframework-4.7.2
在内部,SendMailAsync 与 SendAsync 做同样的事情,尽管代码设计略有不同?
SendAsync 与 SendMailAsync 之间的真正区别是什么;它应该解决我在问题 1 中提到的问题吗?
代码
Dim counter = 0
Public Async Function SendEmail(email As String, bodystuff As String) As Task
Dim smtp As New SmtpClient() '"email-smtp.us-west-3.amazonaws.com")
Dim hostloginpass = "email-smtp.us-west-3.amazonaws.com, AKDKJDAKJ2SKJMSDKJ2,AK2KD298;kdj2kjdkjaiuwkmp2KKK2098K2la97ksjNW,Test_Site"
smtp.Host = cla_util.sGetToken(hostloginpass, 1, ",")
smtp.Port = 587
smtp.Credentials = New System.Net.NetworkCredential(getTokenFromArr(hostloginpass, 2, ","), getTokenFromArr(hostloginpass, 3, ","))
smtp.EnableSsl = true
Dim from As New MailAddress("contact@myserver.com", "Info", System.Text.Encoding.UTF8)
Dim [to] As New MailAddress(email)
Dim message As New MailMessage(from, [to])
message.Body = String.Format("The message I want to send is to this <b>contact: {0}{1}</b>", vbCrLf, bodystuff)
message.IsBodyHtml = True
message.BodyEncoding = System.Text.Encoding.UTF8
message.Subject = "Test email subject"
message.SubjectEncoding = System.Text.Encoding.UTF8
'commented -> Await smtp.SendMailAsync(message)
smtp.SendMailAsync(message)
counter = counter + 1
System.Console.WriteLine("Counter -> " & counter)
End Function
Dim _isCompleted
Dim _timing
Dim Tasks As New ArrayList
Public Async Sub StartEmailRun()
Try
Dim sWatch As New Stopwatch()
sWatch.Start()
For i = 0 To 50
Tasks.Add(SendEmail("a_test_email@gmail.com", "email test"))
Next
Console.WriteLine("SETP OUT OF THE SENDING")
_isCompleted = True
sWatch.Stop()
_timing = sWatch.ElapsedMilliseconds
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Async Sub StartEmailRun2()
Try
Dim sWatch As New Stopwatch()
sWatch.Start()
For i = 0 To 50
Dim thread as New Thread(
Sub()
SendEmail("a_test_email@gmail.com", "email test")
End Sub
)
thread.Start()
Next
Console.WriteLine("SETP OUT OF THE SENDING")
_isCompleted = True
sWatch.Stop()
_timing = sWatch.ElapsedMilliseconds
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
我认为的问题是 SmtpClient 已过时:
https://docs.microsoft.com/pt-br/dotnet/api/system.net.mail.smtpclient?view=netframework-4.7.2
"SmtpClient doesn't support many modern protocols. It is compat-only. It's great for one off emails from tools, but doesn't scale to modern requirements of the protocol."
"Use MailKit or other libraries."