I've found some code in a book on Visual Basic.NET that doesn't seem to work properly.(The book is "Learning Visual Basic.NET Through Applications" by Clayton E.Crooks II (Charles River Media) ISBN 1-58450-242-8).

The essential problem is that the code seems to be okay on the CD which came with the book, but when I copy it into Visual Basic.NET Standard Edition, it reports some errors. I haven't reproduced the code here because it's quite long but if anyone can help, I'll gladly send it to them. All contributions gratefully accepted.


13 Years
Discussion Span
Last Post by arjunsasidharan

Hi Kaynine

Thanks for answering my distress call. Below I've listed the errors and below that I've reproduced the code. As I mentioned, it's not actually my code but source published in a book teaching VB.NET, which makes it all the more baffling. Many thanks for your assistance.



  1. "Namespace for 'System.Web.Mail' cannot be found" (line 1).

  2. "Type 'MailMessage()' not defined" (line 5).

  3. "Name 'MailFormat' is not declared" (line 179).

  4. "Name 'MailPriority' is not declared" (line 181).

  5. "Name 'SmtpMail' is not declared" (line 185 & line 191).

  6. "Type 'MailAttachment' is not defined" (line 203).

    Imports System.Web.Mail
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Dim msg As New MailMessage()
    #Region " Windows Form Designer generated code "
        Public Sub New()
            'This call is required by the Windows Form Designer.
            'Add any initialization after the InitializeComponent() call
        End Sub
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents txtTo As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents txtFrom As System.Windows.Forms.TextBox
        Friend WithEvents txtServer As System.Windows.Forms.TextBox
        Friend WithEvents Label3 As System.Windows.Forms.Label
        Friend WithEvents Label4 As System.Windows.Forms.Label
        Friend WithEvents txtSubject As System.Windows.Forms.TextBox
        Friend WithEvents btnSend As System.Windows.Forms.Button
        Friend WithEvents txtBody As System.Windows.Forms.TextBox
        Friend WithEvents btnAttach As System.Windows.Forms.Button
        Friend WithEvents Label5 As System.Windows.Forms.Label
        Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.txtBody = New System.Windows.Forms.TextBox()
            Me.txtTo = New System.Windows.Forms.TextBox()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.Label2 = New System.Windows.Forms.Label()
            Me.txtFrom = New System.Windows.Forms.TextBox()
            Me.txtServer = New System.Windows.Forms.TextBox()
            Me.Label3 = New System.Windows.Forms.Label()
            Me.Label4 = New System.Windows.Forms.Label()
            Me.txtSubject = New System.Windows.Forms.TextBox()
            Me.btnSend = New System.Windows.Forms.Button()
            Me.btnAttach = New System.Windows.Forms.Button()
            Me.Label5 = New System.Windows.Forms.Label()
            Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
            Me.txtBody.Location = New System.Drawing.Point(16, 144)
            Me.txtBody.Multiline = True
            Me.txtBody.Name = "txtBody"
            Me.txtBody.Size = New System.Drawing.Size(456, 184)
            Me.txtBody.TabIndex = 0
            Me.txtBody.Text = ""
            Me.txtTo.Location = New System.Drawing.Point(64, 16)
            Me.txtTo.Name = "txtTo"
            Me.txtTo.Size = New System.Drawing.Size(176, 20)
            Me.txtTo.TabIndex = 1
            Me.txtTo.Text = ""
            Me.Label1.Location = New System.Drawing.Point(24, 16)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(32, 23)
            Me.Label1.TabIndex = 2
            Me.Label1.Text = "To:"
            Me.Label2.Location = New System.Drawing.Point(16, 48)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(40, 23)
            Me.Label2.TabIndex = 3
            Me.Label2.Text = "From: "
            Me.txtFrom.Location = New System.Drawing.Point(64, 48)
            Me.txtFrom.Name = "txtFrom"
            Me.txtFrom.Size = New System.Drawing.Size(176, 20)
            Me.txtFrom.TabIndex = 4
            Me.txtFrom.Text = ""
            Me.txtServer.Location = New System.Drawing.Point(304, 16)
            Me.txtServer.Name = "txtServer"
            Me.txtServer.Size = New System.Drawing.Size(176, 20)
            Me.txtServer.TabIndex = 5
            Me.txtServer.Text = ""
            Me.Label3.Location = New System.Drawing.Point(256, 16)
            Me.Label3.Name = "Label3"
            Me.Label3.Size = New System.Drawing.Size(48, 23)
            Me.Label3.TabIndex = 6
            Me.Label3.Text = "Server:"
            Me.Label4.Location = New System.Drawing.Point(8, 80)
            Me.Label4.Name = "Label4"
            Me.Label4.Size = New System.Drawing.Size(48, 23)
            Me.Label4.TabIndex = 7
            Me.Label4.Text = "Subject:"
            Me.txtSubject.Location = New System.Drawing.Point(64, 80)
            Me.txtSubject.Name = "txtSubject"
            Me.txtSubject.Size = New System.Drawing.Size(176, 20)
            Me.txtSubject.TabIndex = 8
            Me.txtSubject.Text = ""
            Me.btnSend.Location = New System.Drawing.Point(392, 56)
            Me.btnSend.Name = "btnSend"
            Me.btnSend.TabIndex = 9
            Me.btnSend.Text = "Send"
            Me.btnAttach.Location = New System.Drawing.Point(392, 88)
            Me.btnAttach.Name = "btnAttach"
            Me.btnAttach.TabIndex = 10
            Me.btnAttach.Text = "Attach"
            Me.Label5.Location = New System.Drawing.Point(16, 120)
            Me.Label5.Name = "Label5"
            Me.Label5.TabIndex = 11
            Me.Label5.Text = "Message:"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(488, 334)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.btnAttach, Me.btnSend, Me.txtSubject, Me.Label4, Me.Label3, Me.txtServer, Me.txtFrom, Me.Label2, Me.Label1, Me.txtTo, Me.txtBody})
            Me.Name = "Form1"
            Me.Text = "SMTP Mail"
        End Sub
    #End Region
        Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
            With msg
                .From = txtFrom.Text
                .To = txtTo.Text
                .Subject = txtSubject.Text
                .BodyFormat = MailFormat.Text
                .Body = txtBody.Text
                .Priority = MailPriority.Normal
            End With
            If txtServer.Text <> "" Then
                SmtpMail.SmtpServer = txtServer.Text
                MsgBox("Please enter a valid SMTP server", MsgBoxStyle.Critical, "SMTP Server Error")
                Exit Sub
            End If
        End Sub
        Private Sub btnAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttach.Click
            OpenFileDialog1.InitialDirectory = "c:\"
            OpenFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            OpenFileDialog1.FilterIndex = 2
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                msg.Attachments.Add(New MailAttachment(OpenFileDialog1.FileName))
            End If
        End Sub
    End Class

Edited by pyTony: fixed formating


Sorry about the delay, I'm kinda busy. Your doing this in a Windows Forms Application, not a Web Appliction. Make a new Web Application, and it will work :-)


oh.this programming what I learn in the book is coreect.It can reunning.next time I tell you where the wrong in your code.in the way I come from china .can we make friends.my icq is 217824718.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.