system.net.mail is too slow

2019-07-20 16:40发布

问题:

am using system.net.mail to send email as shown below, but its too slow. it takes about a minute to send, whats wrong with my code. Am calling the code below in backgroundworker_dowork.

[edit]: is there a faster alternative, maybe free or open source code

' 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

回答1:

Leave it up to the SMTP server to distribute the email to the recipients.

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

Use the Bcc property if you don't want the recipient to see the other names.



回答2:

You might want to switch to "pickup mode" where the mail client drops the message(s) in the local IIS SMTP dispatch location instead. That way you're sending mail asynchronously (sorta), although you'll have to install and configure the SMTP component.

Your code seems fine to me, the lag must be in the relay server you're using.