Announcement

Collapse
No announcement yet.

How to send Emails via ASP using SMTP Auth

Collapse

Ads on showthread pages

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to send Emails via ASP using SMTP Auth

    WordPress Hosting on SSD Servers
    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)))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
    %>
    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
    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)
    %>
    That's it! Happy mailing.

    Your comments and/or suggestions are welcome.

    Cheers!
    Last edited by Marc_AccuWebHosting; 05-14-2016, 10:33 AM.

  • #2
    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&#39;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?

    Comment


    • #3
      Originally posted by edgardg@Sep 12 2005, 08:59 PM
      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&#39;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?
      Hi Edgardg,

      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&#39;s easy to create one through your control panel and best of all, it&#39;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.

      Comment


      • #4
        ASP.Net Hosting starts @ $3.18/Mo.
        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,

        Comment

        Working...
        X
        Sign up for a News Letter Click here to sign up