大家经常探讨使用asp,而不使用其他组建能否实现文件的上传,从而开发出支持邮件附件的邮件系统,答案是可以的。请看:
以下是发送邮件的页面,邮件的帐号是员工号,假设是5位的数字,sendmail.asp当然是在合法登陆后才能够看到的
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="/css/forum.css">
<style type=text/css>
<!–
input { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
select { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
textarea { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
–>
</style>
<title>邮件系统</title></head>
<body bgcolor="#fef7ed">
<script language="javascript">
<%
if session("myid")="" or len(session("myid"))<>5 then
response.write "window.open(nolog.asp,target=_top);"
end if
%>
function check(theform)
{
if (theform.geterempl.value==)
{
alert(请输入收件人!);
theform.geterempl.focus();
return false;
}
if (theform.emailtitle.value==)
{
alert(请输入主题!);
theform.emailtitle.focus();
return false;
}
if (theform.emailtitle.value.length>200)
{
alert(主题请少于200字节);
theform.emailtitle.focus();
return false;
}
if (theform.body.value.length>15*1024)
{
alert(正文请少于16k);
theform.body.focus();
return false;
}
if (theform.emailshowname.value.length>1024)
{
alert(签名请少于1k);
theform.emailshowname.focus();
return false;
}
}
</script>
<%
meth=request.querystring("meth")
if meth=1 then
geterempl=trim(request.querystring("geterempl"))
emailtitle=trim(request.querystring("emailtitle"))
elseif meth=2 then
mailid=trim(request.querystring("mailid"))
set conn=server.createobject("adodb.connection")
conn.open "dsn=;uid=;pwd="
dsnpath="dsn=;uid=;pwd="
set rs=server.createobject("adodb.recordset")
selectnew="select * from t_mail where ((geterempl like %"&session("myid")&"% or deleempl like %"&session("myid")&"% or receempl like %"&session("myid")&"%)and (not deleverempl like %"&session("myid")&"%)) and mailid="&mailid&" "
rs.open selectnew,dsnpath,3,3
if rs.bof or rs.eof then
%>
<script language="javascript">
alert("您没有查看这封邮件的权限!");
window.history.back();
</script>
<%
response.end
else
body=rs("body")
emailtitle=rs("emailtitle")
rs.close
set rs=nothing
conn.close
set conn=nothing
end if
end if
%>
<form name="upload_file" onsubmit="return check(this)" action="loadmail.asp" method=post enctype="multipart/form-data" >
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="11%">
<div align="right">发件人:</div>
</td>
<td width="89%">
<input type="hidden" name="senderempl" value="<%=session("myid")%>">
<%=session("myid")%> </td>
</tr>
<tr>
<td width="11%">
<div align="right">收件人:</div>
</td>
<td width="89%">
<input type="text" name="geterempl" size="40" value="<%=geterempl%>">
<input type="checkbox" name="emaillevel" value="1" style="background-color: #fef7ed">
紧急信件 </td>
</tr>
<tr>
<td width="11%" valign="top"> </td>
<td width="89%">发送多个人的时候可以使用"<font color="#9999ff">|</font>"隔开,例如:<font color="#3399ff">01234|01235|01236</font>,第一位和最后一位不需要"<font color="#9999ff">|</font>"<br>
<font color="#ff0000">新功能</font>:您可以把信信直接发送给您设定的<a href="group.asp">某用户</a>,发送格式为:gr:组序号,例如<font color="#0099ff">gr:001</font></td>
</tr>
<tr>
<td width="11%">
<div align="right"></div>
</td>
<td width="89%">
<input type="checkbox" name="receempl" value="1" style="background-color: #fef7ed">
保存一份到收藏夹[<font color="#3399ff">选定此项,则邮件发送到对方邮箱的同时发送到自己的收藏夹里</font>]</td>
</tr>
<tr>
<td width="11%" valign="top"> </td>
<td width="89%"> </td>
</tr>
<tr>
<td width="11%" align="right"> 主题:</td>
<td width="89%">
<input type="text" name="emailtitle" size="60" value="<%=emailtitle%>">
</td>
</tr>
<tr>
<td width="11%" valign="top">
<div align="right">正文:</div>
</td>
<td width="89%">
<textarea name=body rows=8 cols=60><%=body%></textarea>
</td>
</tr>
<tr>
<td width="11%" valign="top">
<div align="right">签名:</div>
</td>
<td width="89%">
<textarea name="emailshowname" cols="30" rows="6"><%=application(session("myid")&"_name")%></textarea>
</td>
</tr>
<tr>
<td width="11%">
<div align="right">
<input type=hidden name="fileuploadstart">
附件1: </div>
</td>
<td width="89%">
<input type="file" name="file_up" size="50">
</td>
</tr>
<tr>
<td width="11%">
<div align="right">附件2:</div>
</td>
<td width="89%">
<input type="file" name="file_up1" size="50">
</td>
</tr>
<tr>
<td width="11%">
<div align="right">附件3:</div>
</td>
<td width="89%">
<input type="file" name="file_up2" size="50">
<input type=hidden name="fileuploadend">
</td>
</tr>
<tr>
<td width="11%">
<div align="right"></div>
</td>
<td width="89%">
<input type=submit value=确定 >
</td>
</tr>
</table>
</form>
</body>
</html>
这次讲到的是处理发送的页面,前一部分是得到发送者ip地址和mac地址,并且禁止用户自己更改自己ip地址的代码,因为我们的系统是需要对个人修改ip的行为进行禁止的。
<%
strip = request.servervariables("remote_addr")
set net = server.createobject("wscript.network")
set sh = server.createobject("wscript.shell")
sh.run "%comspec% /c nbtstat -a " & strip & " > c:\" & strip & ".txt",0,true
set sh = nothing
set fso = createobject("scripting.filesystemobject")
set ts = fso.opentextfile("c:\" & strip & ".txt")
macaddress = null
do while not ts.atendofstream
data = ucase(trim(ts.readline))
if instr(data,"mac address") then
macaddress = trim(split(data,"=")(1))
exit do
end if
loop
ts.close
set ts = nothing
fso.deletefile "c:\" & strip & ".txt"
set fso = nothing
getmacaddress = macaddress
strmac = getmacaddress
set conn=server.createobject("adodb.connection")
conn.open "dsn=;uid=;pwd="
dsnpath="dsn=;uid=;pwd="
set rs=server.createobject("adodb.recordset")
sele="select * from getmac where g_mac="&strmac&""
rs.open sele,dsnpath
if rs.bof then
set conn=server.createobject("adodb.connection")
conn.open "dsn=;uid=;pwd="
dsnpath="dsn=;uid=;pwd="
set rs=server.createobject("adodb.recordset")
g_id=mid(strip,9)
g_id=left(g_id,2)
response.write g_id
if isnumeric(g_id) then
g_id=cint(g_id)
else
g_id=0
end if
sele="insert into getmac(g_ip,g_mac,g_id,g_ok) values("&strip&","&strmac&","&g_id&",0)"
rs.open sele,dsnpath
else
set conn=server.createobject("adodb.connection")
conn.open "dsn=;uid=;pwd="
dsnpath="dsn=;uid=;pwd="
set rs=server.createobject("adodb.recordset")
sele="select * from getmac where g_ip="&trim(strip)&" and g_mac="&trim(strmac)&""
rs.open sele,dsnpath
if rs.bof or rs.eof then
set rs1=server.createobject("adodb.recordset")
sele="insert into badmac(ip, mac ,thetime) values("&strip&","&strmac&","&now()&")"
rs1.open sele,dsnpath
response.redirect("/reg/wrong.asp")
response.end
end if
end if
%>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css/forum.css">
<style type=text/css>
<!–
input { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
select { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
textarea { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}
–>
</style>
<title>邮件系统</title></head><body bgcolor="#fef7ed">
<%
response.expires=0
function bin2str(binstr)
dim varlen,clow,ccc,skipflag
skipflag=0
ccc = ""
if not isnull(binstr) then
varlen=lenb(binstr)
for i=1 to varlen
if skipflag=0 then
clow = midb(binstr,i,1)
if ascb(clow) > 127 then
ccc =ccc & chr(ascw(midb(binstr,i+1,1) & clow))
skipflag=1
else
ccc = ccc & chr(ascb(clow))
end if
else
skipflag=0
end if
next
end if
bin2str = ccc
end function
varbytecount = request.totalbytes
response.write varbytecount
bncrlf = chrb( 13 ) & chrb( 10 )
binhttpheader=request.binaryread(varbytecount)
response.write vbenter
response.write "<br><br>"& cstr(binhttpheader) &"<br><br>"
sread=0
eread=0
开始读非文件域的数据
set conn = server.createobject("adodb.connection")
conn.open "dsn=;uid=;pwd="
sql="select * from t_mail where mailid=0"
set rs=server.createobject("adodb.recordset")
rs.open sql,conn,3,3
rs.addnew
rs("emaillevel")=0
rs("receempl")=""
do while lenb(binhttpheader)>46
divider = leftb( binhttpheader, instrb( binhttpheader, bncrlf ) – 1 )
binheaderdata = leftb(binhttpheader, instrb( binhttpheader, bncrlf & bncrlf )-1)
strheaderdata=bin2str(binheaderdata)
lngfieldnamestart=instr(strheaderdata,"name="&chr(34))+len("name="&chr(34))
response.write "<br>lngfieldnamestart:"&lngfieldnamestart
lngfieldnameend=instr(lngfieldnamestart,strheaderdata,chr(34))
response.write "<br>lngfieldnameend:"&lngfieldnameend
strfieldname=mid(strheaderdata,lngfieldnamestart,lngfieldnameend-lngfieldnamestart)
response.write "<br>strfieldname:" & strfieldname
strfieldname=trim(strfieldname)
strfieldname=replace(strfieldname,vbcrlf,vbnullstring)
判断文件数据时候开始
if strcomp(strfieldname,"fileuploadstart",1)=0 and sread=0 then
response.write "找到了文件开始的地方"
sread=1
response.write "<br>" & instrb( datastart + 1, binhttpheader, divider ) &"<br>"
binhttpheader=midb(binhttpheader,instrb( datastart + 1, binhttpheader, divider ))
exit do
end if
datastart = instrb( binhttpheader, bncrlf & bncrlf ) + 4
dataend = instrb( datastart + 1, binhttpheader, divider ) – datastart
binfieldvalue=midb( binhttpheader, datastart, dataend )
strfieldvalue=bin2str(binfieldvalue)
strfieldvalue=trim(strfieldvalue)
strfieldvalue=replace(strfieldvalue," "," ")
非文件上传域变量赋值
execute strfieldname&"="""&strfieldvalue&""""
response.write strfieldname&":"&strfieldvalue&"<br>"
if strfieldname="geterempl" then
strfieldvalue=replace(strfieldvalue,vbcrlf,vbnullstring)
if instr(strfieldvalue,"gr:")=1 then
邮件组发
response.write len(trim(strfieldvalue))
if len(trim(strfieldvalue))<>6 then
格式错误返回
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件组格式错误!\r正确的格式是:gr:001");
history.back();
</script>
<p>
<%
response.end
else
if not isnumeric(mid(trim(strfieldvalue),4)) then
格式错误返回
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件组格式错误!\r正确的格式是:gr:001");
history.back();
</script>
<p>
<%
response.end
else
thegroup=(mid(trim(strfieldvalue),4))
end if
end if
tmpsql="select * from t_group where owner="&session("myid")&" and groupidowner="&thegroup&""
response.write tmpsql
set tmprs=server.createobject("adodb.recordset")
tmprs.open tmpsql,conn
if tmprs.bof or tmprs.eof then
没有找到该组
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件组<%=thegroup%>没有找到!");
history.back();
</script>
<p>
<%
response.end
else
if tmprs("personnum")=0 then
组内没有用户
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件组<%=thegroup%>中目前没有任何的用户\n所以不能发送");
history.back();
</script>
<p>
<%
response.end
else
strfieldvalue=trim(tmprs("groupempl"))
tmprs.close
set tmprs=nothing
end if
end if
end if
if instr(strfieldvalue,"|") then
组发
allsearch=replace(trim(strfieldvalue),"|",",")
allsearch=""&allsearch&""
tmpstring=trim(strfieldvalue)&"|"
tosearch=""
do while len(tmpstring)>=5
tosearch=left(tmpstring,5)
tmpstring=mid(tmpstring,7)
if instr(tosearch,"|") then
格式错误
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件人格式错误!");
history.back();
</script>
<p>
<%
response.end
end if
tmpsql="select * from (select userid from t_officer where userid in ("&allsearch&")) derivedtbl where userid="&tosearch&""
response.write tmpsql
set tmprs=server.createobject("adodb.recordset")
tmprs.open tmpsql,conn
if tmprs.eof or tmprs.bof then
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件人<%=tosearch%>没有找到!");
history.back();
</script>
<p>
<%
response.end
end if
tmprs.close
set tmprs=nothing
loop
strfieldvalue=trim(strfieldvalue)
else
if len(trim(strfieldvalue))<>5 then
格式不正确
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件人<%=trim(strfieldvalue)%>不正确!");
history.back();
</script>
<p>
<%
response.end
else
if isnumeric(trim(len(strfieldvalue))) then
tmpsql="select * from t_officer where userid="&trim(strfieldvalue)&""
set tmprs=server.createobject("adodb.recordset")
tmprs.open tmpsql,conn
if tmprs.eof or tmprs.bof then
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件人<%=trim(strfieldvalue)%>没有找到\r该员工可能还没有注册!");
history.back();
</script>
<p>
<%
response.end
end if
tmprs.close
set tmprs=nothing
strfieldvalue=trim(strfieldvalue)
else
%>
尝试发送邮件,但是失败了,请修改错误后重试!
<script language="javascript">
alert("您输入的收件人<%=trim(strfieldvalue)%>不正确!");
history.back();
</script>
<p> <%
response.end
end if
end if
end if
end if
strfieldvalue=replace(strfieldvalue,"<","<")
response.write strfieldname
rs(strfieldname)=replace(strfieldvalue,">",">")
binhttpheader=midb(binhttpheader,instrb( datastart + 1, binhttpheader, divider ))
loop
开始处理文件数据
titem=0
rs("filesize_1")=0
rs("filesize_2")=0
rs("filesize_3")=0
do while lenb(binhttpheader)>46
if instrb( binhttpheader, bncrlf & bncrlf )<>0 then
binheaderdata = leftb(binhttpheader,instrb( binhttpheader, bncrlf & bncrlf )-1)
else
exit do
end if
strheaderdata=bin2str(binheaderdata)
读取上传文件的content-type
lngfilecontenttypestart=instr(strheaderdata,"content-type:")+len("content-type:")
strfilecontenttype=trim(mid(strheaderdata,lngfilecontenttypestart))
strfilecontenttype=replace(strfilecontenttype,vbcrlf,vbnullstring)
读取上传的文件名
if instr(strheaderdata,"filename=")>0 then
lngfilenamestart=instr(strheaderdata,"filename="&chr(34))+len("filename="&chr(34))
lngfilenameend=instr(lngfilenamestart,strheaderdata,chr(34))
strfilename=mid(strheaderdata,lngfilenamestart,lngfilenameend-lngfilenamestart)
strfilename=trim(strfilename)
strfilename=replace(strfilename,vbcrlf,vbnullstring)
else
strfilename=""
end if
读取上传文件数据
datastart = instrb( binhttpheader, bncrlf & bncrlf ) + 4
dataend = instrb( datastart + 1, binhttpheader, divider ) – datastart
if strfilename<>"" then
if dataend>0 then
binfieldvalue=midb( binhttpheader, datastart, dataend )
将上传的文件写入数据库
titem=titem+1
response.write "titem:"&titem
rs("filecontenttype_"&titem)=strfilecontenttype
rs("filecontent_"&titem).appendchunk binfieldvalue
rs("filesize_"&titem)=lenb(binfieldvalue)
rs("filename_"&titem)=strfilename
else
binfieldvalue=binhttpheader
end if
end if
if instrb( datastart + 1, binhttpheader, divider )>0 then
binhttpheader=midb(binhttpheader,instrb( datastart + 1, binhttpheader, divider ))
else
binhttpheader=""
end if
loop
rs("sizetotal")=csng(rs("filesize_1"))+csng(rs("filesize_2"))+csng(rs("filesize_3"))+csng(len(rs("body")))+csng(len(rs("emailtitle")))+csng(len(rs("emailshowname")))+csng(len("geterempl"))
if csng(rs("sizetotal"))>=csng(2*1024*1024) then
response.write "对不起,文件太大,请保证每封邮件的总大小不超过2m!"
response.end
end if
rs("mailtime")=now
rs("readerempl")=""
if rs("receempl")<>"" then
rs("receempl")=session("myid")
rs("readerempl")=session("myid")
end if
rs("deleempl")=""
rs("deleverempl")=""
rs("sendmac")=strmac
rs.update
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
<script language=javascript>
window.open("mailok.asp",target="_self")
</script>
</body></html>
最后,我们来讲讲如何把内容从数据库中读出来,内容有这么几类,一类是浏览器上可以显示的,例如*.htm,一类是需要下载的,例如*.exe,还有一种是浏览器可以显示但是不能够让他显示的,例如*.asp,请看代码:
<%
response.buffer= true
response.clear
function getname(oriname)
thename=oriname
do while instr(thename,"/")>0
thename=mid(thename,instr(thename,"/")+1)
loop
do while instr(thename,"\")>0
thename=mid(thename,instr(thename,"\")+1)
loop
getname=thename
end function
function canexec(thechar)
if instr(thechar,".asp")>0 then
canexec=false
exit function
end if
if instr(thechar,".asa")>0 then
canexec=false
exit function
end if
if instr(thechar,".aspx")>0 then
canexec=false
exit function
end if
if instr(thechar,".asax")>0 then
canexec=false
exit function
end if
canexec=true
end function
mailid=request("mailid")
se=request("se")
if se<>1 and se<>2 and se<>3 then
response.end
end if
set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "dsn=;uid=;pwd="
sql="select * from t_mail where ((geterempl like %"&session("myid")&"% or deleempl like %"&session("myid")&"% or receempl like %"&session("myid")&"% ) and (not deleverempl like %"&session("myid")&"%)) and mailid="&mailid&" "
rs.open sql,conn,3,3
if rs.eof or rs.bof then
response.end
end if
if rs("filecontenttype_"&trim(se))<>"text/plain" or (not canexec(getname(trim(rs("filename_"&trim(se)))))) then
response.contenttype = rs("filecontenttype_"&trim(se))
end if
response.addheader "content-type","application/x-msdownload"
if instr(response.contenttype,"application")>0 then
response.addheader "content-disposition","attachment;filename="&getname(trim(rs("filename_"&trim(se))))
end if
response.binarywrite rs("filecontent_"&trim(se))
rs.close
set rs=nothing
conn.close
set conn=nothing
%>