使用ThreadPool发送电子邮件(效率低)

时间:2012-11-28 12:19:48

标签: .net vb.net email threadpool windows-forms-designer

我想快速发送电子邮件,我使用线程池。但是,我计算发送10封带有线程池的电子邮件的时间和发送10封电子邮件的时间(每个循环中的一个),时间是相同的。我的代码有什么问题?

我的线程池代码:

private sub mymethod()
    addr.Add("first address")
    addr.Add("second address")
    addr.Add("third address")

    addr.Add("tenth address")


    ThreadPool.SetMaxThreads(20, 20)
    ThreadPool.SetMinThreads(15, 15)

    start = Date.Now.TimeOfDay.TotalSeconds
    For Each add As Object In addr

        ThreadPool.QueueUserWorkItem(AddressOf SendMail, add) 

    Next
   End Sub

     Private Sub SendMail(ByVal add As Object)
    If add Is Nothing Then
        Throw New ArgumentException("Empty arg!!")
    End If

    Dim client As SmtpClient = New SmtpClient()
    client.Host = "smtp.gmail.com"
    client.Port = "587"
    client.Credentials = New System.Net.NetworkCredential("address", "password")
    client.EnableSsl = True


    Using Mail As New MailMessage
        Mail.Subject = "test"
        Mail.To.Add(add)
        Mail.From = New MailAddress("address")
        Mail.Body = "body"
        client.ServicePoint.MaxIdleTime = 1 

        Try
            client.Send(Mail)
        Catch ex As SmtpException
        Finally
             count += 1
            SendCompleted() 
        End Try
    End Using

End Sub

 Private Sub SendCompleted()

    If Me.InvokeRequired Then
        Me.Invoke(New Action(AddressOf SendCompleted))
        Return
    End If

    If (count = addr.Count) Then

        end_ = Date.Now.TimeOfDay.TotalSeconds

        label1.Text = (end_ - start)
    End If


End Sub

0 个答案:

没有答案