I am trying to send email from VB.Net using code posted below in VB 2008 Express, but get one error "Syntax error" and it points to Imports System.Net.Mail. I also would like to include required by server authentication as well as to make confirmation message box disapear itself after two seconds. Please help.:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Imports System.Net.Mail
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
  Net.NetworkCredential("username@mysite.com", "password")
            SmtpServer.Port = 2525
            SmtpServer.Host = "smtp.somesite.com"
            mail = New MailMessage()
            mail.From = New MailAddress("username@mysite.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from me"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Thank you.

Leo

Unfortunately you should put "Imports System.net.mail" at the very top even before "Public Class Form1" So your code should go like this:

Imports System.Net.Mail
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
  Net.NetworkCredential("username@mysite.com", "password")
            SmtpServer.Port = 2525
            SmtpServer.Host = "smtp.somesite.com"
            mail = New MailMessage()
            mail.From = New MailAddress("username@mysite.com")
            mail.To.Add("TOADDRESS")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from me"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
Imports System.Net.Mail

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       .....
       End Sub
End Class

Mentioned error is gone. Thank you!

The error is gone! Thank you! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.