【问题标题】:Async Email Class Callback?异步电子邮件类回调?
【发布时间】:2011-12-02 16:07:39
【问题描述】:

我正在尝试创建一个异步发送电子邮件的新类。流程完成后如何更新用户界面?

电子邮件类

Private Shared _mailSent As Boolean = False
Private _mailError As String
Public Property mailError() As String
    Get
        Return _mailError
    End Get
    Set(value As String)
        _mailError = value
    End Set
End Property
Public Property mailSent() As Boolean
    Get
        Return _mailSent
    End Get
    Set(value As Boolean)
        _mailSent = value
    End Set
End Property

Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    Dim token As String = e.UserState.ToString()

    If Not e.Error Is Nothing Then
        mailError = String.Format("{0} - {1}. {2}", token, e.Error.ToString(), e.UserState.ToString())
        mailSent = False
    Else
        mailSent = True
    End If
End Sub
Public Sub SendEmail(ByVal eFrom As String,
                     ByVal eTo As String,
                     ByVal eBody As String,
                     ByVal eBodyIsHtml As Boolean,
                     ByVal eSubject As String,
                     Optional ByVal smtpHost As String = "<ip>",
                     Optional ByVal eReplyTo As String = "",
                     Optional ByVal priority As Integer = 1,
                     Optional ByVal eDisplayName As String = "",
                     Optional ByVal eCC As String = "",
                     Optional ByVal eBCC As String = "")
    Try
        Dim emailFrom As New MailAddress(eFrom, eDisplayName, Encoding.UTF8)
        Dim emailTo As New MailAddress(eTo)
        Dim client As New SmtpClient()

        With client
            .Host = smtpHost
        End With

        Using message As New MailMessage(emailFrom, emailTo)
            With message
                .Subject = eSubject
                .SubjectEncoding = Encoding.UTF8
                .Body = eBody
                .BodyEncoding = Encoding.UTF8
                .IsBodyHtml = eBodyIsHtml

                Select Case priority
                    Case 0
                        .Priority = MailPriority.Low
                    Case 1
                        .Priority = MailPriority.Normal
                    Case 2
                        .Priority = MailPriority.High
                End Select

                If eReplyTo.Length > 0 Then
                    Dim eReplyTos As String() = eReplyTo.Split(New Char() {","c})
                    For i As Integer = 0 To eReplyTos.Length - 1
                        .ReplyToList.Add(eReplyTos(i).ToString)
                    Next
                End If

                If eCC.Length > 0 Then
                    Dim eCCs As String() = eCC.Split(New Char() {","c})
                    For i As Integer = 0 To eCCs.Length - 1
                        .CC.Add(eCCs(i).ToString)
                    Next
                End If

                If eBCC.Length > 0 Then
                    Dim eBCCs As String() = eBCC.Split(New Char() {","c})
                    For i As Integer = 0 To eBCCs.Length - 1
                        .Bcc.Add(eBCCs(i).ToString)
                    Next
                End If
            End With

            AddHandler client.SendCompleted, AddressOf SendCompletedCallback

            Dim userState As String = "Test"
            client.SendAsync(message, userState)
        End Using
    Catch ex As Exception
        mailError = "Execption Error: " & ex.Message
        If Not ex.InnerException Is Nothing Then
            mailError &= "<br /><br />" & ex.InnerException.ToString
        End If
    End Try
End Sub

【问题讨论】:

标签: vb.net email asynchronous callback


【解决方案1】:

更新客户端 UI 的最快方法(因为所有代码都在客户端,而不是在 Web 服务中):

Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
Dim token As String = e.UserState.ToString()

If Not e.Error Is Nothing Then
    //SORRY, IN C# CODES
    label.Text = token + " failed";
Else
    //SORRY IN C# CODES
    label.Text = token + " success";
End If
End Sub

剩下的取决于你,即。 label_text_change 事件处理程序,你想提升messageBox,或者改变颜色,你也可以把label.visible = false,用它来引发/触发一个事件处理程序。

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 1970-01-01
    • 2016-04-24
    • 2011-05-06
    • 2021-08-25
    • 2017-07-13
    • 2011-03-25
    • 2020-04-16
    • 1970-01-01
    相关资源
    最近更新 更多