system.net.mail is too slow

做~自己de王妃 提交于 2019-12-11 04:08:21

问题


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.



来源:https://stackoverflow.com/questions/5284514/system-net-mail-is-too-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!