Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Thursday, November 20, 2008

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

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.

Related Articles
   Related Document Sending Mail


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 7/24/2005 3:20:45 PM by Anonymous
Comments: Of course you need 3rd party element if you want to send embedded images in a mail message
Left on 3/14/2005 12:35:56 PM by Anonymous
Comments: smtp servers now require authorization
Left on 12/19/2004 1:57:11 PM by Anonymous
Comments: I always get the following error on debugging:
An unhandled exception of type 'System.Web.HttpException' occurred in system.web.dll

Additional information: Could not access 'CDO.Message' object.

No ratings available.
Left on 11/8/2004 3:14:05 AM by Anonymous
Comments: i  wont  nagarjuna phone  number
Left on 11/1/2004 4:37:54 AM by Anonymous
Comments: Best...........
Left on 7/14/2004 11:27:04 AM by Anonymous
Comments: Thanks for the sample. Can you show how to send embedded images in an html formatted message? PLEASE!
Left on 12/10/2003 8:32:42 AM by Anonymous
Comments: SMTPMail struggles with win98  which is not mentioned
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970