System.Web.Mail.MailMessage in class

Discussion in 'ASP .Net' started by martin, Dec 17, 2003.

  1. martin

    martin Guest

    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.
     
    martin, Dec 17, 2003
    #1
    1. Advertising

  2. martin

    Morgan Guest

    > 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" <> wrote in message
    news:uG$...
    > 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.
    >
    >
     
    Morgan, Dec 17, 2003
    #2
    1. Advertising

  3. martin

    martin Guest

    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" <> wrote in message
    news:uM%...
    > > 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" <> wrote in message
    > news:uG$...
    > > 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.
    > >
    > >

    >
    >
     
    martin, Dec 18, 2003
    #3
  4. martin

    Morgan Guest

    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" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:uM%...
    > > > 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" <> wrote in message
    > > news:uG$...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Morgan, Dec 18, 2003
    #4
  5. martin

    martin Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%...
    > > 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" <> wrote in message
    > > news:uM%...
    > > > > 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" <> wrote in message
    > > > news:uG$...
    > > > > 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.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    martin, Dec 19, 2003
    #5
  6. martin

    Hans Kesting Guest

    "martin" <> wrote in message
    news:...
    > 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
     
    Hans Kesting, Dec 19, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bertus Dam
    Replies:
    5
    Views:
    890
    Bertus Dam
    Aug 29, 2003
  2. stuart
    Replies:
    1
    Views:
    2,600
    Jacky Kwok
    Nov 10, 2003
  3. techie
    Replies:
    3
    Views:
    2,629
    Phil Wilson
    May 12, 2004
  4. gh0st54
    Replies:
    0
    Views:
    361
    gh0st54
    Oct 26, 2004
  5. =?Utf-8?B?TWlja2VCb3k=?=

    Problem with System.Web.Mail.MailMessage and HTML mail

    =?Utf-8?B?TWlja2VCb3k=?=, Jun 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    931
    =?Utf-8?B?TWlja2VCb3k=?=
    Jun 21, 2005
Loading...

Share This Page