Can't send html email message with asp.net

Y

Yossi Naggar

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 = "(e-mail address removed)"
mailmsg.Subject = "some subject string"
mailmsg.Body = <-- an html format body -->
mailmsg.From = "(e-mail address removed)"
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
 
W

William LaMartin

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)
 
Y

Yossi Naggar

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?
 
Y

Yossi Naggar

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.
 
P

Peter O'Reilly

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top