Aanchal Singh

Aanchal Singh

  • NA
  • 50
  • 2.5k

Asynchronously sending Emails in Vb.net?

Jun 16 2020 12:59 AM
While sending the email using async getting error.
  1. Public Async Function SendEmail(ByVal EmailToIds As StringByVal Subject As StringByVal Message As StringOptional ByVal EmailCcIds As String = ""Optional ByVal MailAttachment As String = ""Optional ByVal FromEmailType As Integer = 0, Optional EmailBccIds As String = ""As Task  
  2. Dim EmailPwd() As String  
  3. Dim FromEmail, Password As String  
  4. 'Dim objSmtp As SmtpClient  
  5. Dim objMail As MailMessage  
  6. Dim objAttachment As Attachment  
  7. Try  
  8. EmailPwd = GetEmailPassword(FromEmailType).Split("|")  
  9. FromEmail = EmailPwd(0)  
  10. Password = EmailPwd(1)  
  11. objMail = New MailMessage  
  12. objMail.From = New MailAddress(FromEmail)  
  13. objMail.Subject = Subject  
  14. objMail.IsBodyHtml = True  
  15. objMail.Body = Message  
  16. objMail.To.Add(EmailToIds)  
  17.    If Len(EmailCcIds) > 0 Then  
  18.       objMail.CC.Add(EmailCcIds)  
  19.    End If  
  20.    If Len(EmailBccIds) > 0 Then  
  21.       objMail.Bcc.Add(EmailBccIds)  
  22.    End If  
  23.    If MailAttachment <> "" Then  
  24.       objAttachment = New Attachment(MailAttachment)  
  25.       objMail.Attachments.Add(objAttachment)  
  26.    End If  
  27. Using objSmtp = New SmtpClient()  
  28.    objSmtp.Host = ConfigurationSettings.AppSettings("SmtpClient")  
  29.    objSmtp.DeliveryMethod = SmtpDeliveryMethod.Network  
  30.    objSmtp.UseDefaultCredentials = False  
  31.    objSmtp.Credentials = New NetworkCredential(FromEmail, Password)  
  32.    objSmtp.Port = 587  
  33.    objSmtp.EnableSsl = True  
  34.    ServicePointManager.ServerCertificateValidationCallback = Function(s As Object, certificate As X509Certificate, chain As    X509Chain, sslPolicyErrors As SslPolicyErrors) True  
  35.    Await objSmtp.SendMailAsync(objMail)  
  36. End Using  
  37. Catch ex As Exception  
  38.     throw rx  
  39. End Try  
  40. End Function   
Error : Failure Sending Mail.
 
Kindly help for the same.

Answers (2)