Delphi4.0 开发WEBMAIL程序

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
通常,实现WEBMAIL采用mailto.exe的CGI、在HTML文件中写入“< form action="mailto:电子邮箱地址" method=post >”语句或者调用WINDOWS API函数。采用WINDOWS API 和在HTML文件中写入“< form action="mailto:电子邮箱地址" method=post >”语句都要求用户的浏览器装入EXCHANGE、OUTLOOKEXPRESS、或OUTLOOK等软件,而且还有一些浏览器不支持MAILTO语句。而采用CGI的形式实现WEBMAIL对用户的浏览器没有要求,但效率不高。CGI技术正在逐渐被ISAPI/NSAPI技术所取代。本文就来讨论一下采用ISAPI技术实现WEBMAIL。 ---- 使用Delphi 4开发Web Server程序是非常简单的,Delphi 4中提供了大量的元件和对象,支持Web Server程序的开发。 下面通过一个例子来介绍如何利用DELPHI开发一个响应用户输入的ISAPI的WEBMAIL程序。只有在发送服务器上注册的用户才能通过在浏览器发送邮件。为了简单,程序没有对传送的数据提供保密。
---- 首先,在WEB服务器端安装数据库引擎dbe,并设置好数据库别名:yh,指向一个包含用户名和用户密码的数据库文件user.db。接着建立两个HTML文件,名字分别为:dl.html,qd.html,放在WEB服务器的缺省目录下(如:C:)。
dl.html的内容如下:
< html >
< head >< title > 发送邮件系统< /title >< /head >
< body >
< h1 >发送邮件系统< /h1 >
< p > 请输入您的用户名及密码。< /p >
< form method=”post” action="/scripts/xsmd" >
< 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的property: htmlfile:c:.root.html。nmsmtp1的 property:host(发送邮件服务器的地址。)在这里为smtp.netease.com.。port:25。 全局变量为: sername:string;flag:boolean;
增加一个路径为/feedback的动作项,其代码如下:
procedure TWebModule1.WebModule1WebActionItem1
Action(Sender: TObject;
Request: TWebRequest; Response:
TWebResponse; var Handled: Boolean);
Var Count:integer;
S:string;
Begin
Query1.close;
Query1.sql.clear;
S:=’select count(username) from
user.db where username=”’;
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 的动作项,
它的程序代码如下:
procedure TWebModule1.WebModule1Web
ActionItem2Action(Sender: TObject;
Request: TWebRequest; Response:
TWebResponse; var Handled: Boolean);
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的事件如下:
procedure TWebModule1.NMSMTP1Connect(Sender: TObject);
begin
pageproducer1.htmldoc.add
('< p >已经和发送邮件服务器连接< /p >');
end;
procedure TWebModule1.NMSMTP1Connection
Failed(Sender: TObject);
begin
flag:=false;
pageproducer1.htmldoc.add
('< p >连接失败< /P >');
end;
procedure TWebModule1.NMSMTP1ConnectionRequired
(var Handled: Boolean);
begin
pageproducer1.htmldoc.add('< p >要求进行连接< /p >');
end;
 
procedure TWebModule1.NMSMTP1Failure(Sender: TObject);
begin
pageproducer1.htmldoc.add('< p >发送邮件失败< /p >');
flag:=false;
end;
procedure TWebModule1.NMSMTP1Header
Incomplete(var handled: Boolean;
hiType: Integer);
begin
pageproducer1.htmldoc.add('< p >head不完整< /p >');
flag:=false;
end;
 
procedure TWebModule1.NMSMTP1InvalidHost
(var Handled: Boolean);
begin
pageproducer1.htmldoc.add('< p >
发送邮件服务器地址无效< /p >');
flag:=false;
end;
 
procedure TWebModule1.NMSMTP1RecipientNot
Found(Recipient: String);
begin
pageproducer1.htmldoc.add
('< p >接受邮件地址不正确< /p >');
flag:=false;
end;
 
procedure TWebModule1.NMSMTP1Success(
Sender: TObject);
begin
pageproducer1.htmldoc.add('< p >
成功发送邮件< /p >');
end;
 
---- 将project存为sendmail.dpr,编译后放到WEB服务器的可执行文件路径下(如:c:),即可响应HTML文件dl.htm的用户输入,并且如果用户的用户名及密码正确则可进入发送邮件的页面,用户填写接受邮件地址及主题、内容后即可发送邮件。此程序在NT SERVER上调试通过。
 

Similar threads

顶部