Sending Email from your .Net Application
6/3/2003 9:57:53 PM
The following article covers using the built-in System.Web.Mail objects for writing code to email from a Web page using VB and ASP.Net.
The .Net framework comes complete with feature rich email sending capabilities. No more need to use third party components, now you have it built into the framework.
The following code provides a quick overview of calling the MailMessage object from ASP.Net
First make sure you create a reference to the System.Web.Mail at the top of your VB class file.
Imports System.Web.Mail
The following code snippet is a VB subroutine that we create for calling from any of your ASPX pages.
Public Sub SendEmail(ByVal strFrom As String, ByVal strTo As String, ByVal strBcc As String, ByVal strSubject As String, ByVal strBody As String, Optional ByVal strAttachment As String = "", Optional ByVal strBodyType As String = "")
Dim mail As New MailMessage 'set the email options for sending mail.From = strFrom mail.To = strTo If strBcc <> "" Then mail.Bcc = strBcc End If mail.Subject = strSubject mail.Body = strBody
'here we check if we want to format the email as html or plain text. If strBodyType <> "" Then Select Case LCase(strBodyType) Case "html" mail.BodyFormat = MailFormat.Html Case "text" mail.BodyFormat = MailFormat.Text End Select End If
'if there is an attachment send it. If strAttachment <> "" Then mail.Attachments.Add(New MailAttachment(strAttachment)) End If
' external SMTP server SmtpMail.SmtpServer = “yourmailserver.santry.com“
Try SmtpMail.Send(mail) Catch ' any errors here do something, notify the user, etc. End Try
End Sub
By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.
|