欢迎光临
我们一直在努力

在Windows桌面上使用WSH接收邮件

建站超值云服务器,限时71元/月

公司的销售管理人员日常想要接收订单在$10,000元以上的电子邮件,同时也很愿意感谢来信订购的用户。电子邮件的主体要包括所有必要的信息,因此就没有必要访问公司客户/服务器程序。第一步是近可能地委派数据库服务器的数据。在这里的例子中,我选择了随sql server 7.0安装后随带的northwind数据库。

原文出处:http://www.15seconds.com/issue/991007.htm

   我决定在northwind数据库中建立一个视图,它将提供所需要的一切。启动sql server enterprise管理器,检查一下northwind数据库下的视图列表,order subtotals视图就在眼前,它计算所有放置在northwind数据库中的定单总和。我决定在新视图10k_order_qry中引用这个视图,如下:

create view dbo.[10k_order_qry]

as

select [order subtotals].orderid, convert(varchar(15),

[order subtotals].subtotal) as subtotal, convert(char(10),

orders.orderdate, 110) as orderdate,

customers.companyname, customers.contactname,

customers.country, customers.phone

from [order subtotals], orders, customers

where [order subtotals].orderid = orders.orderid and

orders.customerid = customers.customerid and

subtotal >= 10000

— below lines can be switched in order to look for todays sales over $10,000

— (comment the next line, and uncomment the second line)

and orderdate >= 02-01-1998 and orderdate <= 02-28-1998

–and orderdate = convert(char(10),getdate(),110)

   注意:在视图中引用另外一个视图不是一个好的方法。但是,这里对此不做详细地讨论。

   为了不修改northwind中的数据,并且能在邮件中显示,请回顾这个新视图“10k_order_qry”的最后2行。在其上的注释行解释了最后的目的。我要强调指出的是编写一个存储过程能达到更好的性能效果,因为存储过程能被编译。对于sql server预先设定一个执行计划,将大大地减少计划执行中的周折。

   为了验证输出的报告所需要的所有字段是否准备好,我们可以在sql server query analyzer中执行这个视图进行测试。

我们大多数人,在相当多的时候,都在ms-dos批处理文件中使用过脚本。融入windows 98、internet information server 4.0、windows nt workstation 2000和windows nt server 2000中的微软windows scripting host是一个独立语言的脚本引擎。visual basic和javascript脚本引擎也被包含在windows scripting host中。

   本质上,windows scripting host提供给我们在windows桌面上运行脚本的功能,或者在命令控制台执行,而不需要在html文档或者asp中执行。这是个强大的功能,并且能由此更深地研究,比如编写登录脚本、管理脚本或者机器自动处理的工作。

   现在准备编写脚本来建立一个报告,并且将它发送电子邮件给服务商管理者。我决定以visual basic scripting语言编写这个脚本,可以使用任何文字编辑器来创建它。唯一的要求是以“vbs”扩展名来保存文件,比如

“myscript.vbs”。如果安装了windows nt option pack 4,就存在了脚本调试器msscrdbg.exe,它能被用做创建和调试脚本。它远远强于notepad!

dim objsendmail

dim strto, strfrom

dim strsubject, strbody

dim shipuic

mail constants

const cdobodyformattype = 0 body property is html

const cdomailformattype = 0 newmail object is in mime format

const cdonormal = 1 normal importance (default)

strfrom = "admin@northwind.com" system administrator or dba mail account

strto =" manager@northwind.com" recipient mail account – i.e. sales manager

strsubject = "sales over $10,000" mail subject

call function to build the html mail body

strbody = mailbody()

the following section creates the e-mail object and sends the mail

set objsendmail = createobject("cdonts.newmail")

objsendmail.from = strfrom

objsendmail.to = strto

objsendmail.subject = strsubject

objsendmail.body = strbody

objsendmail.bodyformat = cdobodyformattype

objsendmail.mailformat = cdomailformattype

objsendmail.importance = cdonormal

objsendmail.send

set objsendmail = nothing

**********************************************************************************

function mailbody()

dim oconn

dim ocmd

dim ors

dim tmpbody

set oconn = createobject("adodb.connection")

oconn.open("database=northwind;dsn=northwind;uid=sa;password=;")

set ocmd = createobject("adodb.command")

ocmd.activeconnection = oconn

ocmd.commandtext = "select * from northwind.dbo.[10k_order_qry] order by subtotal desc"

ocmd.commandtype = 1

ocmd.prepared = true

set ors = ocmd.execute

ors.movefirst

tmpbody = "10k customer report"

tmpbody = tmpbody & "as of " & date() & ""

tmpbody = tmpbody & ""

tmpbody = tmpbody & " order id "

tmpbody = tmpbody & " subtotal "

tmpbody = tmpbody & " company "

tmpbody = tmpbody & " contact "

tmpbody = tmpbody & " country "

tmpbody = tmpbody & " phone "

while not ors.eof

tmpbody = tmpbody & " " & ors.fields("orderid") & " "

tmpbody = tmpbody & " " & "$" & ors.fields("subtotal") & " "

tmpbody = tmpbody & " " & ors.fields("companyname") & " "

tmpbody = tmpbody & " " & ors.fields("contactname") & " "

tmpbody = tmpbody & " " & ors.fields("country") & " "

tmpbody = tmpbody & " " & ors.fields("phone") & " "

ors.movenext

wend

tmpbody = tmpbody & " "

mailbody = tmpbody

set ors = nothing

set ocmd = nothing

set oconn = nothing

end function

这里插入了足够的注释用以阐明脚本的含义,但对于那些不熟悉html的用户来说,下面是一个简单的标记解释对应表,我在脚本中使用了它们来建立邮件(报告)的主体:

   标记 含义

?lt;/h2> header tag size 2

?lt;/font> font color "red"

?lt;/b> bold text

?lt;/table> table format

?lt;/tr> table row

?lt;/th> header cell

?lt;/td> data cell

   现在让我们在windows scripting host中运行编写的visual basic脚本。在windows的资源浏览器explorer中,用鼠标右键单击myscript.vbs,选择“属性”,点击“script”功能页面。

  

   检查“stop script after specified number of seconds”复选框,设置足够的时间来让脚本运行完成(默认是10秒钟)。按下ok按钮,新的脚本文件就创建了,但是名字改变为myscript.wsh。wsh文件的内容与古老的“ini”文件类似:

[scriptfile]

path=myscript.vbs

[options]

timeout=30

displaylogo=1

batchmode=0

   假设没有错误,在双击myscript.wsh文件后,邮件接收者将收到一封邮件,内容主体是报告,如下:

   10k customer report

   as of 9/13/99

   order id subtotal company contact country phone

10865 $16387.50 quick-stop hors t kloss germany 0372-035188

10889 $11380.00 rattlesnake canyon grocery paula wilson usa (505) 555-5939

10897 $10835.24 hungry owl all-night grocers patricia mckenna ireland 2967 542

   现在可以确认脚本工作正常,最后一步是设置每日的定时执行,这里使用了windows计划执行程序来完成定时任务的设置。启动windows计划执行程序,选择edit菜单,选择add,输入要执行的脚本名字,设置执行的时间:

   按“ok”按钮,完成设置,这条项目显示如下:

   windows scripting host,cdonts以及计划执行程序有广泛的应用面,这里只是列举了一个实际的应用。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在Windows桌面上使用WSH接收邮件
分享到: 更多 (0)