SendSync如何处理线程池

时间:2019-04-13 03:29:22

标签: vb.net sendmail smtpclient

问题1:

我正在使用SendAsync发送消息

https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendasync?view=netframework-4.7.2

我正在发送大量电子邮件,而消息回调中的延迟太长了: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

1 个答案:

答案 0 :(得分:0)

我认为问题在于SmtpClient已过时:

https://docs.microsoft.com/pt-br/dotnet/api/system.net.mail.smtpclient?view=netframework-4.7.2

“ SmtpClient不支持许多现代协议。它仅兼容。它非常适合从工具发送一封电子邮件,但不能适应该协议的现代要求。”

“使用MailKit或其他库。”