分享
 
 
 

用ASP实现支持附件的EMail系统

王朝asp·作者佚名  2008-05-21
窄屏简体版  字體: |||超大  

大家经常探讨使用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 thenresponse.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 thengeterempl=trim(request.querystring("geterempl"))emailtitle=trim(request.querystring("emailtitle"))elseif meth=2 thenmailid=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,3if rs.bof or rs.eof then%><script language="javascript">alert("您没有查看这封邮件的权限!");window.history.back();</script><%response.endelsebody=rs("body")emailtitle=rs("emailtitle")rs.closeset rs=nothingconn.closeset conn=nothingend ifend 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>"<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,trueSet sh = nothingSet fso = createobject("scripting.filesystemobject")Set ts = fso.opentextfile("c:" & strIP & ".txt")macaddress = nullDo While Not ts.AtEndOfStreamdata = ucase(trim(ts.readline))If instr(data,"MAC ADDRESS") Thenmacaddress = trim(split(data,"=")(1))Exit DoEnd Ifloopts.closeSet ts = nothingfso.deletefile "c:" & strIP & ".txt"Set fso = nothingGetMACAddress = macaddressstrMac = GetMACAddressset 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,dsnpathif rs.bof thenset 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_idif isnumeric(g_id) theng_id=cint(g_id)elseg_id=0end ifsele="insert into getmac(g_ip,g_mac,g_id,g_ok) values('"&strIP&"','"&strMac&"',"&g_id&",0)"rs.open sele,dsnpathelseset 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,dsnpathif rs.bof or rs.eof thenset rs1=server.CreateObject("adodb.recordset")sele="insert into badmac(ip, mac ,thetime) values('"&strIP&"','"&strMac&"','"&now()&"')"rs1.open sele,dsnpathresponse.redirect("/reg/wrong.asp")response.endend ifend 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=0Function bin2str(binstr)Dim varlen,clow,ccc,skipflagskipflag=0ccc = ""If Not IsNull(binstr) Thenvarlen=LenB(binstr)For i=1 To varlenIf skipflag=0 Thenclow = MidB(binstr,i,1)If AscB(clow) > 127 Thenccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))skipflag=1Elseccc = ccc & Chr(AscB(clow))End IfElseskipflag=0End IfNextEnd Ifbin2str = cccEnd FunctionvarByteCount = Request.TotalBytes'response.write varbytecountbnCRLF = chrB( 13 ) & chrB( 10 )binHTTPHeader=Request.BinaryRead(varByteCount)'response.write vbenter'response.write ""& cstr(binhttpheader) &""sread=0eread=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,3rs.addnewrs("emaillevel")=0rs("receempl")=""Do while lenB(binHTTPHeader)>46Divider = 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 "lngfieldnamestart:"&lngfieldnamestartlngFieldNameEnd=Instr(lngFieldNameStart,strHeaderData,chr(34))'response.write "lngfieldnameEND:"&lngfieldnameENDstrFieldName=Mid(strHeaderData,lngFieldNameStart,lngFieldNameEnd-lngFieldNameStart)'RESPOnSE.WRITE "<BR>STRFIELDNAME:" & STRfieldnamestrFieldName=Trim(strFieldName)strFieldName=Replace(strFieldName,vbcrlf,vbnullstring)'判断文件数据时候开始If strComp(strFieldName,"FileUploadStart",1)=0 and sread=0 Then'response.write "找到了文件开始的地方"sread=1'response.write "" & INSTRB( DataStart + 1, binHTTPHeader, divider ) &""binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))exit doEnd ifDataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStartbinFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )strFieldValue=bin2str(binFieldValue)'strFieldValue=Trim(strFieldValue)strFieldValue=Replace(strFieldValue," "," ")'非文件上传域变量赋值'execute strFieldName&"="""&strFieldValue&""""'response.write strFieldName&":"&strFieldValue&""if strfieldname="geterempl" thenstrFieldValue=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("您输入的收件组格式错误!

正确的格式是:'gr:001'");history.back();</script><p><%response.endelseif not isnumeric(mid(trim(strfieldvalue),4)) then'格式错误返回%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件组格式错误!

正确的格式是:'gr:001'");history.back();</script><p><%response.endelsethegroup=(mid(trim(strfieldvalue),4))end ifend iftmpSQL="select * from t_group where owner='"&session("myid")&"' and groupidowner='"&thegroup&"'"'response.write tmpsqlset tmprs=server.CreateObject("ADODB.Recordset")tmprs.Open tmpsql,connif tmprs.bof or tmprs.eof then'没有找到该组%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件组<%=thegroup%>没有找到!");history.back();</script><p><%response.endelseif tmprs("personnum")=0 then'组内没有用户%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件组<%=thegroup%>中目前没有任何的用户

所以不能发送");history.back();</script><p><%response.endelsestrFieldValue=trim(tmprs("groupempl"))tmprs.closeset tmprs=nothingend ifend ifend ifif instr(strfieldValue,"|") then'组发allsearch=replace(trim(strfieldValue),"|","','")allsearch="'"&allsearch&"'"tmpstring=trim(strfieldValue)&"|"tosearch=""do while len(tmpstring)>=5tosearch=left(tmpstring,5)tmpstring=mid(tmpstring,7)if instr(tosearch,"|") then'格式错误%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件人格式错误!");history.back();</script><p><%response.endend iftmpSQL="select * from (select userid from t_officer where userid in ("&allsearch&")) DERIVEDTBL where userid='"&tosearch&"'"'response.write tmpsqlset tmprs=server.CreateObject("ADODB.Recordset")tmprs.Open tmpsql,connif tmprs.eof or tmprs.bof then%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件人<%=tosearch%>没有找到!");history.back();</script><p><%response.endend iftmprs.closeset tmprs=nothingloopstrfieldValue=trim(strFieldValue)elseif len(trim(strFieldValue))<>5 then'格式不正确%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件人<%=trim(strFieldValue)%>不正确!");history.back();</script><p><%response.endelseif isnumeric(trim(len(strFieldValue))) thentmpSQL="select * from t_officer where userid='"&trim(strFieldValue)&"'"set tmprs=server.CreateObject("ADODB.Recordset")tmprs.Open tmpsql,connif tmprs.eof or tmprs.bof then%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件人<%=trim(strFieldValue)%>没有找到

该员工可能还没有注册!");history.back();</script><p><%response.endend iftmprs.closeset tmprs=nothingstrfieldValue=trim(strFieldValue)else%>尝试发送邮件,但是失败了,请修改错误后重试!<script language="javascript">alert("您输入的收件人<%=trim(strFieldValue)%>不正确!");history.back();</script><p> <%response.endend ifend ifend ifend ifstrFieldValue=replace(strFieldValue,"<","<")'response.write strfieldnamers(STRFIELDNAME)=replace(strFieldValue,">",">")binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))loop'开始处理文件数据titem=0rs("filesize_1")=0rs("filesize_2")=0rs("filesize_3")=0Do while lenB(binHTTPHeader)>46if INSTRB( binHTTPHeader, bnCRLF & bnCRLF )<>0 thenbinHeaderData = LeftB(binHTTPHeader,INSTRB( binHTTPHeader, bnCRLF & bnCRLF )-1)elseexit doend ifstrHeaderData=bin2str(binHeaderData)'读取上传文件的Content-TypelngFileContentTypeStart=Instr(strHeaderData,"Content-Type:")+Len("Content-Type:")strFileContentType=Trim(Mid(strHeaderData,lngFileContentTypeStart))strFileContentType=Replace(strFileContentType,vbCRLF,vbNullString)'读取上传的文件名if instr(strheaderdata,"filename=")>0 thenlngFileNameStart=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)elsestrfilename=""end if'读取上传文件数据DataStart = INSTRB( binHTTPHeader, bnCRLF & bnCRLF ) + 4DataEnd = INSTRB( DataStart + 1, binHTTPHeader, divider ) - DataStartIf strFileName<>"" Thenif dataend>0 thenbinFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )'将上传的文件写入数据库titem=titem+1'response.write "titem:"&titemrs("FileContentType_"&titem)=strFileContentTypers("FileContent_"&titem).AppendChunk binFieldValuers("filesize_"&titem)=lenb(binFieldValue)rs("filename_"&titem)=strfilenameelsebinfieldvalue=binhttpheaderend ifEnd ifif INSTRB( DataStart + 1, binHTTPHeader, divider )>0 thenbinHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))elsebinhttpheader=""end iflooprs("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) thenresponse.write "对不起,文件太大,请保证每封邮件的总大小不超过2M!"response.endend ifrs("mailtime")=nowrs("readerempl")=""if rs("receempl")<>"" thenrs("receempl")=session("myid")rs("readerempl")=session("myid")end ifrs("deleempl")=""rs("deleverempl")=""rs("sendmac")=strmacrs.updaters.closeset rs=Nothingconn.Closeset conn=Nothing%><script language=javascript>window.open("mailok.asp",target="_self")</script></body></html>

最后,我们来讲讲如何把内容从数据库中读出来,内容有这么几类,一类是浏览器上可以显示的,例如*.htm,一类是需要下载的,例如*.exe,还有一种是浏览器可以显示但是不能够让他显示的,例如*.asp,请看代码:<%Response.Buffer= trueResponse.Clearfunction getname(oriname)thename=orinamedo while instr(thename,"/")>0thename=mid(thename,instr(thename,"/")+1)loopdo while instr(thename,"")>0thename=mid(thename,instr(thename,"")+1)loopgetname=thenameend functionfunction canexec(thechar)if instr(thechar,".asp")>0 thencanexec=falseexit functionend ifif instr(thechar,".asa")>0 thencanexec=falseexit functionend ifif instr(thechar,".aspx")>0 thencanexec=falseexit functionend ifif instr(thechar,".asax")>0 thencanexec=falseexit functionend ifcanexec=trueend functionmailID=request("mailID")se=request("se")if se<>1 and se<>2 and se<>3 thenresponse.endend ifSet 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,3if rs.eof or rs.bof thenresponse.endend ifif rs("filecontenttype_"&trim(se))<>"text/plain" or (not canexec(getname(trim(rs("filename_"&trim(se)))))) thenResponse.ContentType = rs("FileContentType_"&trim(se))end if'Response.AddHeader "content-type","application/x-msdownload"if instr(response.contenttype,"application")>0 thenresponse.AddHeader "Content-Disposition","attachment;filename="&getname(trim(rs("filename_"&trim(se))))end ifResponse.BinaryWrite rs("FileContent_"&trim(se))rs.closeset rs=Nothingconn.closeset conn=nothing%>

文章到这里就结束了,至于邮件数据库的数据结构大家根据代码自己琢磨吧

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有