user2674855
user2674855

Reputation: 225

not able to send more than one mail in vb.net

I am trying to send a mail to more than one person at a time.

My code is like this;

 Dim SmtpServer As New SmtpClient()
            SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "someMadeUpPassword")
            SmtpServer.Port = 25
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
            Dim omail As New MailMessage()

        omail.From = New MailAddress("[email protected]", "JaseemBinBacker", System.Text.Encoding.UTF8)

        omail.Subject = "Test Mail"
        Dim str As String
        str = "Hai How Are You I am Sendig This Mail for Testing"
        str = str + vbNewLine & "Checking"
        str = str + vbNewLine & "Sucess"
        omail.Body = str
        Dim email As String
        Dim cmdemail As New SqlCommand("SELECT Emailid FROM  dbo.Email_tbl", con.connect)
        dr = cmdemail.ExecuteReader
        While dr.Read
            email = dr("Emailid")
            omail.To.Add(email)

        End While
        dr.Close()
        con.disconnect()
     SmtpServer.SendAsync(omail, Nothing)

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

While executing this, I am getting the following error; An asynchronous call is already in progress. It must be completed or canceled before you can call this method.

My Email Table has more than 10 email ids.

Upvotes: 0

Views: 281

Answers (1)

Heslacher
Heslacher

Reputation: 2167

Change your While loop to:

    While dr.Read
        email = dr("Emailid")
        omail.To.Add(email)

    End While
    SmtpServer.SendAsync(omail, Nothing)

Upvotes: 2

Related Questions