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

Can't send html email message with asp.net

P: n/a
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 = "XX*@XXXX.com"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "XX*@XXXX.com"
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
A new FAQ has just been posted with lots of info that might help:

http://www.systemwebmail.com/

"Yossi Naggar" <na****@netvision.net.il> wrote in message
news:d3**************************@posting.google.c om...
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 = "XX*@XXXX.com"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "XX*@XXXX.com"
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


Nov 18 '05 #2

P: n/a
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" <na****@netvision.net.il> wrote in message
news:d3**************************@posting.google.c om...
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 = "XX*@XXXX.com"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "XX*@XXXX.com"
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

Nov 18 '05 #3

P: n/a
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" <la******@tampabay.rr.com> wrote in message news:<eP**************@TK2MSFTNGP09.phx.gbl>...
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" <na****@netvision.net.il> wrote in message
news:d3**************************@posting.google.c om...
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 = "XX*@XXXX.com"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "XX*@XXXX.com"
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

Nov 18 '05 #4

P: n/a
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" <la******@tampabay.rr.com> wrote in message news:<eP**************@TK2MSFTNGP09.phx.gbl>...
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" <na****@netvision.net.il> wrote in message
news:d3**************************@posting.google.c om...
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 = "XX*@XXXX.com"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "XX*@XXXX.com"
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

Nov 18 '05 #5

P: n/a
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
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.