Can't send html email message with asp.net

Discussion in 'ASP .Net' started by Yossi Naggar, Jan 5, 2004.

  1. Yossi Naggar

    Yossi Naggar Guest

    Hello to everyone,
    Does anybody know how to send html formatted messages with asp.net?
    I tried to send such messages with the code below but the content type is wrong.
    The content type is multi-part and not html/text as expected.

    The code i use is:
    mailmsg.To = ""
    mailmsg.Subject = "some subject string"
    mailmsg.Body = <-- an html format body -->
    mailmsg.From = ""
    mailmsg.BodyFormat = MailFormat.Html
    Try
    SmtpMail.SmtpServer = "127.0.0.1"
    SmtpMail.Send(mailmsg)
    Catch exc As Exception
    Response.Write(exc.Message)
    Response.End()
    End Try
     
    Yossi Naggar, Jan 5, 2004
    #1
    1. Advertising

  2. A new FAQ has just been posted with lots of info that might help:

    http://www.systemwebmail.com/

    "Yossi Naggar" <> wrote in message
    news:...
    > Hello to everyone,
    > Does anybody know how to send html formatted messages with asp.net?
    > I tried to send such messages with the code below but the content type is
    > wrong.
    > The content type is multi-part and not html/text as expected.
    >
    > The code i use is:
    > mailmsg.To = ""
    > mailmsg.Subject = "some subject string"
    > mailmsg.Body = <-- an html format body -->
    > mailmsg.From = ""
    > mailmsg.BodyFormat = MailFormat.Html
    > Try
    > SmtpMail.SmtpServer = "127.0.0.1"
    > SmtpMail.Send(mailmsg)
    > Catch exc As Exception
    > Response.Write(exc.Message)
    > Response.End()
    > End Try
     
    Ken Cox [Microsoft MVP], Jan 5, 2004
    #2
    1. Advertising

  3. Here is what works for me:

    Dim MM As New System.Web.Mail.MailMessage
    Dim S As String
    MM.From = Me.txtFromEmail.Text
    MM.To = Me.txtToEmail.Text
    MM.Subject = Me.txtSubject.Text
    MM.BodyFormat = System.Web.Mail.MailFormat.Html
    S = "<html><head></head><body>"
    S = S & "<p></p>"
    S = S & Whatever HTML code you need to put for your message
    S = S & "</body></html>"
    MM.Body = S
    System.Web.Mail.SmtpMail.SmtpServer = Your SMPT server name (the
    name of your computer if you are testing on localhost)
    System.Web.Mail.SmtpMail.Send(MM)

    "Yossi Naggar" <> wrote in message
    news:...
    > Hello to everyone,
    > Does anybody know how to send html formatted messages with asp.net?
    > I tried to send such messages with the code below but the content type is

    wrong.
    > The content type is multi-part and not html/text as expected.
    >
    > The code i use is:
    > mailmsg.To = ""
    > mailmsg.Subject = "some subject string"
    > mailmsg.Body = <-- an html format body -->
    > mailmsg.From = ""
    > mailmsg.BodyFormat = MailFormat.Html
    > Try
    > SmtpMail.SmtpServer = "127.0.0.1"
    > SmtpMail.Send(mailmsg)
    > Catch exc As Exception
    > Response.Write(exc.Message)
    > Response.End()
    > End Try
     
    William LaMartin, Jan 5, 2004
    #3
  4. Yossi Naggar

    Yossi Naggar Guest

    Well, your code sure works.
    But i encountered another problem: the body of the message (which is
    ofcourse an HTML code) is the value of a textarea in my web
    application.

    If i set the MM.Body as you have done then it's alright, but if i set
    MM.Body to be the value of the textarea then i get the message in the
    wrong format.

    What do you think is the problem?

    "William LaMartin" <> wrote in message news:<>...
    > Here is what works for me:
    >
    > Dim MM As New System.Web.Mail.MailMessage
    > Dim S As String
    > MM.From = Me.txtFromEmail.Text
    > MM.To = Me.txtToEmail.Text
    > MM.Subject = Me.txtSubject.Text
    > MM.BodyFormat = System.Web.Mail.MailFormat.Html
    > S = "<html><head></head><body>"
    > S = S & "<p></p>"
    > S = S & Whatever HTML code you need to put for your message
    > S = S & "</body></html>"
    > MM.Body = S
    > System.Web.Mail.SmtpMail.SmtpServer = Your SMPT server name (the
    > name of your computer if you are testing on localhost)
    > System.Web.Mail.SmtpMail.Send(MM)
    >
    > "Yossi Naggar" <> wrote in message
    > news:...
    > > Hello to everyone,
    > > Does anybody know how to send html formatted messages with asp.net?
    > > I tried to send such messages with the code below but the content type is

    > wrong.
    > > The content type is multi-part and not html/text as expected.
    > >
    > > The code i use is:
    > > mailmsg.To = ""
    > > mailmsg.Subject = "some subject string"
    > > mailmsg.Body = <-- an html format body -->
    > > mailmsg.From = ""
    > > mailmsg.BodyFormat = MailFormat.Html
    > > Try
    > > SmtpMail.SmtpServer = "127.0.0.1"
    > > SmtpMail.Send(mailmsg)
    > > Catch exc As Exception
    > > Response.Write(exc.Message)
    > > Response.End()
    > > End Try
     
    Yossi Naggar, Jan 5, 2004
    #4
  5. Yossi Naggar

    Yossi Naggar Guest

    Well, i am glad to tell you that the problem solved:

    The body of my message was set to the value of a textarea control.
    The reason why i couldn't see html was the replacement of the
    characters "<", ">", " " etc. with the special characters: "&lt;",
    "&gt;", "&nbsp;" and so on.

    It seems that if you write an HTML code in textarea or other control,
    then the .NET replaces the characters: "<", "<", etc. with the special
    characteres above.

    In order to get rid of those special characters you can do:

    MM.Body = replace(MM.Body, "&lt;", "<")
    MM.Body = replace(MM.Body, "&gt;", ">")
    .....

    and so on

    BUT: if you want to solve the problem with even more simple way you
    can do:

    MM.Body = Me.Textarea1.Value.InnerText

    This works just fine.
    "William LaMartin" <> wrote in message news:<>...
    > Here is what works for me:
    >
    > Dim MM As New System.Web.Mail.MailMessage
    > Dim S As String
    > MM.From = Me.txtFromEmail.Text
    > MM.To = Me.txtToEmail.Text
    > MM.Subject = Me.txtSubject.Text
    > MM.BodyFormat = System.Web.Mail.MailFormat.Html
    > S = "<html><head></head><body>"
    > S = S & "<p></p>"
    > S = S & Whatever HTML code you need to put for your message
    > S = S & "</body></html>"
    > MM.Body = S
    > System.Web.Mail.SmtpMail.SmtpServer = Your SMPT server name (the
    > name of your computer if you are testing on localhost)
    > System.Web.Mail.SmtpMail.Send(MM)
    >
    > "Yossi Naggar" <> wrote in message
    > news:...
    > > Hello to everyone,
    > > Does anybody know how to send html formatted messages with asp.net?
    > > I tried to send such messages with the code below but the content type is

    > wrong.
    > > The content type is multi-part and not html/text as expected.
    > >
    > > The code i use is:
    > > mailmsg.To = ""
    > > mailmsg.Subject = "some subject string"
    > > mailmsg.Body = <-- an html format body -->
    > > mailmsg.From = ""
    > > mailmsg.BodyFormat = MailFormat.Html
    > > Try
    > > SmtpMail.SmtpServer = "127.0.0.1"
    > > SmtpMail.Send(mailmsg)
    > > Catch exc As Exception
    > > Response.Write(exc.Message)
    > > Response.End()
    > > End Try
     
    Yossi Naggar, Jan 5, 2004
    #5
  6. use Server.HTMLEncode to restore your textarea entry or use the
    VaildateRequest=false Page attribute. This behavior is new in .NET 1.1 due
    to security considerations. Hope this helps.
    --
    Peter O'Reilly
     
    Peter O'Reilly, Jan 5, 2004
    #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. =?Utf-8?B?UmljaCBSZWtvcw==?=

    Send HTML Email from ASP.NET

    =?Utf-8?B?UmljaCBSZWtvcw==?=, Jan 12, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    514
    =?Utf-8?B?UmljaCBSZWtvcw==?=
    Jan 13, 2005
  2. DaVinci
    Replies:
    2
    Views:
    273
    Keith Thompson
    Apr 8, 2006
  3. Vellingiri Arul

    how can send email message using ruby?

    Vellingiri Arul, Oct 16, 2007, in forum: Ruby
    Replies:
    6
    Views:
    146
    Mohit Sindhwani
    Oct 17, 2007
  4. pbd22
    Replies:
    1
    Views:
    172
  5. Replies:
    2
    Views:
    104
    Richard Balbat
    Nov 12, 2013
Loading...

Share This Page