无法在vb.net中发送多个邮件

时间:2013-08-14 12:24:35

标签: vb.net

我正在尝试一次向多个人发送邮件。

我的代码是这样的;

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

        omail.From = New MailAddress("jasibs002@gmail.com", "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

执行此操作时,我收到以下错误; An asynchronous call is already in progress. It must be completed or canceled before you can call this method.

我的电子邮件Table有超过10个电子邮件ID。

1 个答案:

答案 0 :(得分:2)

将While循环更改为:

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

    End While
    SmtpServer.SendAsync(omail, Nothing)