System.Web.Mail.MailMessage in class

  • Thread starter Thread starter martin
  • Start date Start date
M

martin

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.
 
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
 
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.
 
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
 
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.
 
martin said:
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
 
Back
Top