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
Comments
- Anonymous
June 28, 2006
The comment has been removed - Anonymous
June 28, 2006
Dim toAddr As New MailAddress(trobbins@microsoft.com)
I didn't know VB was this soffisticated =oP - Anonymous
June 28, 2006
Have you thought about MIME encodings ? - Anonymous
June 28, 2006
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 - Anonymous
June 28, 2006
The comment has been removed - Anonymous
July 26, 2006
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! - Anonymous
July 26, 2006
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. - Anonymous
July 26, 2006
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...? - Anonymous
July 26, 2006
The comment has been removed