一个免费的邮件列表源程序(三)

Subscribe.asp
<%@ Language=JavaScript %>

<!--#include file = "include/SetGlobals.asp"-->
<!--#include file = "include/DBPath.asp"-->

<%
// output relevant meta tags
Init( "Subscription" );

// output common top of page
Header( '<a href="work.asp">Work</a> --> Subscription', 3 );

// output page content
Content ( );

// output common bottom of page
Footer( );
%>

<% /* standard page elements */ %>
<!--#include file = "utils/Init.asp"-->
<!--#include file = "utils/Database.asp"-->
<!--#include file = "utils/Header.asp"-->
<!--#include file = "utils/Footer.asp"-->

<%
// ============================================
// the content of this page
// ============================================
function Content ( )
{
   Out ( '<td width="20%"> </td>' );
   Out ( '<td width="60%">' );
    
      // if the form has an email address, validate it first
      // so that if it fails we can show the form to fix
      var sEmail = "";
      var bSubmitted = (Request.Form.Count > 0);

      // has the form been submitted?
      if ( bSubmitted )
      {
         // get the email address from the form...
          sEmail = "" + Request.Form ( "email" );

         // validate the email address and moan if it fails
         if ( !IsValidEmail ( sEmail ) )
         {
            Out ( '<h5><font color="red">"' + sEmail + '" <i>appears</i> to be an invalid email address - please try again!</font></h5>' );
            Out ( '<p><font color="red">If you disagree, please <a href="Contact.asp">contact me</a> directly.</font><p>' );
            // pretend the form hasn'\t been sent yet
            bSubmitted = false;
         }
      }

      // show the form if not submitted yet
      if ( !bSubmitted )
      {
         Out ( 'If you\'re interested in hearing whenever a new article is posted, or an existing one is updated, type in your email address below and hit <b>Subscribe!</b>' );
         Out ( '<p>Whenever you want to stop receiving my emails, guess what? That\'s right, enter your email address and hit <b>Unsubscribe</b>...' );
         Out ( '<p><i>Your email address will never sold to or otherwise used by any third party, just me.</i>' );

         // here's the form tag. the action attribute is the name of
         // the file that will be called with the answer - in this case
         // it's the same page. the method can be "post" to send the
         // form data 'behind the scenes' or "get" to appending the
         // data to the URL in the style page.asp?data1=a&data2=b
         //
         // use post most of the time - it's neater and "get" is limited
         // in the amount of data that can be sent.
         Out ( '<center><form action="Subscribe.asp" method="post">' );
    
            // another table to line up the titles and inputs
            Out ( '<table border="0" cellpadding="0">' );
            Out ( '<tr><td align="right" valign="top">' );
               Out ( 'Email:' );
            Out ( '</td><td align="left" valign="top">' );
               // a simple text box. we'll reference it with the name "name"
               // and show 22 characters on the form. use the maxlength
               // attribute to set the maximum characters they can enter.
               // use value="some text" to pre-fill the input with data.
               //
               // IMPORTANT! using names that are commonly used by
               // other web sites has a big advantage to the user - IE
               // will drop down a list of previous answers, which they
               // can usually pick from rather than type in. Think about this.
               Out ( '<input type="text" name="email" size="31" value="' + sEmail + '"></input>' );
            Out ( '</td></tr>' );

            Out ( '<tr><td align="right" valign="top">' );
               Out ( ' ' );
            Out ( '</td><td align="left" valign="top">' );
               // type='submit" provides a submit button to perform the
               // form action. the button says "Submit" unless you override
               // with the value attribute.
               Out ( '<input type="submit" name="action" value="Subscribe"></input> <input type="submit" name="action" value="Unsubscribe"></input>' );
            Out ( '</td></tr>' );

            Out ( '</table>' );
         Out ( '</form></center>' );
      }
      else
      {
         var sAction = "" + Request.Form ( "action" );

         if ( sAction == "Subscribe" )
            AddEmail ( sEmail ) ;
         else
            RemoveEmail ( sEmail );

         Out ( '<p>' );
      }

      Out ( 'Do you want to see how this form adds and removes addresses to my database? All the source code is just a click away!' );
      Out ( '<p><center><a href="ShowSource.asp? page=Subscribe"><img src="http://edu.cnzz.cn/NewsInfo/images/source.gif" border=0></a></center>' );
      Out ( '<p>In <a href="MailToList.asp">Part 2</a> see how I wrote a form to mail all my subscribers...' );

   Out ( '</td>' );
   Out ( '<td width="20%"> </td>' );
}

// ============================================
// validate email address
// ============================================
function IsValidEmail ( sEmail )
{
   // regular expression courtesy of ed.courtenay@nationwideisp.net
   // I won't even pretend that I've read through this yet!

   if ( sEmail.search ( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0- 9]+)*\.[A-Za-z]{2,5}/ ) != -1 )
      return true;
   else
      return false;
}

// ============================================
// add email to database
// ============================================
function AddEmail ( sEmail )
{       
   // open the connection
   DBInitConnection ( );

   // first see if they are already subscribed
   var sSQL = 'SELECT Email FROM MailingList WHERE Email="' + sEmail + '";';

   DBGetRecords ( sSQL );

   if ( !oRecordSet.EOF )
   {
      Out ( '<h5><font color="red">' + sEmail + ' is already subscribed to my mailing list!</font></h5>' );
      return;
   }

   // this section needs more work - what should be done is that an email is
   // sent to the email address, and only added to the database when we
   // get a reply. that way we know the address is valid and the recipient
   // really wants to join the list. for now though, we'll add to the db now.
   sSQL = 'INSERT INTO MailingList (Email) VALUES ("' + sEmail + '");';

   oConnection.Execute( sSQL );

   // free the connection
   DBReleaseConnection ( );

   Out ( sEmail + ' has been successfully subscribed to my mailing list. ' );
   Out ( '<p>You will now receive an email whenever I write new articles, or if I make an important update to any.' );

   Email ( 'Joined the ShawThing mailing list', sEmail, 'You have successfully subscribed to the mailing list at ShawThing. If you didn\'t request this please reply to this email, or visit http://www.shawthing.com/subscribe.asp to unsubscribe.\n\nThank you.\n\nJames Shaw\nhttp://www.shawthing.com/' );
}

// ============================================
// remove email from database
// ============================================
function RemoveEmail ( sEmail )
{       
   // open the connection
   DBInitConnection ( );

   // first see if they are already subscribed
   var sSQL = 'SELECT Email FROM MailingList WHERE Email="' + sEmail + '";';

   DBGetRecords ( sSQL );

   if ( oRecordSet.EOF )
   {
      Out ( '<h5><font color="red">' + sEmail + ' isn\'t subscribed to my mailing list!</font></h5>' );
      return;
   }

   // delete from the database
   sSQL = 'DELETE FROM MailingList WHERE Email="' + sEmail + '";';

   oConnection.Execute( sSQL );

   // free the connection
   DBReleaseConnection ( );

   Out ( sEmail + ' has been successfully removed from my mailing list. ' );
   Out ( '<p>You have been sent a confirmation email, but after that you will not receive any more emails.' );

   Email ( 'Removal from ShawThing mailing list', sEmail, 'You have been successfully removed from the mailing list at ShawThing. If you didn\'t request this please reply to this email, or visit http://www.shawthing.com/subscribe.asp to re-subscribe.\n\nThank you.\n\nJames Shaw\nhttp://www.shawthing.com/' );
}

// ============================================
// email me!
// ============================================
function Email ( sSubject, sEmail, sMessage )
{   // send an email to the address just to confirm what just happened
   var oMail = Server.CreateObject ( "CDONTS.NewMail" );

   // setup the mail
   oMail.From = 'DB@shawthing.com';

   oMail.To = sEmail;
   oMail.Importance = 1;

   oMail.Subject = sSubject;
   oMail.Body = sMessage;

   // send it
   oMail.Send ( );

   // release object
   oMail = null;
}
%>
     
utils/Database.asp
<%
// globals
var oConnection;
var oRecordSet;
var sConnection;

// ============================================
// example usage:
//      DBInitConnection ( );
//
//      var sSQL = "SELECT * FROM Somewhere";
//
//      DBGetRecords ( sSQL );
//
//      ...use oRecordSet
//
//      DBReleaseRecords ( );      // optional step
//
//      DBReleaseConnection ( );
// ============================================

// ============================================
// initializes database variables for first use on page
// ============================================
function DBInitConnection ( )
{
   // don't open it again if already opened!
   if ( sConnection != undefined )
      return;
       
   // get connection object
   oConnection = Server.CreateObject( 'ADODB.Connection' );

   // get the database connection string
   // use MapPath to make relative path into physical path
   sConnection = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + Server.MapPath ( sDBPath );

   // open the connection
   oConnection.Open( sConnection );

   // as an attempt at optimization we now open
   // the recordset here, not in DBGetRecords()
   oRecordSet = Server.CreateObject ( 'ADODB.Recordset' );
}

// ============================================
// tidies up after DBInitConnection
// ============================================
function DBReleaseConnection ( )
{
   // don't release the connection if not connected!
   if ( sConnection == undefined )
      return;
       
   // as an attempt at optimization we now close
   // the recordset here, not in DBReleaseRecords()
   if ( oRecordSet.State != 0 )
      oRecordSet.Close();
   oRecordSet = undefined;

   oConnection.Close();
   oConnection = undefined;
    
   sConnection = undefined;
}

// ============================================
// executes the passed in SQL statement
// and returns the oRecordSet object
// ============================================
function DBGetRecords ( sSQL )
{
   // remember that this can fail if passed garbage, and hence
   // 'oRecordSet' will already be 'closed'
   oRecordSet = oConnection.Execute( sSQL );
}

// ============================================
// tidies up after DBGetRecords
// ============================================
function DBReleaseRecords ( )
{
   // IMPORTANT: THIS FUNCTION INTENTIONALLY BLANK
   // as an attempt at optimization we now open/close
   // the recordset with the connection, not separately
   // so all code was moved to DBReleaseConnection.
    
   // it is recommended that you still call this function as soon
   // as the recordset is finished with.
    
   // note that it is assumed by the caller that it is legal
   // to call DBReleaseConnection without calling this function
}
%>

时间: 2024-08-31 09:03:35

一个免费的邮件列表源程序(三)的相关文章

一个免费的邮件列表源程序(一)

MailToList.asp<%@ Language=JavaScript %> <!--#include file = "include/SetGlobals.asp"--> <!--#include file = "include/DBPath.asp"--> <% // output relevant meta tags Init( "Mail to list" ); // output commo

一个免费的邮件列表源程序(二)

ShowSource.asp<%@ Language=JavaScript %> <!--#include file = "include/SetGlobals.asp"--> <% // get the page to display from the URL var sPage = "" + Request.QueryString ( "page" ); // make sure it's a page we a

Java Mail API及其应用 —— 一个邮件列表服务器的实现 (二)(转贴)

服务器|邮件列表 邮件列表服务经常用于为工作组提供基于email的讨论环境,订阅者通过它讨论共同感兴趣的问题. 本文提供的示例程序ListServer是一个简单的邮件列表转发服务器:它从指定帐号读取新邮件并发送给所有订阅者.Java Mail API不仅使得实现其基本功能相当简单(使用缺省的POP3和SMTP),而且保证了程序易于支持任何可能遇到的系统环境. 运行该程序需要包含以下支持文件:Java Mail (mail.jar).JAF(activation.jar)以及缺省的POP3支持(p

用ASP.NET设计高效邮件列表

asp.net|设计|邮件列表 一个吸引人的.功能完备的网站往往具有以下特征:一是内容充实.实用,吸引对该类内容干兴趣的浏览者:二是页面设置合理,页面设计精美:三是网站互动性强,用户和网站或者用户与用户之间交流方便高效:四是网站内容更新及时并且可以第一时间通知用户.以上这些特点可能对于一些大型的门户网站而言,不是特别适合,因为门户网站的特点,用户流量往往不是内容的区别而是用户习惯.历史原因和地域区别(比如广东地区的浏览者喜欢网易.北京的喜欢新浪).而对于一般个人网站或者企业网站而言,以上特点就显

Java Mail API及其应用 —— 一个邮件列表服务器的实现 (三)(转贴)

服务器|邮件列表 相关资源1.Java Mail API软件包下载(版本1.1.2) ftp://usmt.java.sun.com/pub/javamail/tyo39/javamail1_1_2.zip 缺省POP3实现软件包下载: ftp://usmt.java.sun.com/pub/javamial/tyo39/pop31_1.zip 以上文件也可以从Sun的Java Mail API主页下载 http://java.sun.com/products/javamail/ 2.JavaB

ASP环境下邮件列表功能的实现 (三)

在访问管理页面之前必须经过身份验证.本实现中我们用图3所示的secure.htm页面供管理员输入身份识别码,若用户输入值非空则用Cookies来保存它.执行管理任务的页面是admin.asp,每当用户试图访问这个页面,下面的代码将检查这个Cookies与用户身份识别码(这里是123456)是否匹配,如匹配失败则将该用户重定向到输入身份识别码的secure.htm页面. < % strPW1 = Request.Form("txtPW") if strPW1 < > &

ASP环境下邮件列表功能的实现 (三)(推荐)

邮件列表 在访问管理页面之前必须经过身份验证.本实现中我们用图3所示的secure.htm页面供管理员输入身份识别码,若用户输入值非空则用Cookies来保存它.执行管理任务的页面是admin.asp,每当用户试图访问这个页面,下面的代码将检查这个Cookies与用户身份识别码(这里是123456)是否匹配,如匹配失败则将该用户重定向到输入身份识别码的secure.htm页面. [图3 ASPMailingList_3.gif] < %    strPW1 = Request.Form("

Java Mail API及其应用 —— 一个邮件列表服务器的实现 (四)(转贴)

服务器|邮件列表 附录:ListServer.java/*** 类ListServer提供基本的邮件列表服务功能:读取指定邮件帐号的所有新邮件,然后转发给* 在emailListFile文件中指定的所有邮件帐号(订阅者).emailListFile中的邮件帐号格式* 为每一行一个邮件帐号.*/import java.util.*;import java.io.*; import javax.mail.*;import javax.mail.internet.*;import javax.acti

Java Mail API及其应用 —— 一个邮件列表服务器的实现 (一)(转贴)

服务器|邮件列表 Java Mail API 是Sun开发的最新标准扩展API之一,它给Java应用程序开发者提供了独立于平台和协议的邮件/通讯解决方案.本文介绍该API的核心机制,并通过一个邮件转发服务器(邮件列表服务器)演示其具体用法. Java Mail API的开发是Sun为Java开发者提供公用API框架的持续努力的良好例证.提倡公用框架,反对受限于供应商的解决方案,充分预示着一个日益开放的开发环境的建立. 在email通讯领域,面向最终应用的开发者(以及用户)已经能够购买到最适合他们