用Delphi开发简单的WebMail程序

来源:网络时间:2011-08-05 17:05:46

  WebMail是指在网页中实现邮件的发送。使用Delphi开发Web Server程序是非常简单的,Delphi中提供了大量的元件和对象。下面通过一个例子来介绍如何利用Delphi开发一个响应用户输入的ISAPI的WebMail程序。为了简单,程序没有对传送的数据提供保密。

  首先,在Web服务器端安装数据库引擎dbe,并设置好数据库别名:yh,指向一个包含用户名和用户密码的数据库文件user.db。接着建立两个HTML文件,名字分别为:dl.html,qd.html,放在Web服务器的缺省目录下(如:c:\inetpub\wwwroot)。

  dl.html的内容如下:

  

  <html>

              <head><title>发送邮件系统</title></head>

              <body>

              <h1>发送邮件系统</h1>

              <p>请输入您的用户名及密码</p>

              <form method=”post”action="/scripts/SendMail">

              <p>用户名<input type="text" length=10 name="username">

              密码:< input type="password" length=10 name="password" ></p>

              <p><input type="submit" value="确定">

              <input type="reset" value="清除"></p>

              </form>

              </body>

              </html>

  qd.html文件内容如下:

  

  <html><head><title>填表</title></head>

              <body>

              <form method=”post”action="feedback">

              <p>请填入接收邮件地址:toaddress:

              <input type=”text”length=20 name=”toaddress”></p>

              <p>请填入主题<input type="text" length=20 name="subject"></p>

              <p>内容:</p>

              <p><input type=“textarea”length=40 width

              =40 name=”body”></p>

              <p><input type="submit" value="确定">

              <input type="reset" value="清除"></p>

              </form >

              </body >

              </html >

  在Delphi中新建一个基于ISAPI的Web Server Application,手动增加nmsmtp1,query1,pageproducer1。其中:pageproducer1的htmlfile属性为c:\inetpub\www.root\qd.html。nmsmtp1的host(发送邮件服务器的地址)在这里为.,port:25。全局变量为:sername:string;flag:boolean。

  增加一个路径为feedback的动作项,其代码如下:

  

   Var

               Count:integer;

              S:string;

               Begin

              Query1.close;

              Query1.sql.clear;

              S:=’select count(username) from user.dbswheresusername=”’;

              S:=s+request.contentfields.values[‘username’]+’”’;

              S:=s+’and password=”’;

              S:=s+request.contentfields.values[‘psword’]+’”’;

              Query1.sql.add(S);

              Query1.open;

              If query1.count=0 then response.content:=

              ’<html><head><title>

              </title>

              <body>用户名、密码不正确,请重新输入</body>

              </html>’

              Else

              Username:=request.contentfields.values[‘username’];

              Response.content:=pageproducer1.content;

              End;

  再增加一个路径为Sendmail的动作项,它的程序代码如下:

  

  Var body:string;

              Begin

              Flag:=true;

              body:=request.contentfields.values[‘body’];

              Pageproducer1.htmldoc.clear;

              Pageproducer1.htmldoc.add(‘< html >< body >’);

              Nmsmtp1.postmessage.clear;

              Nmsmtp1.postmessage.fromaddress:=username+’@netease.com’;

              Nmsmtp1.postmessage.from:=username;

              Nmsmtp1.postmessage.body.add(body);

              Nmsmtp1.postmessage.toaddress.add(request.contentfields.values[‘toaddress’]);

              Nmsmtp1.postmessage.subject:=request.contentfields.values[‘subject’];

              Nmsmtp1.connect;

              If flag=true then

              begin

              Nmsmtp1.sendmail;

              nmsmtp1.disconntent;

              end

              pageproducer1.htmldoc.add(‘</body></html>’);

              response.content:=pageproducer1.content;

              end;

  增加nmsmtp1的OnConnect事件添加如下代码:

  

  pageproducer1.htmldoc.add('<p>已经和发送邮件服务器连接</p>');

              在NMSMTP1的Connection事件添加如下代码:

              flag:=false;

              pageproducer1.htmldoc.add('<p>连接失败</P>');

  

  将project存为sendmail.dpr,编译后放到Web服务器的可执行文件路径下(如:c:\intpub\scripts),即可响应HTML文件dl.htm的用户输入,并且如果用户的用户名及密码正确,则可进入发送邮件的页面。用户填写接受邮件地址及主题、内容后,即可发送邮件。此程序在NT Server上调试通过。

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:rjfawu@163.com