ASP+XMLHTTP跨主机调用Jmail发送邮件
// by redice 2009.8.24// http://www.
“ASP+XMLHTTP跨主机调用Jmail发送邮件” 所要解决的问题是在不支持Jmail组件的服务器上实现邮件的发送。
(国内大多IDC主机都是支持Jmail组件的)
实现原理其实也很简单:
(1)我们需要一台支持Jmail组件的主机,在其Web目录放置我们的“Jmail远程调用接口文件 MailApi.asp”(后附源代码), 该文件的功能是接收参数(接收者邮箱,邮件主题,邮件内容,发送者名称等信息),并调用Jmail发送邮件。 假如这个地址是:http://www.。
(2)在需要发送邮件但不支持Jmail的主机上,通过XMLHTTP向“接口文件 MailApi.asp”POST邮件参数。将SendMail.asp放置于该主机的Web目录,在需要发邮件的位置调用SendMail函数即可。
存在的问题:
没有加入身份认证,在得知接口文件地址后任何人都能调用该接口发送邮件,增加了主机的压力。
附源代码:
程序代码:
<% ASP+XMLHTTP跨主机调用Jmail发送邮件 by redice 2009.8.24 http://www. Jmail远程调用接口文件 response.Charset="gb2312" dim receiver dim subject dim content dim sendername receiver=filter(request("receiver")) subject=filter(request("subject")) content=filter(request("content")) sendername=filter(request("sendername")) response.write receiver & "," & subject & "," & content & "," & sendername & "<br>" if receiver="" or subject="" or content="" or sendername="" then response.write "邮件信息不完整!" response.end end if SendMail receiver,subject,content,sendername ---------------------------------Jmail发送电子邮件函数---------------------------- SMTP等相关信息设置 Const SenderEmail="发送者邮箱" Const Smtp="SMTP服务器地址" Const UserName="SMTP用户名" Const Password="SMTP密码" Public Function SendMail(Receiver,Subject,Content,SenderName) dim msg Set msg = Server.CreateObject("JMail.Message") msg.silent = true msg.Logging = true msg.Charset = "gb2312" msg.MailServerUserName = UserName msg.MailServerPassword = Password msg.From = SenderEmail msg.FromName = SenderName msg.AddRecipient Receiver msg.Subject = Subject msg.Body = Content msg.Send (Smtp) msg.close() set msg = nothing End Function Function filter(str) filter=replace(str,"","") End Function response.write "发送邮件成功!" %> <% ASP+XMLHTTP跨主机调用Jmail发送邮件 by redice 2009.8.24 http://www. 邮件发送函数所在文件 response.Charset="gb2312" Jmail远程接口文件(MailApi.asp)地址 Const MailApiUrl="根据实际情况修改" 邮件发送调用函数 function SendMail(Receiver,Subject,Content,SenderName) dim querystring querystring="receiver=" & receiver querystring=querystring & "&subject=" & server.urlencode(subject) querystring=querystring & "&content=" & server.urlencode(content) querystring=querystring & "&sendername=" & server.urlencode(sendername) response.write querystring & "<br>" dim xmlHttp set xmlHttp=Server.CreateObject("Micro"&"soft"&"."&"XML"&"HTTP") xmlHttp.open "post",MailApiUrl,false xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" xmlHttp.send querystring & vbcrlf & vbcrlf response.write xmlHttp.responseText end function 测试示例 SendMail "redice@,"主题","内容","redice" %>
PS:代码中的西文单引号被过滤了,下面是打包的
http://www.
[ 本帖最后由 redice 于 2009-8-26 20:38 编辑 ]