August 2005 Entries
VS 2005 Create Email code snippet needs to be changed

I am working on VS 2005 website, I need to send an email so I tried the Insert “Code Snippet“ feature and it inserted:

Dim email As New MailMessage()

email.To = "RecipientAddress"

email.From = "SenderAddress"

email.Body = "MessageText"

email.Subject = "SubjectText"

email.BodyFormat = MailFormat.Text

SmtpMail.SmtpServer = "SmtpServerName"

SmtpMail.Send(email)

but the designer gives you the following warning

Warning 15 'System.Web.Mail.MailMessage' is obsolete: 'This class is deprecated.  Please use the System.Net.Mail classes instead. http://go.microsoft.com/fwlink/?linkid=14202' 

so my own future refrence here is what it should be

Dim sMailServer As String = ConfigurationManager.AppSettings().Item("MailServer")

Dim sfromName As String = dr("FName").ToString & " " & dr("LName").ToString

Dim o_Client As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(sMailServer)

Dim o_FromAddress As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress(sFrom, sFromName)

Dim o_ToAddress As System.Net.Mail.MailAddress = New System.Net.Mail.MailAddress(sTo, sTo)

Dim o_Message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(o_FromAddress, o_ToAddress)

o_Message.Subject = sSubject

o_Message.Body = sBody

o_Client.Send(o_Message)