System.Web.Mail The classes in this namespace have been deprecated

Switching from system.web.mail to System.Net.Mail is pretty easy and straight forward, the syntax is a little different and instead of having the server declared in the class you declare it in the web.config.

Old System.Web.Mail version

Function SendEmail(ByVal strEmailTo As String, ByVal strEmailFrom As String, ByVal strSubject As String, ByVal strBody As String, ByVal strBodyFormat As String) As Boolean
        Dim email As New System.Web.Mail.MailMessage
        Dim bSent As Boolean = False
        If Trim(strEmailTo) <> "" Then
            email.To = strEmailTo
            email.From = strEmailFrom
            email.Body = strBody
            email.Subject = strSubject
            If UCase(strBodyFormat) = "TEXT" Then
                email.BodyFormat = Web.Mail.MailFormat.Text
            Else
                email.BodyFormat = Web.Mail.MailFormat.Html
            End If
            System.Web.Mail.SmtpMail.SmtpServer = "smtp.yourdomain.com"
			Try
				System.Web.Mail.SmtpMail.Send(email)
				bSent = True
			Catch ex As Exception
				bSent = False
			End Try
		End If
		SendEmail = bSent
    End Function

New System.net.Mail Version

 Function SendEmail(ByVal strEmailTo As String, ByVal strEmailFrom As String, ByVal strSubject As String, ByVal strBody As String, ByVal strBodyFormat As String) As Boolean
        Dim email As New MailMessage()
        Dim bSent As Boolean = False
        If Trim(strEmailTo) <> "" Then
            email.To.Add(New MailAddress(strEmailTo))
            email.From = New MailAddress(strEmailFrom)
            email.Body = strBody
            email.Subject = strSubject
            If UCase(strBodyFormat) = "TEXT" Then
                email.IsBodyHtml = "false"
            Else
                email.IsBodyHtml = "true"
            End If
            
            Try
                'System.Web.Mail.SmtpMail.Send(email)
                Dim mSmtpClient As New SmtpClient()
                ' Send the mail message
                mSmtpClient.Send(email)
                bSent = True
            Catch ex As Exception
                bSent = False
            End Try
        End If
        SendEmail = bSent
    End Function

Add the server to the web.config

<configuration>
   <system.net>
      <mailSettings>
         <smtp from="defaultEmail@yourdomain.com">
            <network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
         </smtp>
      </mailSettings>
   </system.net>
</configuration>

source

     

Leave a Reply