2016-06-19 15 views
0

vb.netで電子メールの送信者をコーディングしていて、構文エラーが発生しています。以下のコード:'New'の構文エラー

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 

エラーが初期化子のNew SmtpClient

答えて

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ではない。しかし、やはり、複雑なイニシャライザはデバッグの問題に悪影響を与える可能性があります。これで、正確にエラーが発生する行が表示されます。

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がブロックして外に反している、とアンダースコアが必要とされていないこと:

また、あなたはどこにでも

Me.PortTxt.Text.Trim() 
+0

@PloxorPBよろしくお願いいたします。一般に、あなたの問題は 'new'で始まる行にありました。私はちょうどそれをテストした、それはVBで動作しません。このような何か - ( 'new class())。DoSomething'はC#で動作します。しかし、VBではない。しかし、やはり、複雑なイニシャライザはデバッグの問題に悪影響を与える可能性があります。 –

0

これが働いているはずです.Trim()を追加する必要があります。もう一つの答えとして、.Trimも追加されました。

+0

これは、 'With'を使ってinitオブジェクトを作ることができます。しかし、複雑なイニシャライザの問題が残っています - どのラインが問題か分かりません。もしあなたが '{class =} '、' .b = 10、.c =" string1 "}'で 'new Class()しかし、プロパティが複雑なコードに割り当てられると、これはコードの維持とデバッグに悪い**です –