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:
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:
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:
That's it! Happy mailing.
Your comments and/or suggestions are welcome.
Cheers!
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)))41; <> 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 %>
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... <% ----------------- ----------------- ----------------- %>
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 39;No errors, mail sent strResult = "The mail has been sent successfully" Else strResult = "There was a problem sending the mail" 39;Error Handling code follows End If 'Inform the user Response.Write(strResult) %>
Your comments and/or suggestions are welcome.
Cheers!
Comment