我正在尝试使用现有的System.Net.Mail.SmtpClient创建自定义smtp类,但不知何故它没有获取在类外部设置的属性值。
Public Class CustomClient
Implements IDisposable
Private smtpClient As System.Net.Mail.SmtpClient
Public Property FromAddress As String
Public Property ToAddress As String
Public Property Subject As String
Public Property Body As String
Public Property MtaIP As String
Public Sub New()
Me.smtpClient = New Net.Mail.SmtpClient(MtaIP, 25) 'SmtpClient's Host field is still empty after initializationa
smtpClient.EnableSsl = False
smtpClient.UseDefaultCredentials = True
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
End Sub
Public Sub SendMessage()
Try
Using message As New MailMessage(FromAddress, ToAddress)
message.Subject = Subject
message.Body = "TEST"
message.BodyTransferEncoding = Net.Mime.TransferEncoding.QuotedPrintable
smtpClient.Send(message)
End Using
Catch ex As Exception
Dim errorMsg As String = " SMTPClient encountered the following error..." &
vbNewLine & vbNewLine & "Exception Information:" & vbNewLine & BuildExceptionReport(ex)
CourierShared.Common.sendMessage(errorMsg, MessageTypes.Failure)
End Try
End Sub
End Class
以上是我的代码。我确认MtaIP的值是从外部正确传递的,但不知何故它抛出异常抱怨主机名未指定。当我将属性更改为公共字段而手动分配值时,它可以正常工作。
我遗失了一些有财产用途的东西吗?
编辑:我在CustomClient对象中添加了更具体的send方法,下面是我如何使用该类发送消息
Public Class CustomClientTest
Public Sub Test()
Using client As New CustomClient()
client.FromAddress = inboundSender
client.ToAddress = inboundRecipient
client.Subject = inboundMsgSubject
client.MtaIP = "123.234.34.56" 'I use actual host that works but put this as a reference
client.SendMessage()
End Using
End Sub
End Class
答案 0 :(得分:0)
您的属性没有Get
和/或Set
方法。例如:
Private mFromAddress as string
Public Property FromAddress () As String
Get
FromAddress = mFromAddress
End Get
Set(ByVal value As String)
mFromAddress = value
End Set
End Property
在您的外部代码中,首先实例化该类:
Dim MySMTPClient as SMTPClient
' Now you can set the property
MySMTPClient.FromAddress = "SomeEmailAddress"