954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why won't this code work?

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.

meandmymachine

meandmymachine
Newbie Poster
2 posts since Apr 2003
Reputation Points: 10
Solved Threads: 0
 

copy & post the code here.

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 

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.

meandmymachine


ERROR LIST

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()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'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
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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
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.SuspendLayout()
'
'txtBody
'
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 = ""
'
'txtTo
'
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 = ""
'
'Label1
'
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:"
'
'Label2
'
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: "
'
'txtFrom
'
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 = ""
'
'txtServer
'
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 = ""
'
'Label3
'
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:"
'
'Label4
'
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:"
'
'txtSubject
'
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 = ""
'
'btnSend
'
Me.btnSend.Location = New System.Drawing.Point(392, 56)
Me.btnSend.Name = "btnSend"
Me.btnSend.TabIndex = 9
Me.btnSend.Text = "Send"
'
'btnAttach
'
Me.btnAttach.Location = New System.Drawing.Point(392, 88)
Me.btnAttach.Name = "btnAttach"
Me.btnAttach.TabIndex = 10
Me.btnAttach.Text = "Attach"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(16, 120)
Me.Label5.Name = "Label5"
Me.Label5.TabIndex = 11
Me.Label5.Text = "Message:"
'
'Form1
'
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"
Me.ResumeLayout(False)

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
Else
MsgBox("Please enter a valid SMTP server", MsgBoxStyle.Critical, "SMTP Server Error")
Exit Sub
End If

SmtpMail.Send(msg)

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
Debug.WriteLine(OpenFileDialog1.FileName())
msg.Attachments.Add(New MailAttachment(OpenFileDialog1.FileName))
End If

End Sub

End Class

meandmymachine
Newbie Poster
2 posts since Apr 2003
Reputation Points: 10
Solved Threads: 0
 

i know nothing about programming; request was to facilitate others in coming to your assistance.

aeinstein
Team Member - aka kaynine
Team Colleague
645 posts since May 2002
Reputation Points: 186
Solved Threads: 8
 

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 :-)

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

why don't u try and a reference to System.Web assembly (Project->Add Reference)....

srikkanthan
Light Poster
27 posts since May 2004
Reputation Points: 42
Solved Threads: 0
 

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.

malebuterfly
Newbie Poster
1 post since Jun 2004
Reputation Points: 10
Solved Threads: 0
 

What error u getting

lkexpert
Newbie Poster
6 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 
What error u getting



Hey guys.. its simple.. Add the Webrefference System.web n it shoud work.. :cheesy:

arjunsasidharan
Practically a Posting Shark
826 posts since Aug 2006
Reputation Points: 347
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You