Weird Issues with SMTPClient

D

David Thole

Hey all,

I'm still very new at all this, but am going through the ASP.net 2.0
unleashed book, first chapter and trying to program my own little form
emailer for fun. I tried following the code for their version in the
book to the best of my abiltiies, and came across issues, mostly that a
type was expected when trying to compile in MS Visual Studio 2002.
When my stuff wasn't working at all, I decided to try just using his
code almost exactly. I copied it from the CD to my folder, and edited
just the e-mail addresses to my very own so that it wasn't going
somewhere unexpected. The system it's on is Windows .net Server 2003
Standard. The error I got, when running the page was:

---BEGIN PASTE---

Compilation Error

Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.



Compiler Error Message: BC30182: Type expected.



Source Error:







Line 5:

Line 6: Sub Page_Load()

Line 7: Dim client As New SmtpClient()

Line 8: client.Host = "localhost"

Line 9: client.Port = 25





Source File: c:\inetpub\wwwroot\smtpclient\SendMail.aspx Line: 7







Show Detailed Compiler Output:





C:\WINDOWS\system32>
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe" /t:library
/utf8output
/R:"C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll"
/R:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\assembly\dl3\4de41090\1037aa00_40b8c601\smtpclient.DLL"
/R:"C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll"
/R:"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll"
/R:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_global.asax.5w9amsgm.dll"
/out:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.dll" /D:DEBUG=1
/debug+ /define:_MYTYPE=\"Web\"
/imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Specialized,System.Configuration,System.Text,System.Text.RegularExpressions,System.Web,System.Web.Caching,System.Web.SessionState,System.Web.Security,System.Web.Profile,System.Web.UI,System.Web.UI.WebControls,System.Web.UI.WebControls.WebParts,System.Web.UI.HtmlControls
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.0.vb"
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.1.vb"





Microsoft (R) Visual Basic Compiler version 8.0.50727.42

for Microsoft (R) .NET Framework version 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.



c:\inetpub\wwwroot\smtpclient\SendMail.aspx(7) : error BC30182: Type expected.



Dim client As New SmtpClient()





\0----End Paste----

\0
I don't have the smtp mail setup on that server, but when trying my
other form I changed the host and ports accordlingly where it should
have used the mail server. I even had tcpdump running on the port
(running all this through parallels) and nothing was sent. For some
reason "SmtpClient" just doesn't work correctly. Is this because I
have an older version of Visual Studio (2002)? Should I consider
upgrading to 2005? I have a windows mobile 2005 PDA, which is the main
reason why I would want to get .net 2005 -- are there any other
advantages?

Thanks for your help on this error, my start with .net is a little
rough so far, but I'm hoping to get better as I progress through this
book. I apologize ahead of time if this question is too "newbie like".

Thanks,

David
 
K

Ken Cox [Microsoft MVP]

Hi David,

For starters, VS 2002 wouldn't be able to deal with the SmtpClient class
because it doesn't exist in that version of the .NET framework.

However, are you sure the problem isn't in the web.config for that chapter?
I just checked and needed to comment out this part of the web.config to get
the page to run:

<pages pageBaseType="" />

You might want to see if there are any corrections in the author's support
site.

Ken
Microsoft MVP [ASP.NET]
 
D

David Thole

Thanks for the help, I checked my configuration file and didn't find
anything odd about it really. My configuration file right now is:

---BEGIN SNIP---
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<!-- DYNAMIC DEBUG COMPILATION

Set compilation debug="true" to insert debugging symbols
(.pdb information)

into the compiled page. Because this creates a larger file
that executes

more slowly, you should set this value to true only when
debugging and to

false at all other times. For more information, refer to the
documentation about

debugging ASP.NET files.

-->

<compilation defaultLanguage="vb" debug="true" />

<!-- CUSTOM ERROR MESSAGES

Set customErrors mode="On" or "RemoteOnly" to enable custom
error messages, "Off" to disable.

Add <error> tags for each of the errors you want to handle.

-->

<customErrors mode="RemoteOnly" />

<!-- AUTHENTICATION

This section sets the authentication policies of the
application. Possible modes are "Windows",

"Forms", "Passport" and "None"

-->

<authentication mode="Windows" />

<!-- AUTHORIZATION

This section sets the authorization policies of the
application. You can allow or deny access

to application resources by user or role. Wildcards: "*" mean
everyone, "?" means anonymous

(unauthenticated) users.

-->

<authorization>

<allow users="*" /> <!-- Allow all users -->

<!-- <allow users="[comma separated list of users]"

roles="[comma separated list of roles]"/>

<deny users="[comma separated list of users]"

roles="[comma separated list of roles]"/>

-->

</authorization>

<!-- APPLICATION-LEVEL TRACE LOGGING

Application-level tracing enables trace log output for every
page within an application.

Set trace enabled="true" to enable application trace logging.
If pageOutput="true", the

trace information will be displayed at the bottom of each
page. Otherwise, you can view the

application trace log by browsing the "trace.axd" page from
your web application

root.

-->

<trace enabled="false" requestLimit="10" pageOutput="false"
traceMode="SortByTime" localOnly="true" />

<!-- SESSION STATE SETTINGS

By default ASP.NET uses cookies to identify which requests
belong to a particular session.

If cookies are not available, a session can be tracked by
adding a session identifier to the URL.

To disable cookies, set sessionState cookieless="true".

-->

<sessionState mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data
source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20"
/>

<!-- GLOBALIZATION

This section sets the globalization settings of the application.

-->

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

</system.web>

</configuration>

\0
---END SNIP---

I'm using his code kinda for the chapter, it is:

---BEGIN SNIP---

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net.Mail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">



Sub Page_Load()

Dim client As New SmtpClient()

client.Host = "localhost"

client.Port = 25

client.Send("(e-mail address removed)", "(e-mail address removed)", _

"Let's eat lunch!", "Lunch at the Steak House?")

End Sub



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Send Mail</title>

</head>

<body>

<form id="form1" runat="server">

<div>



Email sent!



</div>

</form>

</body>

</html>\0

--- END SNIP---

Basically his code is the same as in the book, the XXX, YYY, AAA, BBB
just substituted for my e-mail addresses.

Was there something I needed to do to web.config to make this work? I
went into iis, and went to the site, and it says that the ASP.net
version I'm using is 2.0.50727

Thanks for the help again on this. I think there are more reasons for
me to upgrade to VS 2005 (or is 2006 coming out soon that I should
wait, and maybe get the express edition just for learning?)

-David
 
D

David Thole

Thanks for the link on that, I'll give some of their examples a try to
see if I can somehow figure out why this isn't working.

-David
 
D

David Thole

Just to add as well...

I just tried the C# code, from the CD, and it worked fine....

Dunno what the deal is.

-David

Thanks for the help, I checked my configuration file and didn't find
anything odd about it really. My configuration file right now is:

---BEGIN SNIP---
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.web>

<!-- DYNAMIC DEBUG COMPILATION

Set compilation debug="true" to insert debugging symbols
(.pdb information)

into the compiled page. Because this creates a larger file
that executes

more slowly, you should set this value to true only when
debugging and to

false at all other times. For more information, refer to the
documentation about

debugging ASP.NET files.

-->

<compilation defaultLanguage="vb" debug="true" />

<!-- CUSTOM ERROR MESSAGES

Set customErrors mode="On" or "RemoteOnly" to enable custom
error messages, "Off" to disable.
Add <error> tags for each of the errors you want to handle.

-->

<customErrors mode="RemoteOnly" />

<!-- AUTHENTICATION
This section sets the authentication policies of the
application. Possible modes are "Windows",
"Forms", "Passport" and "None"

-->

<authentication mode="Windows" />

<!-- AUTHORIZATION
This section sets the authorization policies of the
application. You can allow or deny access

to application resources by user or role. Wildcards: "*" mean
everyone, "?" means anonymous
(unauthenticated) users.

-->

<authorization>

<allow users="*" /> <!-- Allow all users -->

<!-- <allow users="[comma separated list of users]"

roles="[comma separated list of roles]"/>

<deny users="[comma separated list of users]"

roles="[comma separated list of roles]"/>

-->

</authorization>

<!-- APPLICATION-LEVEL TRACE LOGGING

Application-level tracing enables trace log output for every
page within an application.
Set trace enabled="true" to enable application trace logging.
If pageOutput="true", the

trace information will be displayed at the bottom of each
page. Otherwise, you can view the
application trace log by browsing the "trace.axd" page from
your web application

root.
-->

<trace enabled="false" requestLimit="10" pageOutput="false"
traceMode="SortByTime" localOnly="true" />

<!-- SESSION STATE SETTINGS

By default ASP.NET uses cookies to identify which requests
belong to a particular session.
If cookies are not available, a session can be tracked by
adding a session identifier to the URL.
To disable cookies, set sessionState cookieless="true".

-->

<sessionState mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data
source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20"
/>

<!-- GLOBALIZATION

This section sets the globalization settings of the application.
-->

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

</system.web>

</configuration>


---END SNIP---

I'm using his code kinda for the chapter, it is:

---BEGIN SNIP---

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net.Mail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">



Sub Page_Load()

Dim client As New SmtpClient()

client.Host = "localhost"

client.Port = 25

client.Send("(e-mail address removed)", "(e-mail address removed)", _

"Let's eat lunch!", "Lunch at the Steak House?")

End Sub



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Send Mail</title>

</head>

<body>

<form id="form1" runat="server">

<div>



Email sent!


</div>

</form>

</body>

</html>

--- END SNIP---

Basically his code is the same as in the book, the XXX, YYY, AAA, BBB
just substituted for my e-mail addresses.

Was there something I needed to do to web.config to make this work? I
went into iis, and went to the site, and it says that the ASP.net
version I'm using is 2.0.50727

Thanks for the help again on this. I think there are more reasons for
me to upgrade to VS 2005 (or is 2006 coming out soon that I should
wait, and maybe get the express edition just for learning?)

-David
 
J

Juan T. Llibre

re:
For starters, VS 2002 wouldn't be able to deal with the SmtpClient class because it doesn't exist
in that version of the .NET framework.

Ken,

I think he meant that he had VS 2002 installed previous to installing the 2.0 RTM.....but .Net 1.0 won't run on W2K3, so that may be a problem.

He's running the .Net Framework 2.0 :
....and he didn't use VS 2002 to edit it :
In any case, his C# version is now working :
I just tried the C# code, from the CD, and it worked fine.

The VB.NET code should run, too, regardless of whether he's running VS 2002 or not.
I bet that there's a typo in the code, somewhere.

On the question he poses : "Should I consider upgrading to 2005?",
the answer is a definite *YES*.

Editing code with VS 2002 will not make available the .Net Framework 2.0 objects.





Ken Cox said:
Hi David,

For starters, VS 2002 wouldn't be able to deal with the SmtpClient class because it doesn't exist
in that version of the .NET framework.

However, are you sure the problem isn't in the web.config for that chapter? I just checked and
needed to comment out this part of the web.config to get the page to run:

<pages pageBaseType="" />

You might want to see if there are any corrections in the author's support site.

Ken
Microsoft MVP [ASP.NET]



Hey all,

I'm still very new at all this, but am going through the ASP.net 2.0 unleashed book, first
chapter and trying to program my own little form emailer for fun. I tried following the code for
their version in the book to the best of my abiltiies, and came across issues, mostly that a type
was expected when trying to compile in MS Visual Studio 2002. When my stuff wasn't working at
all, I decided to try just using his code almost exactly. I copied it from the CD to my folder,
and edited just the e-mail addresses to my very own so that it wasn't going somewhere unexpected.
The system it's on is Windows .net Server 2003 Standard. The error I got, when running the page
was:

---BEGIN PASTE---

Compilation Error
Description: An error occurred during the compilation of a resource required to service this
request. Please review the following specific error details and modify your source code
appropriately.


Compiler Error Message: BC30182: Type expected.



Source Error:







Line 5:
Line 6: Sub Page_Load()

Line 7: Dim client As New SmtpClient()

Line 8: client.Host = "localhost"

Line 9: client.Port = 25





Source File: c:\inetpub\wwwroot\smtpclient\SendMail.aspx Line: 7






Show Detailed Compiler Output:





C:\WINDOWS\system32> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe" /t:library
/utf8output /R:"C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll"
/R:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\assembly\dl3\4de41090\1037aa00_40b8c601\smtpclient.DLL"
/R:"C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll"
/R:"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll"
/R:"C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll"
/R:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_global.asax.5w9amsgm.dll"
/out:"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.dll" /D:DEBUG=1 /debug+
/define:_MYTYPE=\"Web\"
/imports:Microsoft.VisualBasic,System,System.Collections,System.Collections.Specialized,System.Configuration,System.Text,System.Text.RegularExpressions,System.Web,System.Web.Caching,System.Web.SessionState,System.Web.Security,System.Web.Profile,System.Web.UI,System.Web.UI.WebControls,System.Web.UI.WebControls.WebParts,System.Web.UI.HtmlControls
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.0.vb"
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\smtpclient\8f865d90\1728c4ff\App_Web_nfetm-v4.1.vb"





Microsoft (R) Visual Basic Compiler version 8.0.50727.42

for Microsoft (R) .NET Framework version 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.



c:\inetpub\wwwroot\smtpclient\SendMail.aspx(7) : error BC30182: Type expected.



Dim client As New SmtpClient()





----End Paste----


I don't have the smtp mail setup on that server, but when trying my other form I changed the host
and ports accordlingly where it should have used the mail server. I even had tcpdump running on
the port (running all this through parallels) and nothing was sent. For some reason "SmtpClient"
just doesn't work correctly. Is this because I have an older version of Visual Studio (2002)?
Should I consider upgrading to 2005? I have a windows mobile 2005 PDA, which is the main reason
why I would want to get .net 2005 -- are there any other advantages?

Thanks for your help on this error, my start with .net is a little rough so far, but I'm hoping
to get better as I progress through this book. I apologize ahead of time if this question is too
"newbie like".

Thanks,

David
 
D

David Thole

Hey,

Yeah I don't understand this either. Running .net 2.0, and editing the
pages with notepad then letting the server compile them is what I was
doing for testing purposes. What confuses me is how C# could work
fine, and VB does not. I'm kinda beginning to wonder if I should just
learn C# instead of VB when going through this book. I have no
knowledge of C# right now, but I do know Java and C++ though.

-David
 
J

Juan T. Llibre

re:
Running .net 2.0, and editing the pages with notepad then letting the server compile them is what
I was doing for testing purposes.

I figured as much...

re:
What confuses me is how C# could work fine, and VB does not.

My guess is a typo in the code.
"Type expected" is a typical typo-produced error message, although not always.

re:
I'm kinda beginning to wonder if I should just learn C# instead of VB when going through this
book.

You probably should, although the error is not a good reason to do that.

;-)

re:
I have no knowledge of C# right now, but I do know Java and C++ though.

If you know Java and C++ you should be able to easily learn C#.
Those 3 languages have a lot of affinities between them.

As in all choices we make, choosing a primary development language for ASP.NET,
or for Windows apps, should be based on our previous programming experience.

Those who have a background in early Basic languages and/or VB6 will learn VB.NET more easily.
Those who have a background in C, C++ and/or Java will learn C# more easily.

In any case, you'll find that you'll probably pick up proficiency in both,
as you become a programming wizard, but the primary hurdle is the
one we should pay the most attention to.

In your case, I'd go for C#.
I prefer VB.NET, although I've picked up a bit of C#.

Our different programming backgrounds determine the best choice for each of us.
 
D

David Thole

This was the code I was using before, can you tell anything wrong with
it by chance?

--Begin Snip--

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net.Mail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">



Sub Page_Load()

Dim client As New SmtpClient()

client.Host = "localhost"

client.Port = 25

client.Send("(e-mail address removed)", "(e-mail address removed)", _

"Let's eat lunch!", "Lunch at the Steak House?")

End Sub



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Send Mail</title>

</head>

<body>

<form id="form1" runat="server">

<div>



Email sent!


</div>

</form>

</body>

</html>


--end snip--

Please note this has HTML in it, I don't know what reader you're using,
hopefully it doesn't try to render that (mine doesn't).

Thanks for your help on that. I'm probably going to go with C# to
learn, since it's a lot like what I already know to a degree..although
I do have 4 semesters of VB6 knowledge though as well. Hopefully I can
get a little of both for experience.

Thanks again,

David
 
J

Juan T. Llibre

re:
This was the code I was using before, can you tell anything wrong with it by chance?

Your VB code example worked fine for me by just changing the To and From
addresses, and changing the SMTP server to my SMTP server's name.

Maybe you have a VB.NET configuration problem ?

I must confess that I can find no explanation
for your VB code failing and the C# code succeeding.

As a last resort, have you tried uninstalling/reinstalling the .Net Framework 2.0 ?

Given the fact that the code works for me as copied from your post,
I can't pinpoint a reasonable cause for VB code not working and
C# code not working, unless you have a setup problem.

re:
Hopefully I can get a little of both for experience.

I'm sure you will.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top