'新'中的语法错误

时间:2016-06-19 03:45:12

标签: vb.net

我正在编写vb.net中的电子邮件发件人并且它给出了语法错误。代码如下:

<input ng-model="u.fName" ng-change="change(u)" />

错误在Try Dim message As New MailMessage With { .Subject = Me.SubjectTxt.Text } message.To.Add(Me.ReceiverTxt.Text) message.From = New MailAddress(Me.EmailTxt.Text) message.Body = Me.BodyTxt.Text Dim num2 As Integer = (Me.AttachmentListbox.Items.Count - 1) Dim i As Integer = 0 Do While (i <= num2) Dim item As New Attachment(Conversions.ToString(Me.AttachmentListbox.Items.Item(i))) message.Attachments.Add(item) i += 1 Loop New SmtpClient(Me.ClientBox.Text) With { _ .EnableSsl = True, _ .Credentials = New NetworkCredential(Me.EmailTxt.Text, Me.PasswordTxt.Text), _ .Port = Conversions.ToInteger(Me.PortTxt.Text) _ }.Send(message) Interaction.MsgBox(("Message sent to " & Me.ReceiverTxt.Text), MsgBoxStyle.Information, "SMTP Email Sender") Catch exception1 As Exception ProjectData.SetProjectError(exception1) Dim exception As Exception = exception1 If (Me.ReceiverTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up the Receiver.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.SubjectTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up Subject.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.BodyTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up the Message Body.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.EmailTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up the Email for Account Log In.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.PasswordTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up the Password for Account Log In.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.ClientBox.Text = Nothing) Then Interaction.MsgBox("Please fill up the Server for basis of SMTP Server.", MsgBoxStyle.Exclamation, "You missed something!") ElseIf (Me.PortTxt.Text = Nothing) Then Interaction.MsgBox("Please fill up the Port to successfully send the email.", MsgBoxStyle.Exclamation, "You missed something!") Else Interaction.MsgBox("Sending Failed", DirectCast(Conversions.ToInteger("SMTP Email Sender"), MsgBoxStyle), Nothing) End If ProjectData.ClearProjectError() End Try

2 个答案:

答案 0 :(得分:0)

初始化程序的问题 - 您不知道哪一行是个问题。首先,将代码分成许多行。

Dim smtp As New SmtpClient(Me.ClientBox.Text) With { .EnableSsl = True }
smtp.Credentials = New NetworkCredential(Me.EmailTxt.Text, Me.PasswordTxt.Text)
smtp.Port = Conversions.ToInteger(Me.PortTxt.Text)
smtp.Send(message)

一般来说,你的问题是从新的开始。我刚试过它,它在vb中没有用。这样的事情 - (new class()).DoSomething在c#中起作用。但不是在vb。但话说回来,复杂的初始化器在调试问题上可能是有害的。现在,您将能够确切地导致错误。

此外,您应该在任何地方添加.Trim()

  Me.PortTxt.Text.Trim()

答案 1 :(得分:0)

这应该有效:

With New SmtpClient(Me.ClientBox.Text.Trim) With {
        .EnableSsl = True,
        .Credentials = New NetworkCredential(Me.EmailTxt.Text.Trim, Me.PasswordTxt.Text.Trim),
        .Port = Conversions.ToInteger(Me.PortTxt.Text.Trim) }
    .Send(message)
End With

请注意,.Send与外部With块相对,并且不需要下划线。还添加.Trim作为另一个答案建议。