mailmessage.send

CookieNZ

Member
Joined
Nov 24, 2005
Messages
17
Programming Experience
Beginner
I am new to programming and I am using a book as a tutorial.

I am trying to send email using a button click, but I get an error message saying that mailmessage is used before it is assigned a variable.

<%@ Import Namespace="System.net.mail" %>
<script runat=server>
Sub button_click(ByVal s As Object, ByVal e As EventArgs)
Dim mailmessage As SmtpClient

mailmessage.Send( _
"paul.cook@mosaic.net.nz", _
"paul_bfc@hotmail.com","Sending Mail!", "Hello")

End Sub
</
script>
<
html>
<
head runat="server">
</
head>
<
body>
<form runat="server">
<asp:Button OnClick="button_click" runat=server />
</
form>
</body>
</
html>
 
The Send method of SmtpClient class is not a shared method, so you have to create an instance of that class and assign it to your mailmessage variable before you can use it. You can declare the variable and assign it a new object of its strong type in one statement like this:
VB.NET:
Dim mailmessage as New SmtpClient
As you see in the documentation, using the default constructor of SmtpClient will initialize it with the applications standard settings for host, credentials and port. Still refering to the documentation these settings can be set/found in the <mailSettings> section.
There are also two other overloads of the SmtpClient class constructor, one will let you specify Smtp server, the other Smtp server and port. Here is one example of that:
VB.NET:
Dim mailmessage as New SmtpClient("smtpserver")
 
Cheers. I understand that I need to specify

Dim mailmessage As New SmtpClient("smtpserver")

but not sure on the syntax of the port etc.....

again, I am very new to all this programming stuff
 
I have found some source code on the web, but I still get an error in IE

Sub button_click(ByVal s As Object, ByVal e As EventArgs)
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("paul_bfc@hotmail.com")
mail.To.Add(
"paul.cook@mosaic.net.nz")
'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."
'send the message
Dim smtp As New SmtpClient("localhost")
smtp.Send(mail)
End Sub 'PlainText
 
Unless you got a Smtp server installed at your local machine, you have to use the one of your Internet Service Provider (ISP).
 
Probably configuration problem with your smtp server then, perhaps authentication issue.
 
Back
Top