[英]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.