Асинхронная отправка писем в VB. net? - PullRequest
0 голосов
/ 16 июня 2020
Public Async Function SendEmail(ByVal EmailToIds As String, ByVal Subject As String, ByVal Message As String, Optional ByVal EmailCcIds As String = "", Optional ByVal MailAttachment As String = "", Optional ByVal FromEmailType As Integer = 0, Optional EmailBccIds As String = "") As Task
    Dim EmailPwd() As String
    Dim FromEmail, Password As String
    'Dim objSmtp As SmtpClient
    Dim objMail As MailMessage
    Dim objAttachment As Attachment
    Try
        EmailPwd = GetEmailPassword(FromEmailType).Split("|")
        FromEmail = EmailPwd(0)
        Password = EmailPwd(1)
        objMail = New MailMessage
        objMail.From = New MailAddress(FromEmail)
        objMail.Subject = Subject
        objMail.IsBodyHtml = True
        objMail.Body = Message
        objMail.To.Add(EmailToIds)
        If Len(EmailCcIds) > 0 Then
            objMail.CC.Add(EmailCcIds)
        End If
        If Len(EmailBccIds) > 0 Then
            objMail.Bcc.Add(EmailBccIds)
        End If
        If MailAttachment <> "" Then
            objAttachment = New Attachment(MailAttachment)
            objMail.Attachments.Add(objAttachment)
        End If

        Using objSmtp = New SmtpClient()
            objSmtp.Host = ConfigurationSettings.AppSettings("SmtpClient")
            objSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
            objSmtp.UseDefaultCredentials = False
            objSmtp.Credentials = New NetworkCredential(FromEmail, Password)
            objSmtp.Port = 587
            objSmtp.EnableSsl = True
            ServicePointManager.ServerCertificateValidationCallback = Function(s As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) True
            Await objSmtp.SendMailAsync(objMail)
        End Using

    Catch ex As Exception
        Throw Ex
    End Try
End Function

Ошибка получения: Ошибка отправки почты.

Пожалуйста, помогите мне сделать это.

Большое спасибо заранее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...