Ploxor PB Ploxor PB - 5 months ago 11
Vb.net Question

Syntax erro in New

can not write a lot in English because I'm Brazilian!
But come on, I was doing a sender of emails in vb net and giving Syntax error. Code below, I hope to be helped!

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


The error is in
New SmtpClient
I hope you understand me!

Answer

Problem of initializers - you don't know which line is a problem. First, break your code into many lines.

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)

Now you will be able which line exactly causes the error. And you need to post the error here - exception.Message. Then I'll update the answer

Also, you should add .Trim() everywhere

  Me.PortTxt.Text.Trim()