Click here to Skip to main content
Click here to Skip to main content

Complete ASP / SQL Server / SMTP messaging system

By , 15 Nov 2003
 

sample.jpg

Introduction

Never give out your email on the web! This solution eliminates the risk of SPAM, viruses and personal email exposure. By using a database to receive the message and SMTP to send a response, you can manage webmaster or other types of email without fear. This system uses a simple HTML form that posts to an ASP page that saves the information into a SQL server database. The messages are then managed using a familiar email like interface and the replies and forwards are sent as native SMTP email.

Features:

  • Personal Folders
  • Address Book
  • Actual Email responses
  • Saved Sent Items
  • Column Sorting
  • Simple Integration into existing websites.
  • Works from a simple HTML form
  • No ActiveX or client downloads.

      Background

      The idea was to reduce the number of bad emails that were coming into our webmaster email but we wanted to send an actual email response. This system was what I came up with however, once I got started, I couldn't stop. I added all the normal email features (except for attachments) like replies, forwards and personal folders. I also added an address book so you could store contacts and then use them to send the replies and forwards to multiple people.

      Since the address that the SMTP mail is sent from doesn't exist, so there isn't any risk of people responding to it.

      Since I have a content management system that uses a domain_id identifier, I wanted to be able to offer the Webmaster Email program to many of my clients without having to create a database for each one of them, so I added the domain_id identifier to allow many domains to use it without cross contamination. You will see that almost all functions will filter by this identifier.

      The database is SQL Server. I tried to use an access database but I had trouble getting Access to support multiple recordsets in a single SQL call. Instead of describing the database in detail, I have included a script that will build the SQL database. The data_access.asp contains the username and password that is written into the SQL script. These can be changed as long as they are the same in both places. I use a separate config file when I integrate these. For the demo, I simply hard coded them.

      This application is full of some fairly complex code so I hope everyone enjoys perusing it. A lot of the functions and modules can be used in more applications than this one. If you need a native SMTP email creator, I am including mine with this app.

      Using the code

      Install

      The code simply needs to be copied to any ASP virtual directory. No global.asa is needed. A database script is provided and just simply needs to be ran on the SQL Server with create database privileges.

      Configure

      In the install folder include/data_access.asp, simply replace the settings with your server info and you're all set. In my applications, this is an application variable. You may handle it however you wish.

      Run

      The application has two parts:

      1. The submit message page that you put out on the public side and an ASP to process the posted data.
        • post.htm
        • update_message.asp
      2. The Webmaster email application that you use internally to see and respond to the messages.

      I have provided a page (default.htm) so you can get to both for demo purposes. It might be a good idea if these are on the same server to secure the webmaster email directory with either a login screen or Windows authentication.

      SMTP

      The SMTP email works by simply placing a structured text file in the mailroot of the server. SMTP needs to be configured on the server for the files to be picked up and actually sent. The code will create the email in a temp directory first and then move the item over. This eliminates the problem of SMTP trying to pick the file up before we are done writing it. I put in some naming functions to ensure uniqueness in a multi-user environment. To configure the paths to these two directories, open mod_main.asp in the include directory and change the path names as needed.

      Database

      The database is fairly simple as far as table structures go, but there are some triggers to do cascading deletes and some that manage the read items and notification functions. I had a working copy at one time that sent internal mail with this system and it supported notifications when a person read an item. It also supported priority messages. The database still has fields for these items but I have since removed the code. A clever person could hook this feature back up. Let me know if I can provide any assistance and/or provide some of my old code to let you submit messages internally. Really the only difference is, the internal system had a list of users and the messages were tied to the unique id of those people. Therefore a login was required in order to identify the user and allow each person to read their own mail. There was a series of flags when a sender or a recipient read or deleted an item. I stored settings for each person and allowed them to move the item around separately. To each user, it looked like they were moving their own copy of the message but it was really the same data record being updated.

      The trigger to delete the record once both the recipient and the sender deleted the item from their mailboxes is still in the database.

      Tables

      1. messages
        [id] [int] IDENTITY (1, 1) NOT NULL ,
        [msg_text] [varchar] (8000) ,
        [user_id] [varchar] (100),
        [last_modified] [datetime] NULL ,
        [active_flg_sender] [int] NULL ,
        [msg_subject] [varchar] (500) NULL ,
        [notify_req] [int] NULL ,
        [was_delivered] [int] NULL ,
        [priority] [int] NULL ,
        [msg_recip] [varchar] (1000) NULL ,
        [folder_id] [int] NULL ,
        [domain_id] [int] NULL ,
        [parent] [int] NULL
      2. msg_folders
        [id] [int] IDENTITY (1, 1) NOT NULL ,
        [folder_name] [varchar] (100) NOT NULL ,
        [domain_id] [int] NOT NULL
      3. msg_recip
        [id] [int] IDENTITY (1, 1) NOT NULL ,
        [recip] [varchar] (50) NULL ,
        [msg_id] [int] NULL ,
        [active_flg_recip] [int] NULL ,
        [msg_read] [int] NULL ,
        [reply_id] [int] NULL ,
        [foward_id] [int] NULL ,
        [read_time] [datetime] NULL ,
        [folder_id] [int] NULL ,
        [reply_date] [datetime] NULL
      4. user_addresses
        [id] [int] IDENTITY (1, 1) NOT NULL ,
        [first_name] [varchar] (50) NULL ,
        [last_name] [varchar] (50) NULL ,
        [email] [varchar] (150) NULL ,
        [domain_id] [int] NULL 

      Code

      I have put comments in all the files so they are hopefully self explanatory but just in case, here is a quick summary of the files in the system. I am not including the HTML files in this list simply because there isn't any real code in them. Besides, there is only two: default.htm, and post.htm.

      1. addressbook.asp – This is the main window for the address book. It has an IFRAME for the actual listings.
      2. addresses_inc.asp – This file is inside an IFRAME in the addressbook.asp window. I only do this because I needed a separation so I could have scroll bars.
      3. contactdetail.asp - This file is the input form to add a contact. For this system, I only store the first name, last name and email. You certainly could store whatever you wanted.
      4. inframe.asp – This is the same as default.asp. It can be included in a page or be a main page.
      5. messagedetail.asp – This is the actual message window. It displays just like a regular email window with options to reply and forward the message.
      6. sendmessage.asp – This is the window that pops up if you click reply or forward on a message. It has a button to access the address book for additional addresses. It automatically fills out the “TO” field, the subject (with the classic RE and FW prefaces in place) and the original message.
      7. update_message.asp – This file is used by the public HTML form to post a new message into the system.
      8. updatecontact.asp – This file is the backend code to add/edit and delete a contact.
      9. updatefolder.asp – This file is the backend code to add/edit and delete a personal folder.
      10. updatemessage.asp – This file is the backend code to perform all operations on a message such as reply, forward, move, delete and mark as read. When a message is replied to or forwarded, this file calls the make_email.asp to generate the email text file. For the demo, this has been turned off.
      11. viewmessages.asp – This file is basically the inbox listing. It is included by inframe.asp. It shows all new postings.
      12. viewreadmessages.asp – This file shows all listings that have been read but have not been moved to another personal folder. It is included by inframe.asp.
      13. viewsentmessage.asp – This shows all sent emails. It is included by inframe.asp.
      14. viewpersonalfolder.asp – This is a general personal folder window. It is the same file used by all the personal folders. It shows any message that was moved to a folder regardless if it was read or not. If it was not read, an icon appears next to the unread message.
      15. CDIwm.js – This is a global JavaScript functions module. It is included in almost every file.
      16. mod_main.asp - This is a global include in every ASP page. It is where I put all superfluous functions. It includes the data_access.asp which is the global data connection module. For this application, mod_main is empty but I include it since it is one of my standard architecture components.
      17. data_access.asp - This is a module that I use in all my applications. It contains all the necessary functions to connect easily to a database using ADO. I use it in VB, ASP and even access.
      18. make_email.asp – This is the general-use native SMTP email creation module. It handles all the creating, naming and moving of the file based on the paths configured in mod_main.asp.
    • License

      This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

      A list of licenses authors might use can be found here

      About the Author

      cdidave
      Software Developer (Senior)
      United States United States
      Member
      No Biography provided

      Sign Up to vote   Poor Excellent
      Add a reason or comment to your vote: x
      Votes of 3 or less require a comment

      Comments and Discussions

       
      You must Sign In to use this message board.
      Search this forum  
          Spacing  Noise  Layout  Per page   
      Generalhimemberyeheyes9 Nov '08 - 20:13 
      i just could not connect it to ms sql 2000..
      i always got a message that says login failed to user CDIwmuser
      and of course i tried to change some seting in data_asses.asp file
      like the server name ...(i use the server name of the SQL server)
      so am i right or not...
      here is what i used
      'strServer="SQLCDI\CDI"
      strServer="(local)" .......the name of SQl server
      strUser="CDIWMUser"
      strDatabase="CDIWM"
      strPWD="user"
      but it is not working what shall i do plus i am using IIS so is this wrong
      it usually work for me if the database is Access .mdb
      help!
      GeneralRe: himembercdidave10 Nov '08 - 3:50 
      it looks right, as long as you have granted the user dbo rights to the database, you should be good.
       
      CDIDave

      GeneralRead MsgmemberMember #35693398 Mar '07 - 1:16 
      [id] [int] IDENTITY (1, 1) NOT NULL ,
      [recip] [varchar] (50) NULL ,
      [msg_id] [int] NULL ,
      [active_flg_recip] [int] NULL ,
      [msg_read] [int] NULL How to fill this field any thechnique,
      [reply_id] [int] NULL ,
      [foward_id] [int] NULL ,
      [read_time] [datetime] NULL ,
      [folder_id] [int] NULL ,
      [reply_date] [datetime] NULL

       
      Janifer
      GeneralmemberMember #35693398 Mar '07 - 1:15 
      [id] [int] IDENTITY (1, 1) NOT NULL ,
      [recip] [varchar] (50) NULL ,
      [msg_id] [int] NULL ,
      [active_flg_recip] [int] NULL ,
      [msg_read] [int] NULL How to fill this field any thechnique,
      [reply_id] [int] NULL ,
      [foward_id] [int] NULL ,
      [read_time] [datetime] NULL ,
      [folder_id] [int] NULL ,
      [reply_date] [datetime] NULL

      GeneralRead MessagememberMember #35693398 Mar '07 - 1:09 
      [id] [int] IDENTITY (1, 1) NOT NULL ,
      [recip] [varchar] (50) NULL ,
      [msg_id] [int] NULL ,
      [active_flg_recip] [int] NULL ,
      [msg_read] [int] NULL How to fill this field.
      [reply_id] [int] NULL ,
      [foward_id] [int] NULL ,
      [read_time] [datetime] NULL ,
      [folder_id] [int] NULL ,
      [reply_date] [datetime] NULL

      Generalsendmail.aspmemberjanning19 Jun '06 - 8:19 
      I just want a simple mail sender as below:,
       
      I am using the three files below to be able to "send mail" .

      When I go to http://www.aanning.com/input.htm and put in the e-mails and msg I get":

      Error Type:
      Active Server Pages, ASP 0134 (0x80004005)
      The object has an invalid ProgID of 'MSWC.MyInfo'.
      //global.asa, line 1
       


      I know I'm REAL close.......and this USED to work!! please advise.

      Thanks much
      James.

      ---------------------------------------


      files used below global.asa, input.htm and sendthemail1.asp




      GLOBAL.ASA file:--------------------------------

      <OBJECT runat="server" SCOPE=Session ID=MyInfo PROGID="MSWC.MyInfo">
      </OBJECT>
       

      INPUT.HTM file:---------------------------------

      <html>
      <head><title>Mail Input Page</title></head>
      <body>
      <form method="post" action="sendthemail1.asp" name="input">

      From  <input type="text" name="From" size="20">
      To<input type="text" name="to" size="20">
      Subject<input type="text" name="subject" size="20">
      Body<input type="text" name="body" size="220">
      <input type="submit" value="Send" name="B1"><input type="reset"value="Reset" name="B2">  
      </form>
      </body>
      </html>

       
      SENDTHEMAIL1.ASP file:-------------------------------

      <%
      Response.Buffer = True
      Dim strFrom
      Dim strTo
      Dim strSubject
      Dim strBody
      Dim objCDOMail

      strFrom = Request.Form("From")
      strTo = Request.Form("to")
      strSubject = Request.Form("subject")
      strBody = Request.Form("body")
      Set objCDOMail = Server.CreateObject("CDONTS.NewMail")

      objCDOMail.From = strFrom
      objCDOMail.To = strTo
      objCDOMail.Subject = strSubject
      objCDOMail.Body = strBody
      objCDOMail.Importance = 1
      objCDOMail.Send

      Set objCDOMail = Nothing

      %>
      <html>
      <head><title>Sent Mail</title></head>
      <body>
      Your mail was sent to:<% = request("to") %>

      The time that is was sent was: <% = Now %>
      </body>
      </html>


      GeneralRe: sendmail.aspmemberjanning19 Jun '06 - 8:20 
      Please copy your replies to Janning197@aol.com
      Questionhow to create web sms using asp codememberfadli jaafar1 Apr '06 - 17:03 
      i just want to know how to create web sms using asp code and do you have any demo or example for this solution?
       
      -bOb-
      QuestionCan you Explain the databases StructurememberIan Perera22 Oct '05 - 5:34 
      HEY CDIDAVE
       
      could u give me a better explaination of your database Structure.
       
      thanks

       
      I@n
      QuestionInternal faux-email systemmemberdquillan10 Sep '05 - 23:36 
      I need to develop a web-based message system that application users can utilize to send and respond to messages back and forth to each other. I love the example that you provided here and was hoping that you could assist me in providing a solution. It would not use SMTP or CDNTS but instead use a SQL Server database and nothing else. It would act like and look like email but would only be a messaging system. Users can message each other back and forth basically and thats it.
       
      I have some something already but it is not very realistic. I'd like to take what you have demonstrated just stripped of the SMTP stuff.
       
      Can you help?
       
      Thanks!
      Don
       
      dquillan@hotmail.com
      GeneralReal Working Example please.....memberUCFirefly2 Sep '05 - 2:58 
      This is not a place to sell your code. Go somewhere else if you want to do that.
       

      GeneralErrorsussAli Martin22 Feb '05 - 14:06 
      Hi,
       
      When I open up to view a message I keep getting the following error....
       
      Microsoft OLE DB Provider for ODBC Driver error '80040e07'
       
      [Microsot][ODBC SQL Server Driver][SQL Server]Syntax error converting datetime from character string.
       
      email/include/data_access.asp, line 23
       
      Any help on this would be great.
       
      Cheers

      GeneralErrormemberAli Martin22 Feb '05 - 14:06 
      Hi,
       
      When I open up to view a message I keep getting the following error....
       
      Microsoft OLE DB Provider for ODBC Driver error '80040e07'
       
      [Microsot][ODBC SQL Server Driver][SQL Server]Syntax error converting datetime from character string.
       
      email/include/data_access.asp, line 23
       
      Any help on this would be great.
       
      Cheers

      GeneralUpgrade to the ASP.NETmemberAndrzej Budny12 Dec '04 - 22:16 
      Hi,
      i'am reading Your article. It's fine. I want upgrade this project to the ASP.NET. Any suggestion/ideas welcome.
      Andrew.
      GeneralRe: Upgrade to the ASP.NETmemberMember 206127022 Jul '08 - 23:36 
      HI I am also looking for ASP.NET solution of this project, any ideas?
      Generalwhy the page cannot be displayedmemberarena2118 May '04 - 19:04 
      hello,just now, i 've finished download sourcecode about complete ASP/SQL SERVER/SMTP MESSAGE SYSTEM. and i try to build a database like the readme file n connect to ODBC..as i know the database name is CDIwm right and it has 4 table..but when i try run at my localhost such as http://localhost/CDIwm/
      and then the page be like this:
       
      To send webmaster an email, click "post a message"
      To read messages that have been posted, click the refresh button.
      Webmaster Email Post a message | Documentation | Download Source
       
      The page cannot be displayed
      There is a problem with the page you are trying to reach and it cannot be displayed.
       
      Please try the following:
       
      Click the Refresh button, or try again later.
       
      Open the localhost home page, and then look for links to the information you want.
      HTTP 500.100 - Internal Server Error - ASP error
      Internet Information Services
       
      Technical Information (for support personnel)
       
      Error Type:
      ADODB.Recordset (0x800A0CC1)
      Item cannot be found in the collection corresponding to the requested name or ordinal.
      /CDIwm/inframe.asp, line 244

      D'Oh! | :doh:
      so,hope u can help me to solve this problem...bcoz i'm just beginner in ASP scripts..Cry | :((
       
      don't waste ur time
      QuestionHow about an example with CDOSYSmemberMattcj20 Nov '03 - 1:34 
      Do you have an example using CDOSYS as this example will not run on Windows XP since XP no longer supports CDONTS?
      Confused | :confused:
      AnswerRe: How about an example with CDOSYSmembercdidave20 Nov '03 - 4:43 
      This application does not use CDONTS, it is native SMTP. It creates a text file using SMTP file format and simply places it into the SMTP mail folder on the server. If you have sMTP enabled, it will pick up the file and send it as email...
       
      CDIDave
      GeneralRe: How about an example with CDOSYSmemberMattcj20 Nov '03 - 4:51 
      Perhaps I am mistaken as I'm still very new to this stuff... I was looking at the SendMail.asp file. It creates a CDNTS.NewMail object and sends it further down in the file.
       
      Confused | :confused:
      GeneralRe: How about an example with CDOSYSmembercdidave20 Nov '03 - 5:03 
      I am not using a file called sendmail.asp. Look at the make_email.asp...I do not create a cdonts object..I use the filesystem object
       
      CDIDave

      General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

      Permalink | Advertise | Privacy | Mobile
      Web02 | 2.6.130523.1 | Last Updated 16 Nov 2003
      Article Copyright 2003 by cdidave
      Everything else Copyright © CodeProject, 1999-2013
      Terms of Use
      Layout: fixed | fluid