簡體   English   中英

如何使用我的 vb.net 代碼發送電子郵件?

[英]How can I send an email with my vb.net code?

我正在使用 asp.net/vb.net。 我想發郵件。 我的代碼沒有按原樣發送電子郵件。 我想知道我在這里做錯了什么。

我創建了一個名為 email.text 的文件,用於保存電子郵件模板。 發送電子郵件的其余代碼如下。 我從我的代碼中刪除了個人信息。

我這樣設置 SMTP 連接:

Private SMTPClientConnection As SmtpClient
Sub New()
    SMTPClientConnection = New SmtpClient
    SMTPClientConnection.Host = "HOSTHERE"
    SMTPClientConnection.Port = PORTHERE
    SMTPClientConnection.DeliveryMethod = SmtpDeliveryMethod.Network
End Sub

然后我創建了一個函數來發送電子郵件:

Private Shared Function SendEmail(ByVal emailUser As String, ByVal bodyMessage As List(Of String), ByVal priority As MailPriority) As Boolean
    Dim functionReturnValue As Boolean = False

    Try

        If Not String.IsNullOrWhiteSpace(emailUser) Then

            If Regex.IsMatch(emailUser, "^([a-zA-Z0-9]+([\.+_-][a-zA-Z0-9]+)*)@(([a-zA-Z0-9]+((\.|[-]{1,2})[a-zA-Z0-9]+)*)\.[a-zA-Z]{2,6})$") Then

                Using SMTPClientConnection
                    Dim smtpMessage As MailMessage = New MailMessage()
                    Dim _with1 = smtpMessage
                    _with1.[To].Add(New MailAddress(emailUser))
                    _with1.From = New MailAddress("Test Email" & " <email@email.com>")
                    _with1.ReplyToList.Add(New MailAddress("email@email.com"))
                    _with1.Subject = "Test Email"
                    _with1.Priority = priority
                    Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(bodyMessage(0), Nothing, "text/html")
                    Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(bodyMessage(1), Nothing, "text/plain")
                    _with1.AlternateViews.Add(plainView)
                    _with1.AlternateViews.Add(htmlView)
                    SMTPClientConnection.Send(smtpMessage)
                    Return True
                End Using
            Else
                Throw New SmtpException("Invalid email.")
            End If
        End If

    Catch ex As Exception
    End Try

    Return functionReturnValue
End Function

我在這里使用我的代碼中的函數:

            Dim plainBody As String = File.ReadAllText(HttpContext.Current.Server.MapPath("email.txt"))
            plainBody = plainBody.Replace("%Name%", emailName)

            Dim emailBody As List(Of String) = New List(Of String)(New String() {plainBody})
            SendEmail("email@email.com", emailBody, MailPriority.Normal)

編譯器錯誤消息很清楚。 變量SmtpClientConnection是一個實例變量(它在任何聲明的類實例中作為不同的實體存在),但您試圖在Shared方法(一種沒有類實例的方法)中使用。 在這種方法中,您不能使用實例變量,因為您沒有一個實例,該方法可以從中選擇變量值並使用它。

解決方案可能是從方法中刪除Shared關鍵字,然后,無論何時要調用該方法,您都需要創建類的實例,其中實例變量SmtpClientConnection已初始化並准備好在對SendMail的以下調用中使用方法。

但是,您仍然可以使用Shared方法,但應該刪除實例變量並在SmtpClient方法中創建它:

Private Shared Function SendEmail(ByVal emailUser As String, ByVal bodyMessage As List(Of String), ByVal priority As MailPriority) As Boolean
    Dim functionReturnValue As Boolean = False

    Try

        If Not String.IsNullOrWhiteSpace(emailUser) Then

            If Regex.IsMatch(emailUser, "^([a-zA-Z0-9]+([\.+_-][a-zA-Z0-9]+)*)@(([a-zA-Z0-9]+((\.|[-]{1,2})[a-zA-Z0-9]+)*)\.[a-zA-Z]{2,6})$") Then


               Dim SMTPClientConnection As SmtpClient = New SmtpClient
               SMTPClientConnection.Host = "HOSTHERE"
               SMTPClientConnection.Port = PORTHERE
               SMTPClientConnection.DeliveryMethod = SmtpDeliveryMethod.Network                
               Using SMTPClientConnection
                    Dim smtpMessage As MailMessage = New MailMessage()
                    ......
                    SMTPClientConnection.Send(smtpMessage)
                    Return True
                End Using
            Else
                Throw New SmtpException("Invalid email.")
            End If
        End If

    Catch ex As Exception
       ' No point in catching an exception and doing nothing here.
       ' You can log the exception somewhere and then throw it again
       LogException(ex)
       Throw
       ' or just remove the try/catch block.
    End Try
    Return functionReturnValue
End Function

通過這種方式,變量僅在需要時創建,並在 using 語句結束時銷毀。 還要注意與 Try/Catch 塊相關的注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM