2

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
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Smith
  • 5,765
  • 17
  • 102
  • 161
  • Email doesn't necessarily send instantaneously. It takes time to send and receive data over the Internet. Why do you assume that taking 1 minute to send means that it isn't working properly? – Cody Gray - on strike Mar 12 '11 at 18:35
  • 2
    How many recipients are there? Are you sending files as attachments? Your code seems OK, maybe there is a problem with the mail server. – Jorge Villuendas Zapatero Mar 12 '11 at 18:35
  • @Jorge Villuendas i am looping throug recipients list, but for testing, i just use two recipients. There is no attchment in the test, but i hope to test this too. And am using google smtp server for test – Smith Mar 12 '11 at 18:42
  • @Cody Gray when you send mail through the web, it is almost instant, or t least takes less that 10 seconds to receive it on the receiving end – Smith Mar 12 '11 at 18:44
  • 2
    @Smith it seems that google delays email sended through their smtp server to prevent spam http://stackoverflow.com/questions/2629477/using-gmail-as-smtp-server-in-java-web-app-is-slow/4508888#4508888 – Jorge Villuendas Zapatero Mar 12 '11 at 18:53
  • Now it dosen't even send, it says `the operation have timed out after about 30s of a mminutes – Smith Mar 13 '11 at 12:52

2 Answers2

4

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

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.

kprobst
  • 16,165
  • 5
  • 32
  • 53