Welcome to MSDN Blogs Sign in | Join | Help

Sending an HTML Email with Visual Studio 2005

Question: I am trying to send an email with Visual Studio 2005 using System.Web.Mail and it keeps telling me that it is obsolete? What should I use? Also if you have an example of how to send an HTML email that would also help.

Answer: Within Visual Studio 2005 you can use the System.Net.Mail namespace. This contains the classes used to send email using SMTP. The MailMessage class is used to represent the content of an email message. The SmtpClient transmits email to the SMTP host that you designate for mail delivery. For example you can use the following code within a button of a Windows form to send an HTML email.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim client As New SmtpClient("smtp.server")
       
Dim toAddr As New MailAddress(trobbins@microsoft.com)  
       
Dim fromAddr As New MailAddress(trobbins@microsoft.com)
        Dim message As New MailMessage(fromAddr, toAddr)

        message.IsBodyHtml = True
        
message.Subject = "This is a Test"

        message.Body = "<html><body><b>This is an </b> <font color=red>HTML Message</font></body></html> "

        client.Send(message)

    End Sub

The email that is sent looks like the following

Published Wednesday, June 28, 2006 1:24 PM by trobbins

Comments

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, June 28, 2006 2:20 PM by JohnGalt
NOTE TO EVERYONE:

Please don't forget the .IsBodyHTML property! To offten I see HTML in plain text message parts and because Outlook does freak, you think the message was OK. It isn't, Outlook just displays crap well, and kills the rest of us trying to follow the standards!

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, June 28, 2006 6:06 PM by Eber Irigoyen
Dim toAddr As New MailAddress(trobbins@microsoft.com)

I didn't know VB was this soffisticated =oP

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, June 28, 2006 6:59 PM by senthil
Have you thought about MIME encodings ?

# re: Sending an HTML Email with Visual Studio 2005

Thursday, June 29, 2006 12:54 AM by trobbins
For Mime encodings .NET 2.0 has a new namespace of system.net.mime that should help with that.

http://msdn2.microsoft.com/en-us/library/system.net.mime.aspx

has someinformation about it

# re: Sending an HTML Email with Visual Studio 2005

Thursday, June 29, 2006 7:34 AM by Tobie Fysh
Surely the HTML needs to read :

....<font color=red>ASP.NET</font>.....

(as well as some of the rest of the HTML and the subject line......)

:-)

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, July 26, 2006 1:41 PM by j
Hey, nice example, I tried it and I'm using my pc as the mail server ie using 127.0.0.1 to replace your "smtp.server".

I'm having a problem though because all the mail seems to be staying in the Queue folder (btw, i'm using w2k3), any ideas? (I'm tryin to send the message To an external address, let's say j@wdser.com)

THanks in advance!

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, July 26, 2006 7:50 PM by trobbins
You need to make sure that you have both SMTP and a relay set up to get the email out. I think there are some articles on MSDN/Technet that talk about the actual set up.

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, July 26, 2006 8:00 PM by j
I believe I have and I've checked the log and I see comments such as:

#Fields: time c-ip cs-method cs-uri-stem sc-status
23:04:04 127.0.0.1 EHLO - 250
23:04:04 127.0.0.1 MAIL - 250
23:04:04 127.0.0.1 RCPT - 250
23:04:04 127.0.0.1 DATA - 250
23:05:43 127.0.0.1 QUIT - 240

Which I believe should mean that there were no problems in sending...?

# re: Sending an HTML Email with Visual Studio 2005

Wednesday, July 26, 2006 8:02 PM by j
I'm gona head to technet now though, so thanks for that tip.
(sorry to post again, i forgot to include my 'thanks' ;))
New Comments to this post are disabled
 
Page view tracker