我有这段代码:
For i = 0 To DataGridView1.RowCount - 2
If send_email(body, DataGridView1.Rows(i).Cells(8).Value.ToString) Then
countSentMail = countSentMail + 1
System.Threading.Thread.Sleep(2 * 1000)
ProgressBar1.PerformStep()
End If
Next i
Private Function send_email(ByVal body As String, ByVal emailAddress As String) As Boolean
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Port = [port]
SmtpServer.Host = [ip]
mail = New MailMessage()
mail.From = New MailAddress([email_from])
mail.To.Add([email_to])
mail.Bcc.Add([email_bbc])
mail.Subject = [subject]
mail.Body = body
Mail.IsBodyHtml = True
SmtpServer.Send(mail)
send_email = True
Catch ex As Exception
send_email = False
End Try
End Function
当代码运行表单/应用程序获得“无响应”直到发送出所有电子邮件并且进度条变得无用。如果我评论发送功能的内容 - 进度条按我的预期工作。有什么建议吗?
答案 0 :(得分:1)
抱歉延迟回复。
是。在增加send_email
ProgressBar1
send_email
使用同步的SmtpClient.Send()
并等待服务器响应。
您可以改为使用SmtpClient.SendAsync()
,这将继续执行并在后台等待服务器响应。