|
|
|||||||
| Windows Web Hosting Discussion Forum Discussion Forum on Windows Web Hosting Related Support Issues. Discuss your Issues related to ASP.NET, MS SQL, ASP, DLL, Components, etc... |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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
%>
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... <% ----------------- ----------------- ----------------- %> 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)
%>
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 |
|
|||
|
I have a database with emails.
For these, I am curently sending mails manually. In other words, I send it to myself and then on the bcc I put the address of those I'm sending the emails. Since there are so many, I have to break it down in 3 or 4 times. I want to be able to get them all in a database( that would be easy) and then from the database send emails automatically to each one. My database should be on the hosting service machine. How do I do this? |
|
|||
|
Quote:
Sorry for the delay in reply. Below are the rough guidelines to accomplish what you want, which I believe are very straightforward: Note: You will need some coding experience with ASP. 1) Create an access database. If you are hosting on AccuWebHosting servers then you know that it's easy to create one through your control panel and best of all, it's free. 2) Next, create a table that stores the information, for example the names and email addresses. 3) Next, write an ASP page that does the below: a. Connects to the database. b. Has a loop that iterates through the Recordset (Table) that has the Name and email addresses from the beginning of the Recordset to the end. c. Finally, simply call the mailing function that I have written to send mails in each iteration of your loop.
__________________
Vaughn Team Leader // Tech. Support AccuWebHosting.Com email:support@accuwebhosting.com Blog: blogsandbloggers.com/Vaughn http://www.accuwebhosting.biz/Vaughn...aughn_Sign.jpg |
|
|||
|
Vijay,
What you can do is create an ASP page that sends the emails and then request us to schedule the asp page at time intervals which you need. After this is done, the system will automatically call the ASP page at the scheduled times, resulting in the code being executed in the page. Thanks,
__________________
Vaughn Team Leader // Tech. Support AccuWebHosting.Com email:support@accuwebhosting.com Blog: blogsandbloggers.com/Vaughn http://www.accuwebhosting.biz/Vaughn...aughn_Sign.jpg |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to configure SMTP for my domain | eluceon | Windows Web Hosting Discussion Forum | 1 | 07-28-2006 06:24 PM |
| Prevent the IIS SMTP Virtual Server from Relaying E-mail Messages | Manuel_ | Windows Dedicated Servers | 0 | 06-22-2006 04:36 PM |
| Fraudent Emails sent under our address | Cynthia E | Recent Issues & Updates | 1 | 11-12-2005 12:46 AM |
| How to use the SMTP server | anand | Windows Web Hosting Discussion Forum | 1 | 07-03-2005 06:46 PM |