<p> 大家经常探讨使用asp,而不使用其他组建能否实现文件的上传,从而开发出支持邮件附件的邮件系统,答案是可以的。</p><p> 以下是发送邮件的页面,邮件的帐号是员工号,假设是5位的数字,sendmail.asp当然是在合法登陆后才能够看到的</p><p> <code><html><br /><head><br /><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br /><link rel="stylesheet" type="text/css" href="/css/FORUM.CSS"><br /><style type=text/css><br /><!--<br />input { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />select { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />textarea { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />--><br /></style><br /><title>邮件系统</title></head><br /><body bgcolor="#FEF7ED"><br /><script language="javascript"><br /><%<br />if session("myid")="" or len(session("myid"))<>5 then<br />response.write "window.open('nolog.asp',target='_top');"<br />end if<br />%><br />function check(theform)<br />{<br />if (theform.geterempl.value=='')<br />{<br />alert('请输入收件人!');<br />theform.geterempl.focus();<br />return false;<br />}<br />if (theform.emailtitle.value=='')<br />{<br />alert('请输入主题!');<br />theform.emailtitle.focus();<br />return false;<br />}<br />if (theform.emailtitle.value.length>200)<br />{<br />alert('主题请少于200字节');<br />theform.emailtitle.focus();<br />return false;<br />}<br />if (theform.body.value.length>15*1024)<br />{<br />alert('正文请少于16K');<br />theform.body.focus();<br />return false;<br />}<br />if (theform.emailshowname.value.length>1024)<br />{<br />alert('签名请少于1K');<br />theform.emailshowname.focus();<br />return false;<br />}<br />}<br /></script><br /><%<br />meth=request.querystring("meth")<br />if meth=1 then<br />geterempl=trim(request.querystring("geterempl"))<br />emailtitle=trim(request.querystring("emailtitle"))<br />elseif meth=2 then<br />mailid=trim(request.querystring("mailid"))<br />set conn=server.createobject("adodb.connection")<br />conn.open "DSN=;UID=;PWD="<br />dsnpath="DSN=;UID=;PWD="<br />set rs=server.createobject("adodb.recordset")<br />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&"' "<br />rs.open selectnew,dsnpath,3,3<br />if rs.bof or rs.eof then<br />%><br /><script language="javascript"><br />alert("您没有查看这封邮件的权限!");<br />window.history.back();<br /></script><br /><%<br />response.end<br />else<br />body=rs("body")<br />emailtitle=rs("emailtitle")<br />rs.close<br />set rs=nothing<br />conn.close<br />set conn=nothing<br />end if<br />end if<br />%><br /><Form name="upload_file" onSubmit="return check(this)" action="loadmail.asp" method=post enctype="multipart/form-data" ><br /><table width="100%" border="0" cellspacing="2" cellpadding="2"><br /><tr><br /><td width="11%"><br /><div align="right">发件人:</div><br /></td><br /><td width="89%"><br /><input type="hidden" name="senderempl" value="<%=session("myid")%>"><br /><%=session("myid")%> </td><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right">收件人:</div><br /></td><br /><td width="89%"><br /><input type="text" name="geterempl" size="40" value="<%=geterempl%>"><br /><input type="checkbox" name="emaillevel" value="1" style="background-color: #FEF7ED"><br />紧急信件 </td><br /></tr><br /><tr><br /><td width="11%" valign="top"> </td><br /><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><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right"></div><br /></td><br /><td width="89%"><br /><input type="checkbox" name="receempl" value="1" style="background-color: #FEF7ED"><br />保存一份到收藏夹[<font color="#3399FF">选定此项,则邮件发送到对方邮箱的同时发送到自己的收藏夹里</font>]</td><br /></tr><br /><tr><br /><td width="11%" valign="top"> </td><br /><td width="89%"> </td><br /></tr><br /><tr><br /><td width="11%" align="right"> 主题:</td><br /><td width="89%"><br /><input type="text" name="emailtitle" size="60" value="<%=emailtitle%>"><br /></td><br /></tr><br /><tr><br /><td width="11%" valign="top"><br /><div align="right">正文:</div><br /></td><br /><td width="89%"><br /><TEXTAREA name=body rows=8 cols=60><%=body%></TEXTAREA><br /></td><br /></tr><br /><tr><br /><td width="11%" valign="top"><br /><div align="right">签名:</div><br /></td><br /><td width="89%"><br /><textarea name="emailshowname" cols="30" rows="6"><%=application(session("myid")&"_name")%></textarea><br /></td><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right"><br /><input type=hidden name="FileUploadStart"><br />附件1: </div><br /></td><br /><td width="89%"><br /><input type="file" name="file_up" size="50"><br /></td><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right">附件2:</div><br /></td><br /><td width="89%"><br /><input type="file" name="file_up1" size="50"><br /></td><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right">附件3:</div><br /></td><br /><td width="89%"><br /><input type="file" name="file_up2" size="50"><br /><input type=hidden name="FileUploadEnd"><br /></td><br /></tr><br /><tr><br /><td width="11%"><br /><div align="right"></div><br /></td><br /><td width="89%"><br /><input type=submit value=确定 ><br /></td><br /></tr><br /></table><br /></Form><br /></body><br /></html><br />不过这仅仅只是得到了发送者的ip地址和mac地址,而且禁止用户自己更改自己ip地址的代码,因为我们的系统是需要对个人修改ip的行为进行禁止的。<br /><%<br />strIP = Request.ServerVariables("REMOTE_ADDR")<br />Set net = Server.CreateObject("wscript.network")<br />Set sh = Server.CreateObject("wscript.shell")<br />sh.run "%comspec% /c nbtstat -A " & strIP & " > c:\" & strIP & ".txt",0,true<br />Set sh = nothing<br />Set fso = createobject("scripting.filesystemobject")<br />Set ts = fso.opentextfile("c:\" & strIP & ".txt")<br />macaddress = null<br />Do While Not ts.AtEndOfStream<br />data = ucase(trim(ts.readline))<br />If instr(data,"MAC ADDRESS") Then<br />macaddress = trim(split(data,"=")(1))<br />Exit Do<br />End If<br />loop<br />ts.close<br />Set ts = nothing<br />fso.deletefile "c:\" & strIP & ".txt"<br />Set fso = nothing<br />GetMACAddress = macaddress<br />strMac = GetMACAddress<br />set conn=server.CreateObject("adodb.connection")<br />conn.open "DSN=;UID=;PWD="<br />dsnpath="DSN=;UID=;PWD="<br />set rs=server.CreateObject("adodb.recordset")<br />sele="select * from getmac where g_mac='"&strMac&"'"<br />rs.open sele,dsnpath<br />if rs.bof then<br />set conn=server.CreateObject("adodb.connection")<br />conn.open "DSN=;UID=;PWD="<br />dsnpath="DSN=;UID=;PWD="<br />set rs=server.CreateObject("adodb.recordset")<br />g_id=mid(strIP,9)<br />g_id=left(g_id,2)<br />'response.write g_id<br />if isnumeric(g_id) then<br />g_id=cint(g_id)<br />else<br />g_id=0<br />end if<br />sele="insert into getmac(g_ip,g_mac,g_id,g_ok) values('"&strIP&"','"&strMac&"',"&g_id&",0)"<br />rs.open sele,dsnpath<br />else<br />set conn=server.CreateObject("adodb.connection")<br />conn.open "DSN=;UID=;PWD="<br />dsnpath="DSN=;UID=;PWD="<br />set rs=server.CreateObject("adodb.recordset")<br />sele="select * from getmac where g_ip='"&trim(strIP)&"' and g_mac='"&trim(strMac)&"'"<br />rs.open sele,dsnpath<br />if rs.bof or rs.eof then<br />set rs1=server.CreateObject("adodb.recordset")<br />sele="insert into badmac(ip, mac ,thetime) values('"&strIP&"','"&strMac&"','"&now()&"')"<br />rs1.open sele,dsnpath<br />response.redirect("/reg/wrong.asp")<br />response.end<br />end if<br />end if<br />%><br /><html><br /><head><br /><link rel="stylesheet" type="text/css" href="/css/FORUM.CSS"><br /><style type=text/css><br /><!--<br />input { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />select { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />textarea { font-size: 9pt; color: #0011dd; background-color: #e9e9f9; padding-top: 0px}<br />--><br /></style><br /><title>邮件系统</title></head><body bgcolor="#FEF7ED"><br /><%<br />Response.Expires=0<br />Function bin2str(binstr)<br />Dim varlen,clow,ccc,skipflag<br />skipflag=0<br />ccc = ""<br />If Not IsNull(binstr) Then<br />varlen=LenB(binstr)<br />For i=1 To varlen<br />If skipflag=0 Then<br />clow = MidB(binstr,i,1)<br />If AscB(clow) > 127 Then<br />ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))<br />skipflag=1<br />Else<br />ccc = ccc & Chr(AscB(clow))<br />End If<br />Else<br />skipflag=0<br />End If<br />Next<br />End If<br />bin2str = ccc<br />End Function<br />varByteCount = Request.TotalBytes<br />'response.write varbytecount<br />bnCRLF = chrB( 13 ) & chrB( 10 )<br />binHTTPHeader=Request.BinaryRead(varByteCount)<br />'response.write vbenter<br />'response.write "<br />"& cstr(binhttpheader) &"<br />"<br />sread=0<br />eread=0<br />'开始读非文件域的数据<br />set conn = Server.CreateObject("ADODB.Connection")<br />conn.open "DSN=;UID=;PWD="<br />SQL="select * from t_mail where mailid=0"<br />set rs=server.CreateObject("ADODB.Recordset")<br />rs.Open sql,conn,3,3<br />rs.addnew<br />rs("emaillevel")=0<br />rs("receempl")=""<br />Do while lenB(binHTTPHeader)>46<br />Divider = LEFTb( binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF ) - 1 )<br />binHeaderData = Leftb(binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)<br />strHeaderData=bin2str(binHeaderData)<br />lngFieldNameStart=Instr(strHeaderData,"name="&chr(34))+Len("name="&chr(34))<br />'response.write "<br />lngfieldnamestart:"&lngfieldnamestart<br />lngFieldNameEnd=Instr(lngFieldNameStart,strHeaderData,chr(34))<br />'response.write "<br />lngfieldnameEND:"&lngfieldnameEND<br />strFieldName=Mid(strHeaderData,lngFieldNameStart,lngFieldNameEnd-lngFieldNameStart)<br />'RESPOnSE.WRITE "<BR>STRFIELDNAME:" & STRfieldname<br />strFieldName=Trim(strFieldName)<br />strFieldName=Replace(strFieldName,vbcrlf,vbnullstring)<br />'判断文件数据时候开始<br />If strComp(strFieldName,"FileUploadStart",1)=0 and sread=0 Then<br />'response.write "找到了文件开始的地方"<br />sread=1<br />'response.write "<br />" & INSTRB( DataStart + 1, binHTTPHeader, divider ) &"<br />"<br />binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))<br />exit do<br />End if<br />DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4<br />DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStart<br />binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )<br />strFieldValue=bin2str(binFieldValue)<br />'strFieldValue=Trim(strFieldValue)<br />strFieldValue=Replace(strFieldValue," "," ")<br />'非文件上传域变量赋值<br />'execute strFieldName&"="""&strFieldValue&""""<br />'response.write strFieldName&":"&strFieldValue&"<br />"<br />if strfieldname="geterempl" then<br />strFieldValue=Replace(strFieldValue,vbcrlf,vbnullstring)<br />if instr(strfieldvalue,"gr:")=1 then<br />'邮件组发<br />'response.write len(trim(strfieldvalue))<br />if len(trim(strfieldvalue))<>6 then<br />'格式错误返回<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件组格式错误!\r正确的格式是:'gr:001'");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />else<br />if not isnumeric(mid(trim(strfieldvalue),4)) then<br />'格式错误返回<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件组格式错误!\r正确的格式是:'gr:001'");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />else<br />thegroup=(mid(trim(strfieldvalue),4))<br />end if<br />end if<br />tmpSQL="select * from t_group where owner='"&session("myid")&"' and groupidowner='"&thegroup&"'"<br />'response.write tmpsql<br />set tmprs=server.CreateObject("ADODB.Recordset")<br />tmprs.Open tmpsql,conn<br />if tmprs.bof or tmprs.eof then<br />'没有找到该组<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件组<%=thegroup%>没有找到!");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />else<br />if tmprs("personnum")=0 then<br />'组内没有用户<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件组<%=thegroup%>中目前没有任何的用户\n所以不能发送");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />else<br />strFieldValue=trim(tmprs("groupempl"))<br />tmprs.close<br />set tmprs=nothing<br />end if<br />end if<br />end if<br />if instr(strfieldValue,"|") then<br />'组发<br />allsearch=replace(trim(strfieldValue),"|","','")<br />allsearch="'"&allsearch&"'"<br />tmpstring=trim(strfieldValue)&"|"<br />tosearch=""<br />do while len(tmpstring)>=5<br />tosearch=left(tmpstring,5)<br />tmpstring=mid(tmpstring,7)<br />if instr(tosearch,"|") then<br />'格式错误<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件人格式错误!");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />end if<br />tmpSQL="select * from (select userid from t_officer where userid in ("&allsearch&")) DERIVEDTBL where userid='"&tosearch&"'"<br />'response.write tmpsql<br />set tmprs=server.CreateObject("ADODB.Recordset")<br />tmprs.Open tmpsql,conn<br />if tmprs.eof or tmprs.bof then<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件人<%=tosearch%>没有找到!");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />end if<br />tmprs.close<br />set tmprs=nothing<br />loop<br />strfieldValue=trim(strFieldValue)<br />else<br />if len(trim(strFieldValue))<>5 then<br />'格式不正确<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件人<%=trim(strFieldValue)%>不正确!");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />else<br />if isnumeric(trim(len(strFieldValue))) then<br />tmpSQL="select * from t_officer where userid='"&trim(strFieldValue)&"'"<br />set tmprs=server.CreateObject("ADODB.Recordset")<br />tmprs.Open tmpsql,conn<br />if tmprs.eof or tmprs.bof then<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件人<%=trim(strFieldValue)%>没有找到\r该员工可能还没有注册!");<br />history.back();<br /></script><br /><p><br /><%<br />response.end<br />end if<br />tmprs.close<br />set tmprs=nothing<br />strfieldValue=trim(strFieldValue)<br />else<br />%><br />尝试发送邮件,但是失败了,请修改错误后重试!<br /><script language="javascript"><br />alert("您输入的收件人<%=trim(strFieldValue)%>不正确!");<br />history.back();<br /></script><br /><p> <%<br />response.end<br />end if<br />end if<br />end if<br />end if<br />strFieldValue=replace(strFieldValue,"<","<")<br />'response.write strfieldname<br />rs(STRFIELDNAME)=replace(strFieldValue,">",">")<br />binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))<br />loop<br />'开始处理文件数据<br />titem=0<br />rs("filesize_1")=0<br />rs("filesize_2")=0<br />rs("filesize_3")=0<br />Do while lenB(binHTTPHeader)>46<br />if INSTRB( binHTTPHeader, bnCRLF & bnCRLF )<>0 then<br />binHeaderData = LeftB(binHTTPHeader,INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)<br />else<br />exit do<br />end if<br />strHeaderData=bin2str(binHeaderData)<br />'读取上传文件的Content-Type<br />lngFileContentTypeStart=Instr(strHeaderData,"Content-Type:")+Len("Content-Type:")<br />strFileContentType=Trim(Mid(strHeaderData,lngFileContentTypeStart))<br />strFileContentType=Replace(strFileContentType,vbCRLF,vbNullString)<br />'读取上传的文件名<br />if instr(strheaderdata,"filename=")>0 then<br />lngFileNameStart=Instr(strHeaderData,"filename="&chr(34))+Len("filename="&chr(34))<br />lngFileNameEnd=Instr(lngFileNameStart,strHeaderData,chr(34))<br />strFileName=Mid(strHeaderData,lngFileNameStart,lngFileNameEnd-lngFileNameStart)<br />strFileName=Trim(strFileName)<br />strFileName=Replace(strFileName,vbCRLF,vbNullString)<br />else<br />strfilename=""<br />end if<br />'读取上传文件数据<br />DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4<br />DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStart<br />If strFileName<>"" Then<br />if dataend>0 then<br />binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )<br />'将上传的文件写入数据库<br />titem=titem+1<br />'response.write "titem:"&titem<br />rs("FileContentType_"&titem)=strFileContentType<br />rs("FileContent_"&titem).AppendChunk binFieldValue<br />rs("filesize_"&titem)=lenb(binFieldValue)<br />rs("filename_"&titem)=strfilename<br />else<br />binfieldvalue=binhttpheader<br />end if<br />End if<br />if INSTRB( DataStart + 1, binHTTPHeader, divider )>0 then<br />binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))<br />else<br />binhttpheader=""<br />end if<br />loop<br />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"))<br />if csng(rs("sizetotal"))>=csng(2*1024*1024) then<br />response.write "对不起,文件太大,请保证每封邮件的总大小不超过2M!"<br />response.end<br />end if<br />rs("mailtime")=now<br />rs("readerempl")=""<br />if rs("receempl")<>"" then<br />rs("receempl")=session("myid")<br />rs("readerempl")=session("myid")<br />end if<br />rs("deleempl")=""<br />rs("deleverempl")=""<br />rs("sendmac")=strmac<br />rs.update<br />rs.close<br />set rs=Nothing<br />conn.Close<br />set conn=Nothing<br />%><br /><script language=javascript><br />window.open("mailok.asp",target="_self")<br /></script><br /></body></html><br /> 最后,我们来讲讲如何把内容从数据库中读出来,内容有这么几类,一类是浏览器上可以显示的,例如*.htm,一类是需要下载的,例如*.exe,还有一种是浏览器可以显示但是不能够让他显示的,例如*.asp,请看代码:<br /><%<br />Response.Buffer= true<br />Response.Clear<br />function getname(oriname)<br />thename=oriname<br />do while instr(thename,"/")>0<br />thename=mid(thename,instr(thename,"/")+1)<br />loop<br />do while instr(thename,"\")>0<br />thename=mid(thename,instr(thename,"\")+1)<br />loop<br />getname=thename<br />end function<br />function canexec(thechar)<br />if instr(thechar,".asp")>0 then<br />canexec=false<br />exit function<br />end if<br />if instr(thechar,".asa")>0 then<br />canexec=false<br />exit function<br />end if<br />if instr(thechar,".aspx")>0 then<br />canexec=false<br />exit function<br />end if<br />if instr(thechar,".asax")>0 then<br />canexec=false<br />exit function<br />end if<br />canexec=true<br />end function<br />mailID=request("mailID")<br />se=request("se")<br />if se<>1 and se<>2 and se<>3 then<br />response.end<br />end if<br />Set conn=server.createobject("adodb.connection")<br />set rs=server.createobject("adodb.recordset")<br />conn.open "DSN=;UID=;PWD="<br />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&"' "<br />rs.open sql,conn,3,3<br />if rs.eof or rs.bof then<br />response.end<br />end if<br />if rs("filecontenttype_"&trim(se))<>"text/plain" or (not canexec(getname(trim(rs("filename_"&trim(se)))))) then<br />Response.ContentType = rs("FileContentType_"&trim(se))<br />end if<br />'Response.AddHeader "content-type","application/x-msdownload"<br />if instr(response.contenttype,"application")>0 then<br />response.AddHeader "Content-Disposition","attachment;filename="&getname(trim(rs("filename_"&trim(se))))<br />end if<br />Response.BinaryWrite rs("FileContent_"&trim(se))<br />rs.close<br />set rs=Nothing<br />conn.close<br />set conn=nothing<br />%></code></p>
<p> </p>
<p> 文章到这里就结束了,至于邮件数据库的数据结构大家根据代码自己琢磨吧!</p></p>