ASPNET 2.0: Sending mail in HTML

J

Jim in Arizona

Now that I've figured out how to send mail within our domain:

Dim test as new System.Net.Mail.SmtpClient
Dim nco as New
System.Net.NetworkCredential("mailaccount","password","domain")
test.Credentials = nco
test.Host = "mailserver"
test.Send("(e-mail address removed)","(e-mail address removed)","Subject","Message")

I am wondering how I could send mail in HTML format, or any other format
other than plain ol' text. If I try something like this:

Dim testtext as String = "<p style=""font-size:18pt"">Testing</p>"
test.Send("(e-mail address removed)","(e-mail address removed)","Subject",testtext)

The message body will end up being <p style="font-size:18pt">Testing</p>

How can I fix this so I can send visually appealing messages?

Thanks,
Jim
 
P

Paul Henderson

I am wondering how I could send mail in HTML format, or any other format
other than plain ol' text. If I try something like this:
Dim testtext as String = "<p style=""font-size:18pt"">Testing</p>"
test.Send("(e-mail address removed)","(e-mail address removed)","Subject",testtext)
The message body will end up being <p style="font-size:18pt">Testing</p>
How can I fix this so I can send visually appealing messages?

You can use the System.Net.Mail.MailMessage class to construct a
message object, which can then be sent through an overload of
SmtpClient.Send. The MailMessage object has a four-parameter overloaded
constructor matching that for SmtpClient (i.e. To, From, Subject,
Body), but you can also manipulate the Body directly, and set
BodyIsHtml to be true to have the correct Content-Type set for the
message.
 
J

Jim in Arizona

Paul said:
You can use the System.Net.Mail.MailMessage class to construct a
message object, which can then be sent through an overload of
SmtpClient.Send. The MailMessage object has a four-parameter overloaded
constructor matching that for SmtpClient (i.e. To, From, Subject,
Body), but you can also manipulate the Body directly, and set
BodyIsHtml to be true to have the correct Content-Type set for the
message.

I couldn't quite figure out the code on this. I tried several things,
but still don't get any HTML. Here's what I ended up with that doesn't work.

Dim bodytest As New System.Net.Mail.MailMessage
bodytest.Body = "<p style=""font-size:20pt;"">Test</p>"
bodytest.IsBodyHtml = True

Dim test As New System.Net.Mail.SmtpClient
Dim nco As New System.Net.NetworkCredential("useraccount", "password",
"domain")
test.Credentials = nco
test.Host = "mailserver"
test.Send("(e-mail address removed)", "(e-mail address removed)", "Test
Subject", bodytest.Body)


the send method of the Net.Mail.SmtpClient has two overloads, that I'm
aware of:

Send(string from, string recipients, string subject, string body)
And
Send(System.Net.Mail.MailMessage message)

If I just try:
test.Send(bodytest)
An exception is thrown saying there is no "To" address specified.

I don't know how I would use the mail.mailmessage within the Send(string
from, string recipients, string subject, string body). I i use it the
way I have in my code above, it shows the HTML instead of rendering it
properly.
 
P

Paul Henderson

I don't know how I would use the mail.mailmessage within the Send(string
from, string recipients, string subject, string body). I i use it the
way I have in my code above, it shows the HTML instead of rendering it
properly.

Sorry, some rather bad explanation there by me ;-).

Try:

Dim bodytest As New
System.Net.Mail.MailMessage("(e-mail address removed)",
"(e-mail address removed)", "Test Subject", "<p
style=""font-size:20pt;"">Test</p>")
bodytest.IsBodyHtml = True

Dim test As New System.Net.Mail.SmtpClient
Dim nco As New System.Net.NetworkCredential("useraccount", "password",
"domain")
test.Credentials = nco
test.Host = "mailserver"
test.Send(bodytest)
 
J

Jim in Arizona

Paul said:
Sorry, some rather bad explanation there by me ;-).

Try:

Dim bodytest As New
System.Net.Mail.MailMessage("(e-mail address removed)",
"(e-mail address removed)", "Test Subject", "<p
style=""font-size:20pt;"">Test</p>")
bodytest.IsBodyHtml = True

Dim test As New System.Net.Mail.SmtpClient
Dim nco As New System.Net.NetworkCredential("useraccount", "password",
"domain")
test.Credentials = nco
test.Host = "mailserver"
test.Send(bodytest)

Perfect Paul. Thanks again for your persistent help! This mail stuff
will greatly improve the help desk ticket system I made up a while back.

Jim
 

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,600
Members
45,181
Latest member
RexGreenwa
Top