Usually, the CGI of mailto.exe is used to implement WEBMAIL, writing the "< form action="mailto:email address" method=post >" statement in the HTML file or calling the WINDOWS API function. Using the WINDOWS API and writing the "< form action="mailto:email address" method=post >" statement in the HTML file both require the user's browser to load software such as EXCHANGE, OutlookEXPRESS, or OUTLOOK, and there are also some browsing The server does not support the MAILTO statement. The use of CGI to implement WEBMAIL has no requirements on the user's browser, but it is not efficient. CGI technology is gradually being replaced by ISAPI/NSAPI technology. This article will discuss the implementation of WEBMAIL using ISAPI technology.
It is very simple to develop Web Server programs using Delphi 4. Delphi 4 provides a large number of components and objects to support the development of Web Server programs. The following is an example of how to use DELPHI to develop an ISAPI WEBMAIL program that responds to user input. Only users registered on the sending server can send emails through the browser. For the sake of simplicity, the program does not provide confidentiality for the transmitted data.
First, install the database engine dbe on the WEB server, and set the database alias: yh, pointing to a database file user.db containing the user name and user password. Then create two HTML files, named: dl.html, qd.html respectively, and place them in the default directory of the WEB server (for example: C: INETPUBWWWROOT).
The content of dl.html is as follows:
<html>
< head >< title >Sending Email System</ /title >< /head >
<body>
<h1>Sending email system</h1>
< p > Please enter your username and password. < /p >
< form method="post" action="/scripts/xsmd" >
< p >Username: < input type="text" length=10
name="username" >
Password: < input type="passWord"
length=10 name="password" >< /p >
< p >< input type="submit" value="OK" >
< input type="reset" value="Clear" >< /p >
< /form>
< /body >
< /html >
The contents of the qd.html file are as follows:
< html >< head >< title >Fill in the form</ /title >< /head >
<body>
< form method="post" action="feedback" >
< p >Please enter the receiving email address:toaddress:
< input type="text" length=20
name=”toaddress” >< /p >
< p >Please fill in the subject. < input type="text"
length=20 name="subject" >< /p >
<p>Content:</p>
< p >< input type="textarea" length=40
width=40 name=”body” >< /p >
< p >< input type="submit" value="OK" >
< input type="reset" value="Clear" >< /p >
< /form >
< /body >
< /html >
Create a new ISAPI-based WEB SERVER application in DELPHI and manually add nmsmtp1, query1, and pageproducer1.
Among them: property of pageproducer1: htmlfile: c:inetpubwww.rootqd.html. The property:host of nmsmtp1 (the address of the sending mail server.) is smtp.netease.com. here. port:25. The global variables are: sername:string;flag:boolean;
Add an action item with the path /feedback, the code is as follows:
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 >The username and password are incorrect, please re-enter them.
< /body >< /html >'
Else
Username:=request.contentfields.values['username'];
Response.content:=pageproducer1.content;
End;
Add another action item with the path /sendmail.
Its program code is as follows:
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;
The events that increase nmsmtp1 are as follows:
procedure TWebModule1.NMSMTP1Connect(Sender: TObject);
begin
pageproducer1.htmldoc.add
('< p >Already connected to the outgoing mail server</p>');
end;
procedure TWebModule1.NMSMTP1Connection
Failed(Sender: TObject);
begin
flag:=false;
pageproducer1.htmldoc.add
('< p >Connection failed< /P >');
end;
procedure TWebModule1.NMSMTP1ConnectionRequired
(var Handled: Boolean);
begin
pageproducer1.htmldoc.add('< p >Requires connection</p>');
end;
procedure TWebModule1.NMSMTP1Failure(Sender: TObject);
begin
pageproducer1.htmldoc.add('< p >Failed to send email</p>');
flag:=false;
end;
procedure TWebModule1.NMSMTP1Header
Incomplete(var handled: Boolean;
hiType: Integer);
begin
pageproducer1.htmldoc.add('< p >head is incomplete</ /p >');
flag:=false;
end;
procedure TWebModule1.NMSMTP1InvalidHost
(var Handled: Boolean);
begin
pageproducer1.htmldoc.add('< p >
The sending email server address is invalid</p>');
flag:=false;
end;
procedure TWebModule1.NMSMTP1RecipientNot
Found(Recipient: String);
begin
pageproducer1.htmldoc.add
('< p >The email address received is incorrect</p>');
flag:=false;
end;
procedure TWebModule1.NMSMTP1Success(
Sender: TObject);
begin
pageproducer1.htmldoc.add('< p >
Email sent successfully</ /p >');
end;
Save the project as sendmail.dpr, compile it and put it in the executable file path of the WEB server (such as: c:intpubscripts), you can respond to the user input of the HTML file dl.htm, and if the user's user name and password are correct, You can enter the page for sending emails. The user can send emails after filling in the email address, subject, and content. This program passed debugging on NT SERVER.