【发布时间】:2011-03-12 18:28:11
【问题描述】:
我正在使用 system.net.mail 发送电子邮件,如下所示,但它太慢了。发送大约需要一分钟,我的代码有什么问题。我在 backgroundworker_dowork 中调用下面的代码。
[编辑]:有没有更快的替代方案,可能是免费或开源代码
' send the email '
Dim smtp As SmtpClient = New SmtpClient()
Dim mail As New MailMessage()
Dim i As Long = 0
' SMTP settings '
With smtp
.Host = Trim$(sSMTP)
.Port = Trim$(iPort)
.UseDefaultCredentials = False
.Credentials = New System.Net.NetworkCredential(sUserID, sPword)
.EnableSsl = bSSL
End With
' create the mail '
With mail
If sAttachment <> vbNullString Then
.Attachments.Add(New Net.Mail.Attachment(sAttachment))
End If
.From = New MailAddress(sFromEmail, sFromName)
.ReplyTo = New MailAddress(sReplyTo)
.Subject = sSubject
.IsBodyHtml = True
.Body = sMessage
End With
For i = 0 To lstRecipients.Count - 1
mail.To.Add(lstRecipients(i))
Debug.Print(lstRecipients(i))
Try
smtp.Send(mail)
lSent += 1
bwrkMain.ReportProgress(CInt(100 * (i + 1) / iTotalRecipients))
SetStatus("Sent:" & lstRecipients(i))
Catch ex As Exception
bwrkMain.ReportProgress(CInt(100 * (i + 1) / iTotalRecipients))
SetStatus("Not Sent:" & lstRecipients(i))
End Try
mail.To.Clear()
Next
【问题讨论】:
-
电子邮件不一定会立即发送。通过 Internet 发送和接收数据需要时间。为什么您认为发送 1 分钟就意味着它无法正常工作?
-
有多少个收件人?您是否将文件作为附件发送?您的代码似乎没问题,可能是邮件服务器有问题。
-
@Jorge Villuendas 我正在遍历收件人列表,但为了测试,我只使用两个收件人。测试中没有附件,但我希望也可以测试一下。我正在使用 google smtp 服务器进行测试
-
@Cody Gray 当你通过网络发送邮件时,它几乎是即时的,或者至少需要不到 10 秒的时间在接收端接收它
-
@Smith 似乎谷歌延迟了通过他们的 smtp 服务器发送的电子邮件以防止垃圾邮件stackoverflow.com/questions/2629477/…
标签: .net vb.net email system.net.mail