title: smtp clients `SendAsync()` method
body: <pre><code> Protected Sub btnLocalSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLocalSubmit.Click
  
              Dim logic = New connections
              logic.emailsToSend(User.Identity.Name, getURL, reportedBy)
              SendAsync()
              Response.Redirect(getRedirectionPath, False)
          Catch ex As Exception
              Response.Write(ex.Message)
          Finally
              _con.Close()
              _con.Dispose()
              _sqlComm.Dispose()
  
          End Try
      End Sub
  
      Sub SendAsync()
  
          Dim _con As New SqlConnection(ConfigurationManager.ConnectionStrings("CitizenJDBConnectionString").ConnectionString)
          Dim _sqlDataAdapter As New SqlDataAdapter("SELECT * FROM EmailSender", _con)
          Dim _table As New System.Data.DataTable
  
          Try
              _con.Open()
              _sqlDataAdapter.Fill(_table)
              _con.Close()
  
              For i As Integer = 0 To _table.Rows.Count - 1
  
  
                  Dim AppPath As String = Request.PhysicalApplicationPath
  
                  Dim sr As New StreamReader(AppPath &amp; "EmailTemplates/NewReport.txt")
                  Dim message As New MailMessage()
  
                  message.IsBodyHtml = True
  
                  message.From = New MailAddress("admin@xxxx.com")
  
                  message.To.Add(New MailAddress(_table.Rows(i).Item(1)))
  
  
                  message.Subject = "New User registration !"
  
                  message.Body = sr.ReadToEnd()
  
                  sr.Close()
  
                  message.Body = message.Body.Replace("&lt;%ReporterName%&gt;", _table.Rows(i).Item(3))
  
                  message.Body = message.Body.Replace("&lt;%ReportURL%&gt;", _table.Rows(i).Item(2))
  
                  Dim client As New SmtpClient()
                  client.Host = "smtp.xxxxx.com"
                  'smtp.gmail.com
                  client.Port = 25
                  client.UseDefaultCredentials = True
                  client.Credentials = New System.Net.NetworkCredential("admin@xxxx.com", "123456")
                  'client.EnableSsl = True
                  Dim userState As Object = message
  
                  'wire up the event for when the Async send is completed
                  AddHandler client.SendCompleted, AddressOf SmtpClient_OnCompleted
  
                  client.SendAsync(message, userState)
  
              Next
  
          Catch ex As Exception
              Response.Write(ex.Message)
          End Try
  
  
      End Sub 'SendAsync
  
      Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
          'Get the Original MailMessage object
          Dim message As MailMessage = CType(e.UserState, MailMessage)
  
          'write out the subject
          Dim subject As String = message.Subject
  
          If e.Cancelled Then
              Console.WriteLine("Send canceled for mail with subject [{0}].", subject)
          End If
          If Not (e.Error Is Nothing) Then
              Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString())
          Else
              Console.WriteLine("Message [{0}] sent.", subject)
          End If
      End Sub 'SmtpClient_OnCompleted
  </code></pre>
  
  <p>I am using the smtp clients <code>SendAsync()</code> function to send out emails asynchronously...but this function does not work ...why?? i do not get any email..when i send it Synchronously...i get the emails, this means my settings are correct...so what is wrong with the <code>SendAsync()</code> method??</p>