View Single Post
  #1 (permalink)  
Old 08-02-2005
Vaughn Vaughn is offline
Member
 
Join Date: Aug 2004
Posts: 65
Rep Power: 0
Vaughn is on a distinguished road
Send a message via MSN to Vaughn Send a message via Yahoo to Vaughn
Default

How to send Emails via ASP using SMTP Authentication

There are several ways through which emails can be sent via ASP. The method which I am going to demonstrate will use CDOSYS and will use SMTP authentication. Secondly, I will encapsulate the code in a general function so that it can be used in any of your ASP pages by just including this file. The salient features of the code which I have demonstrated are:

1) Usage of a function, thus expanding portability.
2) Optional usage of fields like 'Cc' and 'Bcc' as per the senders discretion.
3) The sender can skip the 'From' email address if it is same as the SMTP user.
4) The sender can decide whether the content of the mail is to be send in text or html format.
5) After the mail is sent, an error number, if any, is returned. Thus, the sender can write proper error handling code, if required.

So let's begin!

A. Implementation

1) Create a file called 'EMail.asp', or you can choose any other name as per your preference.
2) Paste the below code into it:

Code:
<!--
 Â* Â*METADATA
 Â* Â*TYPE="typelib"
 Â* Â*UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
 Â* Â*NAME="CDO for Windows 2000 Library"
-->
<%
Function SendMail(SMTPServer, SMTPUserName, SMTPPassword, EMailFrom, EMailTo, EMailCc, EMailBcc, EMailSubject, EMailType, EMailContent)
 Â* Â*Dim cdoConfig
 Â* Â*Dim cdoMessage
 Â* Â*Dim intErr
 Â* Â*
 Â* Â*Set cdoConfig = CreateObject("CDO.Configuration")
 Â* Â*With cdoConfig.Fields
 Â* Â* Â* Â* .Item(cdoSendUsingMethod) = cdoSendUsingPort
 Â* Â* Â* Â* .Item(cdoSMTPServer) = SMTPServer
 Â* Â* Â* Â* .Item(cdoSMTPAuthenticate) = 1
 Â* Â* Â* Â* .Item(cdoSendUsername) = SMTPUserName
 Â* Â* Â* Â* .Item(cdoSendPassword) = SMTPPassword
 Â* Â* Â* Â* .Update
 Â* Â*End With
 Â* 
 Â* Â*Set cdoMessage = CreateObject("CDO.Message")
 Â* Â*With cdoMessage
 Â* Â* Â* Â* Set .Configuration = cdoConfig
 Â* Â* Â* Â* If Len(Trim(CStr(EMailFrom))) = 0 Then
 Â* Â* Â* Â* Â* Â* Â*.From = SMTPUserName
 Â* Â* Â* Â* Else
 Â* Â* Â* Â* Â* Â* Â*.From = EMailFrom
 Â* Â* Â* Â* End If
 Â* Â* Â* Â* .To = EMailTo
 Â* Â* Â* Â* If Len(Trim(CStr(EMailCc))) <> 0 Then .Cc = EMailCc
 Â* Â* Â* Â* If Len(Trim(CStr(EMailBcc))) <> 0 Then .Bcc = EMailBcc
 Â* Â* Â* Â* .Subject = EmailSubject
 Â* Â* Â* Â* If EMailType = "text" Then
 Â* Â* Â* Â* Â* Â* Â*.TextBody = EMailContent
 Â* Â* Â* Â* ElseIf EMailType = "html" Then
 Â* Â* Â* Â* Â* Â* Â*.HTMLBody = EMailContent
 Â* Â* Â* Â* End If
 Â* Â* Â* Â* .Send

 Â* Â* Â* Â* intErr = Err.Number
 Â* End With

 Â* Set cdoMessage = Nothing
 Â* Set cdoConfig = Nothing

 Â* SendMail = intErr
End Function
%>
Alternatively, you can download the file from here.

3) Save the file. You're done!

B. Usage

Using this function is extremely simple and just a matter of including the above file and calling the function. Let's take an example where you have to send mails from an ASP file called 'MailMembers.asp'. Follow the below steps to implement this:

1) Move the 'EMail.asp' in your 'includes' directory. Typically this directory is called 'includes' itself, personally I use that name for the sake of convenience.
2) Open the ASP file, in our case it would be 'MailMembers.asp'.
3) Scroll to the top and insert the below line at the top of the page, but below the '@Language' or 'Option Explicit' or any other directives, if you are using them:

Code:
<%@Language=VBScript%>
<% Option Explicit %>
<!-- #include virtual = "/Includes/Email.asp" -->
'Other Include files follows...
-----------------
-----------------
-----------------
'Your regular ASP code follows...
<%
-----------------
-----------------
-----------------
%>
4) Finally call the 'SendMail' at the point in your code where you need to send the mail. The below example demonstrates error trapping as well:

Code:
<%
'Declare Variables
Dim intError
Dim strResult

intError = SendMail("mail.mydomainname.com", "mail@mydomainname.com", "mypass", "admin@mydomainname.com", "john@accuwebhosting.com", "", "", "New membership!", "text", "Welcome to our club!")
If intError = 0 Then
 Â* Â*'No errors, mail sent
 Â* Â*strResult = "The mail has been sent successfully"
Else
 Â* Â*strResult = "There was a problem sending the mail"
 Â* Â*'Error Handling code follows
 Â* Â*-----------------
 Â* Â*-----------------
 Â* Â*-----------------
End If

'Inform the user
Response.Write(strResult)
%>
That's it! Happy mailing.

Your comments and/or suggestions are welcome.

Cheers!
__________________
Vaughn
Team Leader // Tech. Support
AccuWebHosting.Com
email:support@accuwebhosting.com

Blog: blogsandbloggers.com/Vaughn
http://www.accuwebhosting.biz/Vaughn...aughn_Sign.jpg
Reply With Quote