system.net.mail太慢了

时间:2011-03-12 18:28:12

标签: .net vb.net email system.net.mail

我正在使用system.net.mail发送电子邮件,如下所示,但它太慢了。它需要大约一分钟发送,我的代码有什么问题。我在backgroundworker_dowork中调用下面的代码。

[edit]:有更快的替代方案,可能是免费或开源代码

' 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

2 个答案:

答案 0 :(得分:4)

将其留给SMTP服务器将电子邮件分发给收件人。

For i = 0 To lstRecipients.Count - 1
    mail.To.Add(lstRecipients(i))
Next
smtp.Send(mail)

如果您不希望收件人看到其他名称,请使用密件抄送属性。

答案 1 :(得分:1)

您可能希望切换到“提取模式”,而邮件客户端则将邮件放在本地IIS SMTP分派位置中。这样你就可以异步发送邮件(sorta),尽管你必须install and configure SMTP组件。

您的代码对我来说似乎不错,滞后必须在您正在使用的中继服务器中。