The topic includes followings.
SMTP protocol is designed to send mails to mail server by mail client's requests. The specification is defined by RFC821(August 1982). Within the Internet, SMTP is a well-known mail transfer protocol.
Outlook/Becky is famous mail client software in Windows environments. Mail server that support SMTP protocol is called SMTP server. SMTP server software works on computers that is connected by TCP/IP protocol on LAN or remote-computers In UNIX/Linux environments, sendmail or qmail is famous SMTP server software. In WindowsNT/2000 environments, Computers that run IIS can work as SMTP server.
BASP21 Pro's support the mail client features.
SMTP server is listening to port 25 after establishing a TCP/IP connection. BASP21 Pro object try to establish a connection to port 25 on mail server using WinSock when requested to send mail. when a connection is established, it will send mail data using SMTP protocol. If server name is wrong, the connection will fail. Use telnet command as following to ensure server name.
telnet server name 25The server name is correct. If the connection is established and massages from SMTP server are shown.
Followings are brief introduction of communication with SMTP server.
1. Obtain IP address from SMTP server name using DNS - omissible if you specify IP address directly. 2. Establish a TCP connection to port 25 3. Obtain domain name from client IP address for HELO/EHLO command - omissible if you specify domain name directly. 4. Call mail server (HELO/EHLO command) 5. Authentication (SMTP AUTH) - option 6. Prepare to send mail (MAIL FROM command) 7. Notify destination address (RCPT TO command) 8. send data (body) (DATA command) 9. Notify completion (QUIT command) 10. Disconnect TCPNormally, SendMail method do these jobs only once. when you specify mail-queue, SendMail method only create mail files. FlushMail method repeat above jobs( 6.Prepare to send mail to 8.send data ) as many times as mail files in mail-queue
you can speed up above jobs (depending on your environments), with specifying IP address and domain name directory which can skip [DNS process of 1.] and [DNS process of 3.].
----------------- basp21p.ini ------------------------------------------- [honban] server=myhost.basp21.com/192.168.1.200
As for detailed feature of SMTP commands, You can refer to RFC821(specification of SMTP protocol). No mail can be sent without specifying user name and password. If the SMTP server use SMTP AUTH. Regarding authentication system including SMTP AUTH, we explain in the next section.
SendMail/FlushMail method return the result of mail reception from SMTP server. so, you can not know if the mail was properly sent to the recipients or not at that moment. this is same as sending mail using mail client.
SMTP server return messages to SMTP command from client with 3 digit response code. SMTP command and response code will be logged in log record you can check the log record when trouble shooting. You can know the condition of process from the first number of the response code.
code | Description |
2xx | normal response |
3xx | normal response, next data needed. |
4xx | temporary error e.g. insufficient memory. |
5xx | mail address format error, permanent errors e.g. authentication error |
POP Before SMTP(POP3 authentication) allows to send mail from the IP addresses that is authenticated to receive mail from the correspond mail server for a certain fixed time. this authentication is sometime used by free mail service like geocities. BASP21 Pro can use POP3 authentication using RcvMail method. usually you only need to execute STAT (command to notify the number of mails) .
Authentication called SMTP AUTH is supported. SMTP AUTH sends user and password data in the session of SMTP protocol to get authentication from SMTP server. EHLO command is used to use SMTP AUTH authentication while HELO command is usually used to salute SMTP server. In most case, the user name and password are the these of correspond mail server. There are two major methods of sending user name and password, one is "LOGIN" that use BASE64, the other is "CRAM-MD5" that hash data using MD5.
BASP21 Pro supports "LOGIN" and "CRAM-MD5". To use SMTP AUTH by BASP21 Pro, specify user name + ":" + password + "," +"login|cram-md5" (separated by a comma) after MAILFROM address in the MailFrom property
bobj.MailFrom = "guest@basp21.com,user1:pass1" ' LOGIN is the default method. bobj.MailFrom = "guest@basp21.com,user1:pass1,cram-md5"
guest@basp21.com <guest@basp21.com> arbitrary name<guest@basp21.com> guest@basp21.com(arbitrary name)To specify arbitrary name would be better, because mail client view it, while it does not affect SMTP server's operation
You can use Japanese for an arbitrary name, if your mail client does not support Japanese, it will view garbled characters.
To specify more than one send to address, separate them by comma (","). Don't use comma(",") within an arbitrary name
mailto = "guest@basp21.com,guest2<guest2@basp21.com>"
header fields | description |
Received | Timestamp of mail server (SMTP Server) relayed to other servers. |
Message-ID | Unique ID on the Internet |
Date | Date information and Time information including TimeZone info. This information describe the time the sender's application send the e-mail. |
From | Sender's mail address. Next 3 types are allowed. name@domain real_name <name@domain> name@domain (real_name) It seems to be displayed on the client mail software (mailer), the part of real_name only. . You can name it anything for real_name. |
To | Receivers' mail address. All mail addresses you specified here, would be visible from individual receiver. If you worry about this matter because of security or privacy reason, you should better to set only one address for each mail. In addition, using BCC is much better to send a mail to many people at one time. |
Cc | Means "Carbon-Copy". The address / addresses where you want to send the copy of mail. Mostly, this is used when you want to inform someone that such mail has sent for some reason. |
Reply-To | If you want to determine the return address which you expect to receive reply
and it is different from the mail address you specified at From field, then
enter it here. When "Reply" button has been pushed, most
mailer behaves to send the selected mail to the mail address displayed at
Reply-To field. Use it in this way; mailto = "id1" & vbtab & ">Reply-To: me@domain" |
Subject | The title of mail itself. When you omitted, you still have a choice, it will be a very vain mail or a mysterious mail. So, just try to enter somewhat you can title it. |
Content-Transfer-Encoding | The type of how mail content was encoded. Such case that content is binary, the mail should be delivered with the header including Content-Transfer-Encoding as base64, uuencode, or etc.. It also depends on each content. |
Content-Type | File Format and Coding Types |
X-Mailer | Header to show the version and client e-Mail application (called mailer) being used |
X-Mail-Agent | Header to show the program name being used. BASP21 Pro uses this header information. |
----------------- basp21p.ini ------------------------------------------- [test] server=smtp-server mailfrom=your@basp21.com mailoption=cc,hoge@basp21.com,hoge2@basp21.com,bcc,hoge3@basp21.com [honban] server=hosta/192.168.1.200 mailfrom=your@basp21.com,user1:pass1 mailoption=cc,hoge@basp21.com,>Reply-To: me@basp21.comIn this case (shown above), when the property of 'Env' is set as "test", "smtp-server" will be used as SMTP server. E-mail address recognized as MAILFROM will be your@basp21.com in this case.
bobj.Env = "test" bobj.SendMail mailto,subj,body,files
When the property of 'Env' is set as "honban", "192.168.1.200" will be used as SMTP server in this case. The former word separated by "/" will be used as domain-name, when the SMTP protocol put HELO command to the server.
bobj.Env = "honban" bobj.SendMail mailto,subj,body,files
You can also specify the SMTP server name and MAILFROM address dynamically in your scripts, as shown below.
bobj.Env = "honban" bobj.Server = "hosta/192.168.1.200" bobj.MailFrom = "your@basp21.com,user1:pass1" bobj.MailOption = "cc,hoge@basp21.com,>Reply-To: me@basp21.com" bobj.SendMail mailto,subj,body,files
E-mail data files will be created in Shift_JIS code, not being encoded into JIS code, after the validity check of e-mail addresses and mail options. The content written in 'MailOption' Property will be also imported into the e-mail data files, however, SMTP server name will be not. E-mail data file contains the data like this below.
Line 1 : MAILFROM address + Mail Options (such as Header Information) Line 2 : MAILTO address + SMTP AUTH parameter (Optional) Line 3 : Subject Line 4 : Attached File Name Line 5-: <<Contents>>You can see the content of e-mail data files using 'ReadMail' Method . MailQueue directory ,"err" subdirectory and "sent" subdirectory will be created automatically when you specify the 'mailqueue' parameter in BASP21P.INI file.
For example, when you describe BASP21P.INI file shown below:
----------------- basp21p.ini ------------------------------------------- [iis5] home=c:\test\env1 mailqueue=.mailqueue3 directories shown below will be created automatically.
c:\test\env1\mailqueue c:\test\env1\mailqueue\err c:\test\env1\mailqueue\sentThe content of E-mail data files created in the directory specified by 'MailQueue' Property (or 'mailqueue' parameter in .ini file) will be forwarded to SMTP server by using 'FlushMail' Method.
Here is a sample script using 'SendMail' Method. Typical HTML <FORM> are used run before this script.
================ sendm01.asp ============================================ <html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <title>SendMail Testing Page</title></head> <body> <% set bobj = Server.CreateObject("basp21pro") bobj.Env = "iis5:" & Request.ServerVariables("REMOTE_ADDR") mailto=Request.Form("name") subj=Request.Form("subj") & now() comment=Request.Form("comment") bobj.MailQueue="" rc = bobj.SendMail(mailto,subj,comment) If bobj.IsError Then rc = bobj.LastMsg End If set bobj = Nothing %> rc=<% = rc %><br> name=<% = name %><br> subj=<% = subj %><br> <pre> comment=<% = comment %> </pre> <p></body></html>This is a sample HTML file which will send messages with "POST" method to sendm01.asp (shown above).
================ sendm01.html ============================================ <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <TITLE>SendMail Testing Page</TITLE></HEAD> <BODY> <FORM ACTION=sendm1.asp METHOD=POST> name:<INPUT TYPE=TEXT name=name><BR> subj:<INPUT TYPE=TEXT name=subj><BR> comment:<TEXTAREA name=comment rows=5 cols=50></TEXTAREA><BR> <INPUT TYPE=SUBMIT name=SUBMIT value=GO!!!!> </FORM> </BODY></HTML>This is a sample of BASP21P.INI file.
----------------- basp21p.ini ------------------------------------------- [global] allow=env [iis5] allow=sendmail server=smtp-server mailfrom=hoge@basp21.com home=c:\basp21p\iis5 logfile=.\log.txt logfilerotate=1024If there are a lot of e-mails transmitted by IIS process, loads in the connection line and SMTP server will be too high because of the continuous connection and disconnection process. In such a case, there's worth consideration of package mail transmission using 'MailQueue' and 'FlushMail' Method.
----------------- sendm02.asp ------------------------------------------- <html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <title>SendMail Testing Page</title></head> <body> <% set bobj = Server.CreateObject("basp21pro") bobj.Env = "iis5:" & Request.ServerVariables("REMOTE_ADDR") If IsRun("FlushMail") Then rc = bobj.FlushMail If bobj.IsError Then rc = bobj.LastMsg End If Else rc = "FlushMail busy" End If set bobj = Nothing Function IsRun(method) ' Check the running Method with <Monitor> Method IsRun = 0 ar = bobj.Monitor(1) If bobj.Result <= 0 Then Exit Function For Each ret in ar If InStr(ret,method) > 0 Then IsRun = 1 Exit Function End If Next End Function %> rc=<% = rc %><br> </pre> <p></body></html>This is a sample script written in JScript.
----------------- jsendm02.asp ------------------------------------------- <html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <title>SendMail Testing Page</title></head> <body> <% var bobj,rc; bobj = Server.CreateObject("basp21pro"); bobj.Env = "iis5:" & Request.ServerVariables("REMOTE_ADDR"); if (IsRun("FlushMail") == 0) { rc = bobj.FlushMail(); if (bobj.IsError == 1)rc = bobj.LastMsg; } else rc = "FlushMail busy"; function IsRun(method) { var vbarray,ar,ret,taskno; vbarray = bobj.Monitor(1); if (bobj.Result <= 0) return 0; taskno = bobj.Result; ar = vbarray.toArray(); var i; for (i = 0; i < taskno; i++) { if (ar[i].indexOf(method) != -1) return 1; } return 0; } %> rc=<% = rc %><br> </pre> <p></body></html>'FlushMail' Method creates a list of e-mail data files stored in MailQueue Directory when this Method was called. 'FlushMail' Method transmit e-mails referring the list created by itself. Of course, e-mails created by 'SendMail' Method after the creation of the list will not be transmitted in this procedure.
When the transmission are properly proceeded, the e-mail data files which are forwarded to SMTP server are deleted from MailQueue Directory. If the MailQueue Directory has its subdirectory named " sent", the e-mail data files correctly transmitted move to the subdirectory.
The e-mail data files not forwarded properly to SMTP server remain in the MailQueue Directory. If the MailQueue Directory has its subdirectory named "err", the e-mail data file which failed the transmission move to the subdirectory
'FlushMail' Method of the 2nd henceforth will fail if the 'FlushMail' Method is on process in the same MailQueue Directory. We recommend you to check whether 'FlushMail' Method process are running or not by using 'Monitor' Method
at 18:00 /every: f /interactive c:\b21pro\bhelper -e env1 flushmailYou can run specified Method in another CPU process by using 'Process' Method. In this sample script written in VBScript, e-mail data files are transmitted ('FlushMail' Method is used) in another CPU process when the number of files in MailQueue directory is over 20.
----------------- sendm03.asp ------------------------------------------- <html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <title>BASP21 Pro Page</title></head> <body> <% set bobj = Server.CreateObject("basp21pro") bobj.Env ="env1:test1.asp" name=Request.Form("name") subj=Request.Form("subj") comment=name & vbCrLf & Request.Form("comment") mailto="g1<hoge@basp21.com>" rc = bobj.SendMail(mailto,subj,comment) If Not bobj.IsError Then msg = "mail queueing...." call CheckFlushMail Else msg = bobj.LastMsg End If Sub CheckFlushMail mailcount = bobj.FileCheck(bobj.MailQueue,1) If 20 < mailcount Then If IsRun("FlushMail") = 0 Then rc = bobj.Process("-e env1 FlushMail") msg = "FlushMail Start ... " & mailcount & " threadid=" & rc Else msg = "FlushMail Running..." End If End If End Sub Function IsRun(method) ' Check the running Method with <Monitor> Method IsRun = 0 ar = bobj.Monitor(1) If bobj.Result <= 0 Then Exit Function For Each ret in ar If InStr(ret,method) > 0 Then IsRun = 1 Exit Function End If Next End Function %> msg=<% = msg %><br> rc=<% = rc %><br> name=<% = name %><br> subj=<% = subj %><br> <pre> comment=<% = comment %> </pre> <p></body></html>This is a sample script written in JScript.
----------------- jsendm03.asp ------------------------------------------- <%@ LANGUAGE="JavaScript" %> <html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <title>BASP21 Pro Page</title></head> <body> <% var bobj,msg; bobj = Server.CreateObject("basp21pro"); bobj.Env ="env1:jtest1.asp"; var name = Request.Form("name"); var subj = Request.Form("subj"); var comment = name + unescape("%0D%0A") + Request.Form("comment"); var mailto = "g1<hoge@basp21.com>"; var rc = bobj.SendMail(mailto,subj,comment); if (!bobj.IsError) { msg = "mail queueing...."; CheckFlushMail(); } else { msg = bobj.LastMsg; } function CheckFlushMail() { var mailcount,rc; mailcount = bobj.FileCheck(bobj.MailQueue,1); if (20 < mailcount) { if (IsRun("FlushMail") == 0) { rc = bobj.Process("-e env1 FlushMail"); msg = "FlushMail Start ... " + mailcount + " threadid=" + rc; } else { msg = "FlushMail Running..."; } } } function IsRun(method) // check the running method (process) { var vbarray,ar,ret,taskno; vbarray = bobj.Monitor(1); if (bobj.Result <= 0) return 0; taskno = bobj.Result; ar = vbarray.toArray(); var i; for (i = 0; i < taskno; i++) { if (ar[i].indexOf(method) != -1) return 1; } return 0; } %> msg=<% = msg %><br> rc=<% = rc %><br> name=<% = name %><br> subj=<% = subj %><br> <pre> comment=<% = comment %> </pre> <p></body></html>
mailto = Request.Form("name") mailto = mailto & ",cc,hoge2<hoge2@basp21.com>,hoge3<hoge3@basp21.com>" mailto = mailto & ",bcc,hoge4<hoge4@basp21.com>,hoge5<hoge5@basp21.com>" rc = bobj.SendMail(mailto,subj,comment)Also, you can specify CC/BCC addresses by using 'MailOption' Property in .INI file.
mailto = Request.Form("name") bobj.MailOption = "cc,hoge2<hoge2@basp21.com>,hoge3<hoge3@basp21.com>" & _ ",bcc,hoge4<hoge4@basp21.com>,hoge5<hoge5@basp21.com>"You can set the default value of 'MailOption' Property with 'mailoption' parameter in .INI file.
subject = "Conference Notification!" bobj.SendMail mailto,subject,bodyThe E-mail Contents (Mail Body) should be specified by the third parameter of 'SendMail' Method. "New Line" Code should be specified by CR(13) + LF(10). If you use only CR or only LF as New Line Code, this new line code will be transformed to CR + LF.
body = "Hello" & vbCrLf & "World!"Hankaku-kana (Japanese) Characters in E-mail Subject and Mail Body will be converted into Zenkaku-Kana while transmission process automatically.
The data of E-mail contents are usually transmitted in 7-bit. If you use Japanese code in the header information, BASP21 Pro will convert the E-mail Subject into JIS code and encode with BASE 64. If the Mail Body contains Japanese Character, it will convert them into JIS code. The Attached Files will be encoded with BASE 64 and transmitted after the Mail Body being marked off by "MIME boundary" string.
files = "c:\temp\abc.doc" rc = bobj.SendMail(mailto,subj,comment,files)To specify 2 or more files as Attached Files, use "," (COMMA) as delimiter.
files = "c:\temp\abc.doc,c:\temp\abc.jpg,c:\temp\abc.txt" rc = bobj.SendMail(mailto,subj,comment,files)Optionally, you can specify the alias or Content-Type Header in MIME by using "|" as delimiter. This feature is often used when you want to transmit an attached file in another name.
files="c:\temp\aa.txt|abc.txt" ' specify alias files="c:\temp\aa.txt||application/octet-stream" ' specify Content-type files="c:\temp\aa.txt|abc.txt|application/octet-stream" ' alias+Content-typeThe Content-type Header Information is referred by E-mail Client Software when it receive the attached files.
File Extension | Content-type |
txt | text/plain |
htm html | text/html |
jpg jpeg | image/jpeg |
gif | image/gif |
bmp | image/bmp |
zip | application/x-zip-compressed |
doc | application/msword |
xls | application/vnd.ms-excel |
ppt | application/vnd.ms-excel |
xls | application/vnd.ms-powerpoint |
exe dll | application/vnd.ms-download |
eml mht mhtml nws | message/rfc822 |
fdf | application/vnd.fdf |
application/pdf | |
rmi | audio/mid |
qt mov | video/quicktime |
avi | video/x-msvideo |
tiff | image/tiff |
wav mp1 mp2 mp3 | audio/wav |
als | Audio/X-Alpha5 |
OTHERS | application/octet-stream |
bobj.MailOption = ">Return-Path: <admin@basp21.com>"With the e-mail including 'Disposition-Notification-To' Header Information, E-mail recipient's mail software send you back a notification of disposition automatically. (if the e-mail software have the functions.)
bobj.MailOption = ">Disposition-Notification-To: <admin@basp21.com>"Use 'Content-Type' Header Information properly if you want to send HTML format e-mails.
bobj.MailOption = ">Content-Type: text/html; charset=iso-2022-jp"The default value of 'MailOption' Property can be set by using 'mailoption' parameter in .INI file.
----------------- basp21p.ini ------------------------------------------- [iis5] mailoption=>Return-Path: <admin@basp21.com>, >Disposition-Notification-To: <admin@basp21.com>
bobj.MailFrom = "guest@basp21.com,user1:pass1" ' default, "LOGIN" bobj.MailFrom = "guest@basp21.com,user1:pass1,cram-md5"The default value of 'MailFrom' Property can be set by using 'mailfrom' Parameter in .INI file.
----------------- basp21p.ini ------------------------------------------- [iis5] allow=sendmail,flushmail mailfrom=guest@basp21.com,user1:pass1
bobj.RcvMail "","","stat" bobj.FlushMailE-mail transmission will be allowed for a while ( about 10 minutes or more) after the connection to the server and after the use of 'RcvMail' Method.
550 Relaying is prohibited to xxxxxx 550 5.7.1 Unable to relay for xxxxxx 550 xxxxxxxxxxx Relaying denied to xxxx 550 relaying mail to xxxx is not allowed to xxxxx 550 Unable to relay for xxxxxx 553 xxxx... Relay operation rejected 571 Relay operation rejected xxxxxxxxxx 572 Relay not authorizedIf the relaying error occured, refer to Authentication with 'SendMail' or POP Before SMTP section and try them.
Topics includes as follows.
1. Obtain IP address from POP3 server name using DNS - omissible if you specify IP address directly. 2. Establish a TCP connection to port 110. 3. Authentication (USER Command, PASS Command) 4. Confirm contents in the mailbox (STAT Command) 5. Retrieve each mails (RETR Command) 6. Request for deletion (DELE Command) 7. Notify completion (QUIT Command) 8. TCP disconnectionYou can specify the procedure from [4.] to [6.] POP3 Command shown above by using command of 'RcvMail' Method.
POP3 server returns messages with response code to the POP3 command from client. When in trouble, you can check and confirm the logs of communication with client and server in detail , for POP3 command and response code would be recorded at Log Record .
POP3 Command | Description |
STAT | Returns how many mails are stored in mail server and its total byte size |
LIST | Returns byte size of each mails. |
RETR | Retrieves the content of a specified mail. |
DELE | Deletes a specified mail. |
TOP | Retrieves the message header of a specified mail and the specified line of its content. |
UIDL | Retrieves the ID of a specified mail. |
USER | Sends User Name used at User Authentication. |
PASS | Sends Password used at User Authentication. |
APOP | Sends the Information of User Authentication encrypted by MD5 method. |
You can judge the procedure ended normally or not by the head letter of Response Code whether it is "+" or "-". POP3 Response Code is as follows.
POP3 Response | Description |
+OK xxx | Regular Response |
-xxxxx | Irregular Response caused by some errors. |
rc = bobj.RcvMail("user1","pass1","stat")The default values of username and password can be specified by 'mailuserpass' parameter in .INI file. If you set zero byte string at the first and second parameter in 'RcvMail' Method, 'mailuserpass' parameter would be used to specify username and password.
rc = bobj.RcvMail("","","stat")
Recently, the way to transmit password with encryption is supported by general POP3 server (software), as is called 'APOP Authentication'. BASP21 Pro also support this 'APOP Authentication'. To use APOP, one blank is applied to "a" or "A" ahead of the password.
rc = bobj.RcvMail("user1","A pass1","stat")
ar = bobj.RcvMail("user1","pass1","stat") mail_count = bobj.Result If mail_count > 0 Then temp = Split(ar(0)," ") mail_size = temp(1) End IfThe count of e-mails stored in POP3 server would be set in 'Result' Property. If the procedure would be terminated normally, the result of 'RcvMail' Method would be an array and each of every element of the array would have < (Mail Number) + " " + (Mail Size) > String. Refer to the example shown above.
RcvMail Command | Description |
SAVENEW | Receives all new mails, and does not delete from Mail Server. |
SAVENEWD | Receives all new mails, and deletes from Mail Server. |
SAVEALL | Receives all mails (up to 8192 mails), and does not delete from Mail Server. |
SAVEALLD | Receives all mails (up to 8192 mails), and deletes from Mail Server. |
SAVE n[-n2] | Receives #n mail, and does not delete from mail server. To set the range, parameter will be n-n2, where retrieving from #n mail to #n2 mail. If n2 is omitted, it returns only #n mail. |
SAVD n[-n2] | Receives #n mail, and deletes from Mail Server. To set the range, parameter will be n-n2, where retrieving from #n mail to #n2 mail. If n2 is omitted, it returns only #n mail. |
The result of receiving command of 'RcvMail' Method would be an array of file
names equivalent to received mails.
The number of received mail is set at 'Result' Property.
ar = bobj.RcvMail("user1","pass1","SAVENEW",bobj.MailBox & "\new") mail_count = bobj.Result
The other Command of 'RcvMail' Method is as follows.
RcvMail Command | Description |
LIST [n[-n2]] | Returns an array of Subject, From, and Date fields, and each field are separated by tab. To set the range, parameter will be n-n2, where retrieving from #n mail to #n2 mail. If n2 is omitted, it returns only #n mail. If both n and n2 are omitted, it returns all (or first 8192nd) mail. |
SIZE [n[-n2]] | Returns the size of mails matched off against the #n mail to #n2 mail. To set the range, parameter will be n-n2. If n2 is omitted, it returns only #n mail's. If both n and n2 are omitted, it returns all (or first 8192nd) mail's. It returns strings which contains the number and " " (blank) and size like "n ssss". |
UIDL [n[-n2]] | Returns the UIDL of mails matched off against the # n mail to #n2 mail. To set the range, parameter will be n-n2. If n2 is omitted, it returns only #n mail's. If both n and n2 are omitted, it returns all (or first 8192nd) mail's. It returns strings which contains the number and " " (blank) and UIDL like "n uuuu". |
DELE [n[-n2]] | Deletes #n mail from mail server. To set the range, parameter will be n-n2, where retrieving from #n mail to #n2 mail. If n2 is omitted, it deletes only #n mail. If both n and n2 are omitted, it deletes all (or first 8192nd) mails. |
DELU UIDLX[,uildx,...] | Deletes mails specified by UIDLX from server. Using "," (COMMA) as delimiter, you can set 8192 UIDLX at the same time. |
<< Mail Header >> [CR LF] << Mail Body >> (JIS code)A header is usually consisted of a line. A long header is usually formed into 70 bites at most, and the first word in the second line is blank or connected with a tab. A text is sent as it is without paragraph changes from a mail server unless a sender specifies paragraph changes with the code. When you send text data with Japanese sentences having more than 1000 bites in a line, it is possible that Japanese sentences turn into garbage throughout the lines, depending on the mail client.
The Japanese part of a header is formed with base64 transformed from JIS code. As for transforming the code of Japanese part in the header and the text and, the following MIME, mail client does it and mail server has nothing to do with it.
When there is an attachment file besides the text, the text and the file data get divided by a boundary character line in a format called MIME. A mail server, disregarding MIME, regards the attachment as text data. Normally, an attachment file is text data encoded by base 64.
<< Mail Header >> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="sep680007" [CR LF] --sep680007 [MIME Header] << Mail Body >> --sep680007 [MIME Header] << Attached File 1 >> --sep680007 [MIME Header] << Attached File 2 >> --sep680007--Use ReadMail Method in order to interpret the received RFC822 file, and separate it into header, text, and attachment file.
File name samples:
c:\test\env1\mailbox\3a92700d00000001 c:\test\env1\mailbox\3a92700d00000002 c:\test\env1\mailbox\3a92700d00000003
UIDL is a unique extension that gets returned from the server by POP3's UIDL command. UIDLX is an ID that is converted to 16-bit characters by adding an "X" in place of characters that are not numbers or periods.
When mail is received, Basp21Pro always issues a UIDL command, and reads the UIDL that corresponds to the mail number. When there is a file with the same UDLIX name in the directory that the file is read from, it is regarded as read mail, and the contents of the mail will not be read from the server. when there is no UDLIX name file in the directory, It is regarded as a new mail, and will be read from the server and saved with UDLIX name format.
Set fs = Server.CreateObject("Scripting.FileSystemObject") fs.DeleteFile bobj.MailBox & "\new\" & "acf00000001234"
Using the SortMail method enables you to delete only the mail files that meet the conditions.
ar = bobj.SortMail(bobj.MailBox,"",0,"Date:=2001/02",1) If bobj.Result > 0 Then cmd = "DELU " & Join(ar,",") outarray = bobj.RcvMail(user,pass,cmd) ' delete from mail server. End IfTo delete them from mail server at the same time, use a DELU command.
The name of mail file that was received from mail server with RcvMail method
becomes UIDLX.
Using a DELU command of RcvMail method enables you to delete the mail from the
mail server by specifying the UIDLX. This method is useful to delete the mail
files that have been stored in local folders.
The function shown below (XIDL2X) converts UIDL (in) into UIDLX (out).
' UIDL-> UIDLX convert Function UIDL2X(uidl) Dim ch,i,j,dlen,out,tbl dlen = Len(uidl) out = "" tbl = "0123456789ABCDEF" For i = 1 to dlen ch = Mid(uidl,i,1) If ((ch >= "0" And ch <= "9") Or _ ch = "." Or _ (ch >= "a" And ch <= "z") Or _ (ch >= "A" And ch <= "Z" And ch <> "X") ) Then out = out & ch Else out = out & "X" j = Clng(Asc(ch)) out = out & Mid(tbl,((j \ 16) And 15) + 1,1) out = out & Mid(tbl,(j And 15) + 1,1) End If Next UIDL2X = out End FunctionThe function shown below (X2UIDL) converts UIDLX (in) into UIDL (out).
' UIDLX --> UIDL convert Function X2UIDL(uidlx) Dim ch,ch2,i,j,j2,dlen,out dlen = Len(uidlx) out = "" For i = 1 to dlen ch = Mid(uidlx,i,1) If ch <> "X" Then out = out & ch Else i = i + 1 ch = Mid(uidlx,i,1) j = Clng(Asc(ch)) If ch <= "9" Then j = j - 48 Else If ch <= "F" Then j = j - 55 Else j = j -87 End if End If i = i + 1 ch2 = Mid(uidlx,i,1) j2 = Clng(Asc(ch2)) If ch2 <= "9" Then j2 = j2 - 48 Else If ch2 <= "F" Then j2 = j2 - 55 Else j2 = j2 - 87 End If End If out = out & Chr(j * 16 + j2) End If Next X2UIDL = out End Function
RcvMail receives and saves a mail as a file in RFC822 format. In a mail containing Japanese, the Japanese in its header or text portion is encoded in base64 or JIS code. Since the attached file is encoded in MIME specification following the text portion, it cannot be retrieved directly.
This section describes how to read a received mail and how to sort and select a mail saved in directory along with associated operations.
Following topics will be explained in this section.
mfile = bobj.MailBox & "\3ab9b19100000001" ar = bobj.ReadMail(mfile,"from:subject:date:") rc = bobj.Result If rc > 0 Then mailfrom = ar(0) ' retrieve Form header subject = ar(1) ' retrieve Subject header dates = ar(2) ' retrieve Date header End IfIf the job was completed normally, headers specified by this parameter are returned in turn into the array as the returned values of 'ReadMail' Method. If the second parameter is omitted or specified by "", all the headers are returned.
ar = bobj.ReadMail(mfile)Date header is modified matching to the local time of the machine on which BASP21 Pro is operating.
Date: 2001/03/22 17:00:00Local time of the machine can be changed by selecting [Control panel]-[Date and Time]-[Time zone] in sequence. If the time zone is changed, it affects other programs that use this date and time, so we do not recommend to change the time zone. Instead, we recommend to set the timebiasparameter to adjust the time of Date-header without changing the local time of the machine.
----------------- basp21p.ini ------------------------------------------- [hawai] timebias=-19
bobj.Env = "hawai" ar = bobj.ReadMail(mfile)
ar = bobj.ReadMail(mfile,"from:subject:date:") rc = bobj.Result If rc > 0 Then For Each line in ar If InStr(line,"Body: ") = 1 Then body = Mid(line,7) ' Retrieval of Mail Body End If Next End IfIf you don't want the body text to be returned, specify it with "nobody:".
ar = bobj.ReadMail(mfile,"from:subject:date:nobody:")
ar = bobj.ReadMail(mfile,"from:subject:date:","c:\temp")The name of attached file is the data that begins with "File: " after the body text of the returned value array of ReadMail.
ar = bobj.ReadMail(mfile,"from:subject:date:") rc = bobj.Result If rc > 0 Then For Each line in ar If InStr(line,"File: ") = 1 Then files = files & "," & Mid(line,7) ' Retrieval of attached file names End If Next End IfBy default, when the file with the same name exists in the storing directory of attached files, it is overwritten. If you don't want to overwrite it, you can specify how to deal the file using the 'readmailopt' parameter.
----------------- basp21p.ini ------------------------------------------- [iis5] readmailopt=2
ar = bobj.ReadMail(mfile,"from:subject:date:nofile:")Another method to do so is to specify the 'readmailopt' parameter
----------------- basp21p.ini ------------------------------------------- [iis5] readmailopt=1
----------------- sortmail01.asp ------------------------------------------- Set bobj = WScript.CreateObject("basp21pro","basp21pro_") bobj.Env = "env1" ar = bobj.SortMail(bobj.MailBox,"Date:",1) rc = bobj.Result html = "<HTML><BODY><TABLE>" If rc > 0 Then For Each line in ar mfile = bobj.MailBox & "\" & line mdata = readmailtag(mfile) html = html & vbCrLf & mdata Next End If html = html & vbCrLf & "</TABLE></BODY></HTML>" Function readmailtag(fname) Dim ar,mfile,rc,mailfrom,subject,maildate ar = bobj.ReadMail(fname,"from:subject:date:nobody:nofile:") rc = bobj.Result If rc > 0 Then mailfrom = bobj.RepTagChar(ar(0)) subject = bobj.RepTagChar(ar(1)) maildate = ar(2) End If readmailtag = "<TR><TD>" & mailfrom & "</TD><TD>" & subject & _ "</TD><TD>" & maildate & "</TD></TR>" End FunctionThe sample shown below is written in JScript.
----------------- jsortmail01.asp ------------------------------------------- var bobj,rc,vbarray,ar,crlf; crlf = unescape("%0D%0A"); set bobj = Server.CreateObject("basp21pro"); bobj.Env = "env1"; vbarray = bobj.SortMail(bobj.MailBox,"Date:",1); rc = bobj.Result; html = "<HTML><BODY><TABLE>"; if (rc > 0) { ar = vbarray.toArray(); var i; for (i = 0; i < rc ; i++) { mfile = bobj.MailBox + "\\" + ar[i]; mdata = readmailtag(mfile); html = html + crlf + mdata; } } html = html + crlf + "</TABLE></BODY></HTML>"; function readmailtag(fname) { var vbarray,ar,rc,mailfrom,subject,maildate; vbarray = bobj.ReadMail(fname,"from:subject:date:nobody:nofile:"); rc = bobj.Result; if (rc > 0) { ar = vbarray.toArray(); mailfrom = bobj.RepTagChar(ar[0]); subject = bobj.RepTagChar(ar[1]); maildate = ar[2]; return "<TR><TD>" + mailfrom + "</TD><TD>" + subject + "</TD><TD>" + maildate + "</TD></TR>"; } return ""; }
----------------- sortmail02.asp ------------------------------------------- Set bobj = Server.CreateObject("basp21pro") bobj.Env = "env1" ar = bobj.SortMail(bobj.MailBox,"Date:",1,"from:=freemail") rc = bobj.Result html = "<HTML><BODY><TABLE>" If rc > 0 Then For Each line in ar mfile = bobj.MailBox & "\" & line mdata = readmailtag(mfile) html = html & vbCrLf & mdata Next End If html = html & vbCrLf & "</TABLE></BODY></HTML>" Function readmailtag(fname) Dim ar,mfile,rc,mailfrom,subject,maildate ar = bobj.ReadMail(fname,"from:subject:date:nobody:nofile:") rc = bobj.Result If rc > 0 Then mailfrom = bobj.RepTagChar(ar(0)) subject = bobj.RepTagChar(ar(1)) maildate = ar(2) End If readmailtag = "<TR><TD>" & mailfrom & "</TD><TD>" & subject & _ "</TD><TD>" & maildate & "</TD></TR>" End FunctionThe sample shown below is written in JScript.
----------------- jsortmail02.asp ------------------------------------------- var bobj,rc,vbarray,ar,crlf; crlf = unescape("%0D%0A"); set bobj = Server.CreateObject("basp21pro"); bobj.Env = "env1"; vbarray = bobj.SortMail(bobj.MailBox,"Date:",1,"from:=freemail"); rc = bobj.Result; html = "<HTML><BODY><TABLE>"; if (rc > 0) { ar = vbarray.toArray(); var i; for (i = 0; i < rc ; i++) { mfile = bobj.MailBox + "\\" + ar[i]; mdata = readmailtag(mfile); html = html + crlf + mdata; } } html = html + crlf + "</TABLE></BODY></HTML>"; function readmailtag(fname) { var vbarray,ar,rc,mailfrom,subject,maildate; vbarray = bobj.ReadMail(fname,"from:subject:date:nobody:nofile:"); rc = bobj.Result; if (rc > 0) { ar = vbarray.toArray(); mailfrom = bobj.RepTagChar(ar[0]); subject = bobj.RepTagChar(ar[1]); maildate = ar[2]; return "<TR><TD>" + mailfrom + "</TD><TD>" + subject + "</TD><TD>" + maildate + "</TD></TR>"; } return ""; }
----------------- sortmail03.asp ------------------------------------------- Set bobj = Server.CreateObject("basp21pro") bobj.Env = "env1" ar = bobj.SortMail(bobj.MailBox,"",0,"from:=freemail",1)The sample shown below is written in JScript.
----------------- jsortmail03.asp ------------------------------------------- var bobj,rc,vbarray,ar,crlf; crlf = unescape("%0D%0A"); set bobj = Server.CreateObject("basp21pro"); bobj.Env = "env1"; vbarray = bobj.SortMail(bobj.MailBox,"",0,"from:=freemail",1);
----------------- reply01.asp ------------------------------------------- Dim bobj,ar,rc,ctr Set bobj = Server.CreateObject("basp21pro") bobj.Env = "env1" ar = bobj.SortMail(bobj.MailBox,"",0,"subject:=basp21") rc = bobj.Result If rc > 0 Then For Each line in ar mfile = bobj.MailBox & "\" & line mailto = getmailto(mfile) putmail mailto ctr = ctr + 1 Next End If If ctr > 0 Then bobj.FlushMail End If Function getmailto(fname) Dim ar,rc,mailto ar = bobj.ReadMail(fname,"reply-to:from:") rc = bobj.Result If rc > 0 Then mailto = Mid(ar(0),10) mailto = Trim(mailto) If Len(mailto) <= 0 Then mailto = Mid(ar(1),6) mailto = Trim(mailto) End If End If getmailto = mailto End Function Sub putmail(mailto) Dim fso,f subject = "Request for Guide" Set fso = Server.CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(bobj.Home & "\body.txt", 1) body = f.ReadAll files = bobj.Home & "\basp21.doc" bobj.SendMail mailto,subject,body,files End SubThe sample shown below is written in JScript.
----------------- jreply01.asp ------------------------------------------- var bobj,vbarray,ar,rc,ctr; bobj = Server.CreateObject("basp21pro"); bobj.Env = "env1"; vbarray = bobj.SortMail(bobj.MailBox,"",0,"subject:=basp21"); rc = bobj.Result ; ctr = 0; if (rc > 0) { ar = vbarray.toArray(); var i,mfile,mailto; for (i = 0; i < rc; i++) { mfile = bobj.MailBox + "\\" + ar[i]; mailto = getmailto(mfile); putmail(mailto); ctr++; } } if (ctr > 0) { bobj.FlushMail(); } function getmailto(fname) { var vbarray,ar,rc,mailto; vbarray = bobj.ReadMail(fname,"reply-to:from:") rc = bobj.Result; if (rc > 0 ) { ar = vbarray.toArray(); mailto = ar[0].substr(9); if (mailto.length <= 1) { mailto = ar[1].substr(5); } mailto = mailto.replace(/(^\s*)|(\s*$)/g, ""); } return mailto; } function putmail(mailto) { var fso, f,subject,body,files; subject = "Send Information"; fso = Server.CreateObject("Scripting.FileSystemObject"); f = fso.OpenTextFile(bobj.Home + "\\body.txt", 1); body = f.ReadAll(); files = bobj.Home + "\\basp21.doc"; bobj.SendMail(mailto,subject,body,files); }
Explanation includes following topics.
----separator20010301 Content-Disposition: form-data; name="yourname" BASP21 Pro ----separator20010301 Content-Disposition: form-data; name="file1"; filename="C:\TEMP\upload.jpg" Content-Type: application/octet-stream xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ----separator20010301--The POST data are transmitted by fields of the form in such a way that a delimiter (---- separator20010301+CRLF) comes first, followed by headers indicating a field name (name="xxxx") and field type (Content-Type) of the text data or file, then by actual data, a delimiter, and the next field, and so forth. A symbol "--" is added to the last delimiter .
There are following BASP21 Pro methods that handle data posted to server-side.
Method | Description |
Form | Retrieve text data sent by INPUT TYPE="TEXT" tag or etc. |
FormBinary | Retrieve text or file as binary data. |
FormFileName | Retrieve file name sent by INPUT TYPE="FILE" tag. |
FormFileSize | Retrieve file size sent by INPUT TYPE="FILE" tag. |
FormSaveAs | Store file in server sent by INPUT TYPE="FILE" tag. |
----------------- fileup01.html ------------------------------------------- <HTML><BODY> <FORM ACTION="fileup.asp" ENCTYPE="multipart/form-data" METHOD="POST"> Your Name: <INPUT TYPE=TEXT NAME="yourname"><BR> File1 : <INPUT TYPE=FILE NAME="file1"><BR> File2 : <INPUT TYPE=FILE NAME="file2"><BR> <INPUT TYPE=SUBMIT NAME=UPLOAD> </FORM></BODY></HTML>Specify the name of CGI/ASP that receive data in server-side in 'ACTION' parameter. this example does not specify the path name of the ASP file, so the html file written above is located in the same virtual directory as the ASP file.
Specify "multipart/form-data" in ENCTYPE, and "POST' in METHOD. Use INPUT tag's TYPE=FILE to specify file. when you specify TYPE=FILE, current browser's RFC1867 implementation does not allow to set default value of the file name in text box by VALUE= for security reason. this implementation is same in IE and Netscape.
Execute following asp file to see the actual data sent from browser.
----------------- fileup02.asp ------------------------------------------- <% a=Request.TotalBytes b=Request.BinaryRead(a) Response.Expires = 0 Response.Buffer = TRUE Response.Clear Response.ContentType= "application/x-binary" Response.AddHeader "Content-Disposition","Attachment; filename=""your.dat""" Response.BinaryWrite b Response.End %>The script above return the received uploaded content to browser just as it is. To check the returned data, save it by using browser's save dialog. Each field in the file is separated following RFC1867 format.
You can also upload file from browser or VBA by UpLoad method without using browser's form(TYPE=FILE).
Usually, Japanese data of file name or text data in POST data is processed as SJIS code. You can specify character code in the third parameter of Form method/ FormFileName method
----------------- fileup03.asp ------------------------------------------- <% a=Request.TotalBytes ' total POST data size b=Request.BinaryRead(a) ' retrieve POST data set bobj=Server.CreateObject("basp21pro") name=bobj.Form(b,"yourname") fname=bobj.FormFileName(b,"file1") fname=Mid(fname,InstrRev(fname,"\")+1) fsize=bobj.FormSaveAs(b,"file1","c:\temp\" & fname) If fsize < 1 Then ' Error msg = bobj.LastMsg Else msg = fsize End If %> <HTML><HEAD><TITLE>File Upload Test</TITLE> <BODY> <H1>Testing</H1> <BR> <%= name %>, Upload completed.<BR> file size= <%= msg %><BR> </BODY></HTML>Use Form method to retrieve the value of text data or check box field. Each data will be separated by tab when retrieving multiple data using Form method in SELECT tag.
FormSaveAs method save received file data to server-side file. In ASP environment using IIS anonymous authentication, BASP21 Pro method work in the security context of IUSR_machine name account. when the writing drive is NTFS file system, you need to configure proper access permission.
Tips for better scripting : Before responding to client, check the return value of FormSaveAs method.
Uploading large file(more than 50MB) at a time makes Request.BinaryRead method extremely slow.
Default Maximum file size for uploading by UpLoad method at a time is 2GB-1(2147483647). When the size is more than 100MB, Use split option ("-G size") to reduce the load on IIS. You can upload 2GB over file using separate option.
set longer script out time when uploading large file.
<% Server.ScriptTimeout = 3600 %>
Specify split size in size in megabytes (MB). Server-side script specified in URL will be called and executed multiple times depending on split size. Optimal split size differs on IIS environment. Following client-side VBScript example shows how to upload file by 5MB size.
bobj.ShowDIalog = 1 fname = bobj.Home & "\" & "bigdata.dat" url = "http://www.basp21.com/test/bigfileup.asp" rc = bobj.UpLoad(url,fname,"-G 5")When using split option, Use Specify Add mode in the fourth parameter of server-side FileSaveAs method to save file. Following is the FormSaveAs method example using VBScript.
----------------- bigfileup.asp ------------------------------------------- <% Response.Clear Response.Buffer = True Server.ScriptTimeout = 3600 a=Request.TotalBytes b=Request.BinaryRead(a) Set bobj=Server.CreateObject("basp21pro") name=bobj.FormFileName(b,"xfile001") name=Mid(name,InstrRev(name,"\")+1) fsize=bobj.FormSaveAs(b,"xfile001","c:\temp\" & name,2) ' Add Mode If fsize <= 0 Then ' when error occurs, return Response.Status other than 2xx ' to cancel UpLoad method. Response.Write "<HTML><BODY>506" & bobj.LastMsg & "</BODY></HTML>" Response.Status = "506 " & bobj.LastMsg Response.End End If %> <HTML><HEAD><TITLE>File Upload with -G option</TITLE> <BODY> fsize= <%= fsize %><BR> fname= <%= fname %><BR> </BODY></HTML>
ASP script's external process name is Mtx.exe on Windows NT 4.0 and DllHost.exe on Windows 2000.
On IIS4, [Execute in IIS process] is the default value.
FormSaveAs ERROR:-6 [IUSR_xxxxx]createfile error c:\xxxx\xxxx 3:Specified path not found. [Reason] Directory name specified in the third parameter of FormSaveAs is wrong. [Workaround] Specify proper directory name in the third parameter of FormSaveAs.
FormSaveAs ERROR:-6 [IUSR_xxxxx]createfile error c:\xxxx\xxxx 5:Access denied. [Reason] IUSR_xxxxx account in the directory specified in the third parameter of FormSaveAs have no access permission. [Workaround] Give write access to the account or Everyone group using security dialog.
FormSaveAs ERROR:-19 [IUSR_xxxxx]invalid arg xxxxx [Reason] The Name of NAME tag(NAME=) in TYPE=FILE and the name specified in FormSaveAs' second parameter is not identical. [Workaround] Specify the proper name of NAME tag in FormSaveAs' second parameter.Error log record's first minus numeric number means BASP21 Pro's error number, next comes meaning(or Win32 API name that returns error), and then system error number (Win32 GetLastError function's return value) and corresponding message.
rc = bobj.DownLoad("https://www.basp21.com/secure/excel.xls","c:\secret","")DownLoad method's SSL encrypted connection is supported in following OS.
Main reasons for not using upload function by browser's HTML form but using BASP21 Pro's Upload method.:
----------------- jupload04.html ------------------------------------------- <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <TITLE>BASP21 UpLoad</TITLE> <BODY onLoad="upload()"> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6" CODEBASE="BASP21P.CAB#version=1,0,3,1"> <PARAM NAME="Env" VALUE="ie3.02"> </OBJECT> <SCRIPT language="JavaScript"> function upload() { var filename = bobj.home + "\\log.lzh"; status = filename; bobj.ShowDialog = 1; var rc = bobj.UpLoad("http://your-server/test/fileup1.asp",filename); status = "Done " + rc; } </SCRIPT> <SCRIPT FOR="bobj" EVENT="OnLog(log)" LANGUAGE="JavaScript"> status = log; </SCRIPT> <SCRIPT FOR="bobj" EVENT="OnProgress(msg,current,total)" LANGUAGE="JavaScript"> status = msg; </SCRIPT> </BODY></HTML>Tips:
----------------- fileup05.asp ------------------------------------------- <% Response.Clear Response.Buffer = True a=Request.TotalBytes ' sum size of POST data b=Request.BinaryRead(a) ' retrieve POST data set bobj=Server.CreateObject("basp21pro") fname=bobj.FormFileName(b,"xfile001") fname=Mid(fname,InstrRev(fname,"\")+1) fsize=bobj.FormSaveAs(b,"xfile001","c:\temp\" & fname) If fsize < 1 Then ' when error occurs, return Response.status other than 2xx to terminate UpLoad method Response.Write "<HTML><BODY>506" & bobj.LastMsg & "</BODY></HTML>" Response.Status = "506 " & bobj.LastMsg Response.End Else msg = fsize End If %> <HTML><HEAD><TITLE>File Upload Test</TITLE> <BODY> file size= <%= msg %><BR> </BODY></HTML>In case you use Upload method, To save all files in directory specify "*" in the second parameter of FormSaveAs method to save all files in directory
----------------- fileup05all.asp ------------------------------------------- <% a=Request.TotalBytes b=Request.BinaryRead(a) set bobj=Server.CreateObject("basp21pro") bobj.Env = "env1:" & Request.ServerVariables("REMOTE_ADDR") dir = bobj.Home & "\fileup2" rc=bobj.FormSaveAs(b,"*",dir) msg=bobj.LastMsg Set bobj = Nothing %> <HTML><HEAD><TITLE>File Upload All</TITLE> <BODY> <H1>Testing</H1> <BR> rc= <%= rc %><BR> msg = <%= msg %> </BODY></HTML>Following is JScript sample. JScript use "\\" for "\" character.
----------------- jfileup05all.asp ------------------------------------------- <%@ LANGUAGE="JavaScript" %> <% var a = Request.TotalBytes; var b = Request.BinaryRead(a); var bobj = Server.CreateObject("basp21pro"); bobj.Env = "env1:JScript " + Request.ServerVariables("REMOTE_ADDR"); var dir = bobj.Home + "\\fileup2"; var rc = bobj.FormSaveAs(b,"*",dir); var msg = bobj.LastMsg; %> <HTML><HEAD><TITLE>File Upload Test</TITLE> <BODY> <H1>Testing</H1> <BR> rc= <%= rc %><BR> msg = <%= msg %> </BODY></HTML>
var files = bobj.home + "\\*.html"; var rc = bobj.UpLoad("ftp://your-ftp-server/dir1",files);Following is the JScript sample explaining how to automatically upload file to FTP server when viewing page in IE.
----------------- fileup06.html ------------------------------------------- <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <TITLE>BASP21 UpLoad</TITLE> <BODY onLoad="upload()"> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6" CODEBASE="BASP21P.CAB#version=1,0,3,1"> <PARAM NAME="Env" VALUE="ie3.02"> </OBJECT> <SCRIPT language="JavaScript"> function upload() { var filename = bobj.home + "\\*.html"; status = filename; bobj.ShowDialog = 1; var rc = bobj.UpLoad("ftp://user1:pass1@your-ftp-server/html",filename); status = "Done " + rc; } </SCRIPT> </BODY></HTML>Specify FTP account name and password with "@" before server name in URL as "ftp://xxxx:yyyy@server". or you can also specify them in 'ftpuserpass' parameter of BASP21P.INI file.
----------------- basp21p.ini ------------------------------------------- [ie3.02] allow=upload,showdialog ftpuserpass=user1:pass1
If the script file extension in upload URL specified in client's UpLoad method is "php" or "php3", It regards the server-side script as PHP and send file's field name as "xfiles[]"(Note:Lower case). PHP interprets "[]" after variable name as alignment.
var rc = bobj.UpLoad("http://your-server/test/fileup2.php",bobj.home);
when upload succeed tag name of field become variable name by PHP'RFC1867 implementation, and it can be access from PHP script.
PHP variable name | description |
$xfiles[n] | temporary file name created when uploading. |
$xfiles_name[n] | original file name |
$xfiles_size[n] | file size |
$xfiles_type[n] | file MIME type, application/octet-stream fixed. |
PHP internally creates temp files to upload when executing script. these files will be automatically deleted after the execution. you need to copy these files in order to save these files.
PHP upload can deal field name using "[]" as array. Variable $xfiles[0] is the first uploaded temp file name, variable $xfiles[1] is the second temp file name. Following is the server-side PHP script sample explaining UpLoad method.
----------------- fileup07.php ------------------------------------------- <HTML><HEAD> <TITLE>UpLoad using PHP3</TITLE></HEAD> <BODY> <?php if (!isset($HTTP_POST_VARS)) { echo "no post data"; } else { $path = '/tmp/upload/'; $max = 999; echo "<table border=1>"; echo "<tr><td>#</td><td>xfile</td><td>size</td></tr>"; for ($i = 0;$i < $max;$i++) { if ($xfiles[$i] == '') break; $j = $i+1; echo "<tr><td>$j</td><td>$xfiles[$i]</td>"; echo "<td>$xfiles_size[$i]</td></tr>"; if (!copy($xfiles[$i],$path.$xfiles_name[$i])) echo "failed to copy $xfiles[$i]<br>\n"; } echo "</table>"; } ?> </BODY> </HTML>
Procedure | Good Point / Bad Point |
Output using Response.BinaryWrite method from server-side. | No need for script in client-side. Supported in IE and Netscape. Browser dependence, Behavior may differ depending on browser's version. Only support ASP for server-side. |
DownLoad method in client. | in-progress status view supported. Japanese code conversion supported. Auto downloading without prompting save dialogue supported. HTTP and FTP protocol supported. non-browser clients also supported (Excel VBA. .etc) At Server-side you can use not only ASP but also Apache, FTP server or etc. BASP21 Pro installation is needed at client-side. Command line( BHELPER.EXE). Only IE is supported for browser. Resume feature supported. |
----------------- download08.asp ------------------------------------------- <% set bobj = Server.CreateObject("basp21pro") bobj.Env ="env1:" & Request.ServerVariables("REMOTE_ADDR") fname = "data1.xls" fpath = bobj.Home & "\" & fname data = bobj.BinaryRead(fpath) If Not bobj.IsError Then Response.Expires = 0 Response.Buffer = TRUE Response.Clear Response.ContentType= "application/x-binary" para = "Attachment; filename=" & """" & fname & """" Response.AddHeader "Content-Disposition",para Response.BinaryWrite data Response.End Else msg = bobj.LastMsg Response.Write "<HTML><BODY>" & vbCrLf Response.Write "<B>DownLoad Error</B><BR>" & msg & vbCrLf Response.Write "</BODY></HTML>" & vbCrLf End If %>If BinaryRead method returns following error (LastMsg property), IUSR_xxxxx account has no access privilege to the file. Right click on the file or directory, and add IUSR_xxxxx account in security dialog, then give read privilege to the account.
ERROR:-6 [IUSR_xxxxx]createfile error c:\test\xxxx.xxx 5:Access denied.
----------------- download09.html ------------------------------------------- <HTML><HEAD> <TITLE>BASP21 Pro Auto Download</TITLE> <BODY onLoad="download()"> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6"> <PARAM NAME="Env" VALUE="env1"> </OBJECT> <SCRIPT language="JavaScript"> function download() { bobj.ShowDialog = 3; bobj.DownLoad("http://basp21.com/data/log.lzh",bobj.home + "\\log.lzh"); MSG.innerText = "Done. rc=" + bobj.Result; } </SCRIPT> <P ID="MSG">DownLoading....</P> </BODY></HTML>Note: innerText property (DHTML) is supported by IE 4.0 or above. IE 3.02 has no DHTML support.
rc = bobj.DownLoad("https://www.basp21.com/secure/excel.xls","c:\secret","")DownLoad method's SSL encrypted connection is supported in following OS.
----------------- download10.html ------------------------------------------- <HTML><HEAD> <TITLE>BASP21 Pro Auto Download</TITLE> <BODY onLoad="download()"> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6"> <PARAM NAME="Env" VALUE="env1"> </OBJECT> <SCRIPT language="JavaScript"> function download() { bobj.ShowDialog = 3; bobj.DownLoad("http://basp21.com/data/logeuc.txt",bobj.home + "\\log.lzh","-j sjis"); MSG.innerText = "Done. rc=" + bobj.Result; } </SCRIPT> <P ID="MSG">DownLoading....</P> </BODY></HTML>
----------------- nodialog01.html ------------------------------------------- <HTML><HEAD> <TITLE>BASP21 Pro Auto Download With Event</TITLE> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <BODY bgcolor=#000000 TEXT=#ffffff> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6"> <PARAM NAME="Env" VALUE="env1"> </OBJECT> <SCRIPT LANGUAGE="VBScript"> Dim bar,barsize Sub Window_OnLoad() bar = 0: barsize = 30 ' unit of Progress Bar bobj.FireEvent = 1 bobj.DownLoad "http://www.basp21.com/data/log.lzh",bobj.home rc = bobj.Result If rc = -2 Then PROGRESSBAR.innerText = "canceled. " Else PROGRESSBAR.innerText = rc End If End Sub Sub bobj_OnLog(log) status = log End Sub Sub bobj_OnProgress(msg,total,current) Dim line,wk,per,i,j line = Split(msg,vbCrLf) i = InStr(line(2),"%") wk = Mid(line(2),i-2,2) per = wk / (100 / barsize) per = Int(per) If bar <> per Then bar = per progress = "" For i = 0 To bar -1 progress = progress & "**" Next For j = i To barsize -1 progress = progress & "--" Next PROGRESSBAR.innerText = progress End If MSG0.innerText = line(0) MSG1.innerText = line(1) MSG2.innerText = line(2) MSG3.innerText = line(3) End Sub Sub cbtn_OnClick() bobj.Cancel() End Sub </SCRIPT> <P ALIGN=CENTER> <INPUT type="button" NAME="cbtn" value="Cancel"> </P> <P ID="MSG0"></P> <P ID="MSG1"></P> <P ID="MSG2"></P> <P ID="MSG3"></P> <P ID="PROGRESSBAR"></P> </BODY></HTML>JScript sample.
----------------- jnodialog01.html ------------------------------------------- <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=Shift_JIS"> <TITLE>BASP21 Pro Auto Download With Event</TITLE> <BODY onLoad="download()" bgcolor=#000000 TEXT=#ffffff> <OBJECT ID="bobj" CLASSID="CLSID:EC0C18A0-4D5E-11D4-896C-00000E4E0AD6"> <PARAM NAME="Env" VALUE="env1"> </OBJECT> <SCRIPT LANGUAGE="JavaScript"> var bar,barsize; function download() { bar = 0; barsize = 20; // unit of Progress Bar bobj.FireEvent = 1; bobj.DownLoad("http://www.basp21.com/data/log.lzh",bobj.home); var rc = bobj.Result; if (rc == -2) PROGRESSBAR.innerText = "canceled. "; else PROGRESSBAR.innerText = rc; } </SCRIPT> <SCRIPT FOR="bobj" EVENT="OnLog(log)" LANGUAGE="JavaScript"> status = log; </SCRIPT> <SCRIPT FOR="bobj" EVENT="OnProgress(msg,total,current)" LANGUAGE="JavaScript"> var line,wk,per,i,j; line = msg.split(unescape('%0D%0A')); i = line[2].indexOf("%"); wk = line[2].substr(i-2,2); per = wk / (100 / barsize); per = Math.ceil(per); if (bar != per) { bar = per; var progress = ""; for (i = 0;i < bar;i++) progress = progress + "**"; for (j = i;j < barsize;j++) progress = progress + "--"; PROGRESSBAR.innerText = progress; } MSG0.innerText = line[0]; MSG1.innerText = line[1]; MSG2.innerText = line[2]; MSG3.innerText = line[3]; </SCRIPT> <P ALIGN=CENTER> <INPUT type="button" value="Cancel" onclick="bobj.Cancel()"> </P> <P ID="MSG0"></P> <P ID="MSG1"></P> <P ID="MSG2"></P> <P ID="MSG3"></P> <P ID="PROGRESSBAR"></P> </BODY></HTML>