如何发送带有vb.net代码的电子邮件?

问题描述 投票:0回答:1

我正在使用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 protected]>")
                    _with1.ReplyToList.Add(New MailAddress("[email protected]"))
                    _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 protected]", emailBody, MailPriority.Normal)
asp.net vb.net smtp smtpclient
1个回答
0
投票

编译器错误消息已清除。变量SmtpClientConnection是一个实例变量(它在声明的任何类实例中作为不同的实体存在),但是您尝试在Shared方法(一个没有类实例的方法)中使用。在这种方法内,您不能使用实例变量,因为您没有一个实例,方法可以从中选择变量并使用它。

解决方案可能是从方法中删除Shared关键字,然后,每当要调用该方法时,都需要创建一个包含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
© www.soinside.com 2019 - 2024. All rights reserved.