By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

System.Web.Mail.MailMessage in class

P: n/a
Hi,

I have created a class that is totally seperate from my web application.
However this class is used extensivly by the web application for stuff like
data access.
I wish to add a function to this class that will send email, however my
class will not seem to recognise the
system.web.mail.mailmessage.

my first impression on this is that I may have to use some sort of third
part mail component to send mail from inside my class,
however I am baffled why the class will not recognise the
system.web.mail.mailmessage namespace

The code that I normally use to send mail is listed below, this does not
work from inside my class

Dim Message As System.Web.Mail.MailMessage = New
System.Web.Mail.MailMessage
Dim SmtpMail As System.Web.Mail.SmtpMail
Message.To = strEmailAddress
Message.From = txtFrom.Text
Trace.Warn("FROM ADDRESS", txtFrom.Text)
Message.Subject = txtSubject.Text
Message.Body = strSendEmailText

Message.BodyFormat = Mail.MailFormat.Html

SmtpMail.SmtpServer = "saturn"

SmtpMail.Send(Message)

any help is greatly appreciated

cheers

martin.
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> I have created a class that is totally seperate from my web application.
I assume this is in a seperate Class Library project?
If so, you'll need to add a reference to the System.Web library to your
Class Library project.

HTH,

Morgan

"martin" <ma***************@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a class that is totally seperate from my web application. However this class is used extensivly by the web application for stuff like data access.
I wish to add a function to this class that will send email, however my
class will not seem to recognise the
system.web.mail.mailmessage.

my first impression on this is that I may have to use some sort of third
part mail component to send mail from inside my class,
however I am baffled why the class will not recognise the
system.web.mail.mailmessage namespace

The code that I normally use to send mail is listed below, this does not
work from inside my class

Dim Message As System.Web.Mail.MailMessage = New
System.Web.Mail.MailMessage
Dim SmtpMail As System.Web.Mail.SmtpMail
Message.To = strEmailAddress
Message.From = txtFrom.Text
Trace.Warn("FROM ADDRESS", txtFrom.Text)
Message.Subject = txtSubject.Text
Message.Body = strSendEmailText

Message.BodyFormat = Mail.MailFormat.Html

SmtpMail.SmtpServer = "saturn"

SmtpMail.Send(Message)

any help is greatly appreciated

cheers

martin.

Nov 18 '05 #2

P: n/a
Hi,

Maybe I wasn't clear enough in my orignal post.
The new class library will not let me make a referenece to
system.web.mail.mailmessage
it will let me make a reference to
system.web
but this is no good to me, as I want access to the "Mail" part of the
namespace.
It would appear that most of the system.web namespace is only availible from
a web application.

The only system.web namespaces that are availible to import in my class are

Imports System.Web.AspNetHostingPermission
Imports System.Web.AspNetHostingPermissionAttribute
Imports System.Web.AspNetHostingPermissionLevel

This is my whole problem,
if I cant import the namespace how can I use the classes.
I have tried making a fully qualified refernece to the object but that will
not work either.

I would appreciate any thoughts.

cheers

martin.



"Morgan" <za**********@primposta.com> wrote in message
news:uM****************@TK2MSFTNGP12.phx.gbl...
I have created a class that is totally seperate from my web application.

I assume this is in a seperate Class Library project?
If so, you'll need to add a reference to the System.Web library to your
Class Library project.

HTH,

Morgan

"martin" <ma***************@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a class that is totally seperate from my web

application.
However this class is used extensivly by the web application for stuff

like
data access.
I wish to add a function to this class that will send email, however my
class will not seem to recognise the
system.web.mail.mailmessage.

my first impression on this is that I may have to use some sort of third
part mail component to send mail from inside my class,
however I am baffled why the class will not recognise the
system.web.mail.mailmessage namespace

The code that I normally use to send mail is listed below, this does not
work from inside my class

Dim Message As System.Web.Mail.MailMessage = New
System.Web.Mail.MailMessage
Dim SmtpMail As System.Web.Mail.SmtpMail
Message.To = strEmailAddress
Message.From = txtFrom.Text
Trace.Warn("FROM ADDRESS", txtFrom.Text)
Message.Subject = txtSubject.Text
Message.Body = strSendEmailText

Message.BodyFormat = Mail.MailFormat.Html

SmtpMail.SmtpServer = "saturn"

SmtpMail.Send(Message)

any help is greatly appreciated

cheers

martin.


Nov 18 '05 #3

P: n/a
You cannot reference system.web.mail.mailmessage, as it is a member of the
System.Web library, you can only create an instance of the object
mailmessage

I don't know what you're working with, but try this for starters...
<assuming VB.Net>
1. Create a new ClassLibrary project
2. Right click on the "References" folder in the Solution Explorer and "Add
Reference"
3. Select System.Web from the .Net tab.
4. In the Class1.vb file, paste this code:

Imports System.Web

Public Class Class1

Private Sub SendMessage()

Dim msg As New System.Web.Mail.MailMessage()

With msg

End With

End Sub

End Class

No errors when I build. Seems you've got something going on in your project
that isn't allowing access to the root system.web library.

--Morgan
"martin" <ma***************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

Maybe I wasn't clear enough in my orignal post.
The new class library will not let me make a referenece to
system.web.mail.mailmessage
it will let me make a reference to
system.web
but this is no good to me, as I want access to the "Mail" part of the
namespace.
It would appear that most of the system.web namespace is only availible from a web application.

The only system.web namespaces that are availible to import in my class are
Imports System.Web.AspNetHostingPermission
Imports System.Web.AspNetHostingPermissionAttribute
Imports System.Web.AspNetHostingPermissionLevel

This is my whole problem,
if I cant import the namespace how can I use the classes.
I have tried making a fully qualified refernece to the object but that will not work either.

I would appreciate any thoughts.

cheers

martin.



"Morgan" <za**********@primposta.com> wrote in message
news:uM****************@TK2MSFTNGP12.phx.gbl...
I have created a class that is totally seperate from my web application.
I assume this is in a seperate Class Library project?
If so, you'll need to add a reference to the System.Web library to your
Class Library project.

HTH,

Morgan

"martin" <ma***************@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have created a class that is totally seperate from my web

application.
However this class is used extensivly by the web application for stuff

like
data access.
I wish to add a function to this class that will send email, however

my class will not seem to recognise the
system.web.mail.mailmessage.

my first impression on this is that I may have to use some sort of third part mail component to send mail from inside my class,
however I am baffled why the class will not recognise the
system.web.mail.mailmessage namespace

The code that I normally use to send mail is listed below, this does not work from inside my class

Dim Message As System.Web.Mail.MailMessage = New
System.Web.Mail.MailMessage
Dim SmtpMail As System.Web.Mail.SmtpMail
Message.To = strEmailAddress
Message.From = txtFrom.Text
Trace.Warn("FROM ADDRESS", txtFrom.Text)
Message.Subject = txtSubject.Text
Message.Body = strSendEmailText

Message.BodyFormat = Mail.MailFormat.Html

SmtpMail.SmtpServer = "saturn"

SmtpMail.Send(Message)

any help is greatly appreciated

cheers

martin.



Nov 18 '05 #4

P: n/a
Hi Morgan,

thnaks for you help. the thing that I was missing was not adding a
referenece in my project.

I thought simply writing

imports system.web at the top of the vb file would do the trick.

seems it won't and I have to add the refernce.

anyway, everything works perfectly now so thanks.

cheers

martin.

"Morgan" <za**********@primposta.com> wrote in message
news:eT**************@TK2MSFTNGP11.phx.gbl...
You cannot reference system.web.mail.mailmessage, as it is a member of the
System.Web library, you can only create an instance of the object
mailmessage

I don't know what you're working with, but try this for starters...
<assuming VB.Net>
1. Create a new ClassLibrary project
2. Right click on the "References" folder in the Solution Explorer and "Add Reference"
3. Select System.Web from the .Net tab.
4. In the Class1.vb file, paste this code:

Imports System.Web

Public Class Class1

Private Sub SendMessage()

Dim msg As New System.Web.Mail.MailMessage()

With msg

End With

End Sub

End Class

No errors when I build. Seems you've got something going on in your project that isn't allowing access to the root system.web library.

--Morgan
"martin" <ma***************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

Maybe I wasn't clear enough in my orignal post.
The new class library will not let me make a referenece to
system.web.mail.mailmessage
it will let me make a reference to
system.web
but this is no good to me, as I want access to the "Mail" part of the
namespace.
It would appear that most of the system.web namespace is only availible

from
a web application.

The only system.web namespaces that are availible to import in my class

are

Imports System.Web.AspNetHostingPermission
Imports System.Web.AspNetHostingPermissionAttribute
Imports System.Web.AspNetHostingPermissionLevel

This is my whole problem,
if I cant import the namespace how can I use the classes.
I have tried making a fully qualified refernece to the object but that

will
not work either.

I would appreciate any thoughts.

cheers

martin.



"Morgan" <za**********@primposta.com> wrote in message
news:uM****************@TK2MSFTNGP12.phx.gbl...
> I have created a class that is totally seperate from my web application. I assume this is in a seperate Class Library project?
If so, you'll need to add a reference to the System.Web library to your Class Library project.

HTH,

Morgan

"martin" <ma***************@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I have created a class that is totally seperate from my web
application.
> However this class is used extensivly by the web application for stuff like
> data access.
> I wish to add a function to this class that will send email, however my > class will not seem to recognise the
> system.web.mail.mailmessage.
>
> my first impression on this is that I may have to use some sort of third > part mail component to send mail from inside my class,
> however I am baffled why the class will not recognise the
> system.web.mail.mailmessage namespace
>
> The code that I normally use to send mail is listed below, this does not > work from inside my class
>
> Dim Message As System.Web.Mail.MailMessage = New
> System.Web.Mail.MailMessage
> Dim SmtpMail As System.Web.Mail.SmtpMail
> Message.To = strEmailAddress
> Message.From = txtFrom.Text
> Trace.Warn("FROM ADDRESS", txtFrom.Text)
> Message.Subject = txtSubject.Text
> Message.Body = strSendEmailText
>
> Message.BodyFormat = Mail.MailFormat.Html
>
> SmtpMail.SmtpServer = "saturn"
>
> SmtpMail.Send(Message)
>
> any help is greatly appreciated
>
> cheers
>
> martin.
>
>



Nov 18 '05 #5

P: n/a

"martin" <ma***************@hotmail.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
Hi Morgan,

thnaks for you help. the thing that I was missing was not adding a
referenece in my project.

I thought simply writing

imports system.web at the top of the vb file would do the trick.

seems it won't and I have to add the refernce.

anyway, everything works perfectly now so thanks.

cheers

martin.


"Imports" in VB (and the equivalent "using" in C#) only make it easier to
write
the code: you don't have to type the complete namespace path if that is
already
mentioned in an Import/using clause.
Without an Import/using you can still access everything you have referenced,
but then you need to type the complete namespace.
As you found out, "referencing" is the important part.

Hans Kesting
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.