任何方式来压缩这个ASP.net System.Net.Mail代码

时间:2014-04-22 14:19:02

标签: asp.net vb.net

我使用以下代码在用户登录失败时发送电子邮件提醒。

是否有更简单的方法来编写这个,以便电子邮件例程不必像这样写出两次,对于存在的用户和不存在的用户?基本上我正在寻找一种更有效的写作方式。

感谢您的帮助。

Protected Sub LoginUser_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoginError

    LoginUser.FailureText = "Invalid Username or Password - Please Try Again"

    Dim UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox)

    Dim CurrentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)

    If (CurrentUser IsNot Nothing) Then

        If (CurrentUser.IsLockedOut = True) Then
            LoginUser.FailureText = "Your account has been locked - Contact the system administrator"
        ElseIf (CurrentUser.IsApproved = False) Then
            LoginUser.FailureText = "Your account is disabled - Contact the system administrator"
        End If

        Dim Email_Error As New Thread(
            Sub()
                Dim mailObject As New System.Net.Mail.MailMessage()
                Dim emailCredentials As New System.Net.NetworkCredential("email@domain.com", "password")

                mailObject.To.Add("my@email.com")

                If (CurrentUser.IsLockedOut = True) Then
                    mailObject.Subject = "User " & CurrentUser.ToString() & " is locked out"
                ElseIf (CurrentUser.IsApproved = False) Then
                    mailObject.Subject = "User " & CurrentUser.ToString() & " is disabled"
                Else
                    mailObject.Subject = "User " & CurrentUser.ToString() & " login failed"
                End If

                mailObject.From = New System.Net.Mail.MailAddress("info@domain.com")
                mailObject.IsBodyHtml = True

                If (CurrentUser.IsLockedOut = True) Then
                    mailObject.Body = "User is locked out"
                ElseIf (CurrentUser.IsApproved = False) Then
                    mailObject.Body = "User is disabled"
                Else
                    mailObject.Body = "User login failed"
                End If

                Dim SmtpMail As New System.Net.Mail.SmtpClient("smtp.email.com")
                SmtpMail.UseDefaultCredentials = False
                SmtpMail.EnableSsl = False
                SmtpMail.Credentials = emailCredentials
                SmtpMail.Port = 587
                SmtpMail.Send(mailObject)
            End Sub
        )
        Email_Error.Start()

    Else
        Dim Email_NoUser_Error As New Thread(
            Sub()
                Dim mailObject As New System.Net.Mail.MailMessage()
                Dim emailCredentials As New System.Net.NetworkCredential("email@domain.com", "password")

                mailObject.To.Add("my@email.com")

                mailObject.Subject = "Unknown User " & UserName.Text() & " login failed"

                mailObject.From = New System.Net.Mail.MailAddress("info@domain.com")
                mailObject.IsBodyHtml = True

                mailObject.Body = "Unknown User login failed"

                Dim SmtpMail As New System.Net.Mail.SmtpClient("smtp.email.com")
                SmtpMail.UseDefaultCredentials = False
                SmtpMail.EnableSsl = False
                SmtpMail.Credentials = emailCredentials
                SmtpMail.Port = 587
                SmtpMail.Send(mailObject)
            End Sub
        )
        Email_NoUser_Error.Start()
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

我只创建一次mailObject并设置所有保持不变的字段,然后调用函数Email_ErrorEmail_NoUser_Error只设置那些变化的字段,然后返回第一个函数发送到那里。