vbs定时发送邮件的方法与代码
程序员文章站
2022-03-06 23:48:52
'用vbs写个脚本,然后用windows平台下的计划任务来调用,每天定时群发邮件. '代码如下: 下载地址 http://www...
'用vbs写个脚本,然后用windows平台下的计划任务来调用,每天定时群发邮件.
'代码如下: 下载地址 http://www.51tiao.com/info.vbs
dim connstr,conn
dim sql,rs,msg
sub opendb()
connstr = "dsn=51tiao.com;uid=sa;pwd=;"
if not isobject(conn) then
set conn = createobject("adodb.connection")
conn.open connstr
end if
end sub
opendb()
send()
closedb()
sub send()
on error resume next '有错继续执行
'邮件内容
msg = "<html><head><title>上海跳蚤市场今日推荐 "&date()&"</title>"&vbcrlf _
&"<meta name=""author"" content=""清风, qq: 110125707, msn: anwellsz@msn.com"">"&vbcrlf _
&"<style type='text/css'>"&vbcrlf _
&"<!--"&vbcrlf _
&"td,form,select,input,p,table,.font {font-size: 12px;line-height: 20px}"&vbcrlf _
&"a:link { color: #000000; font-size: 12px; text-decoration: none}"&vbcrlf _
&"a:visited { color: #000000; font-size: 12px; text-decoration: none}"&vbcrlf _
&"a:hover { color: #ff7f2c; font-size: 12px; text-decoration: underline}"&vbcrlf _
&"-->"&vbcrlf _
&"</style>"&vbcrlf _
&"</head><body>"&vbcrlf _
&"<table width=640>"&vbcrlf _
&"<tr><td align=right>今日推荐信息 "&year(date())&"年"&month(date())&"月"&day(date())&"日 <a href=""http://www.51tiao.com"" target=""_blank""><font size=3><b>上海跳蚤市场</b></font></a> </td></tr></table></div></td></tr></table>"&vbcrlf _
&"<table width=640>"&vbcrlf _
&"<tr bgcolor='#ff9d5c'><td height=3></td></tr><tr><td> </td></tr><tr>"&vbcrlf _
&"<td>"&vbcrlf _
&" <ul>"&vbcrlf _
&" <p>"
sql = "select distinct top 100 a.infoid,a.strtitle from newinfoarticle a "_
&"inner join newinfoprop b "_
&"on a.infoid = b.infoid and a.intgood = 1 and a.intshenhe = 1 and b.rid1 = 908 and datediff(d,createtime,getdate())=0 "_
&"order by a.infoid desc"
set rs = conn.execute(sql)
if rs.eof then
wscript.echo "没有记录!"
rs.close : set rs = nothing
exit sub
end if
do while not rs.eof
msg = msg&"★ <a href=""http://www.51tiao.com/4/show.asp?id="&rs("infoid")&""" title = """&rs("strtitle")&""" target=""_blank"">"_
&rs("strtitle")&"</a><br>"&vbcrlf
rs.movenext
loop
rs.close : set rs=nothing
msg = msg & "</ul></p>"&vbcrlf _
&"</td>"&vbcrlf _
&"</tr><tr><td> </td></tr><tr bgcolor='#ff9d5c'><td height=3></td></tr>"&vbcrlf _
&"<tr align=right><td><a href=""http://www.51tiao.com"" target=""_blank""><font face='arial black' size=3>51tiao.com</font></a> </td></tr>"&vbcrlf _
&"</table><p></p></body></html>"
'取得邮件地址
dim i,total,jmail
i = 1
dim badmail '不接收的邮件列表 格式 '邮件地址','邮件地址'
badmail = "'123@163.com','122@126.com'"
sql = "select distinct b.stremail from userinfo a inner join userinfo_1 b "_
&"on a.id = b.intuserid and b.stremail <> '' and (charindex('3',a.struserlevel)>0 or charindex('4',a.struserlevel)>0) "_
&"and b.stremail not in ("&badmail&") "_
&"order by b.stremail"
set rs = createobject("adodb.recordset")
rs.open sql,conn,1,1
total = rs.recordcount
if rs.eof then
wscript.echo "没有用户!"
rs.close : set rs = nothing
exit sub
end if
'每二十个邮件地址发送一次
for i = 1 to total
if i mod 20 = 1 then
set jmail = createobject("jmail.message") '建立发送邮件的对象
'jmail.silent = true '屏蔽例外错误,返回false跟true两值
jmail.logging = true '记录日志
jmail.charset = "gb2312" '邮件的文字编码
jmail.contenttype = "text/html" '邮件的格式为html格式或纯文本
end if
jmail.addrecipient rs(0)
if i mod 20 = 0 or i = 665 then
jmail.from = "info at 51tiao" '发件人的e-mail地址
jmail.fromname = "上海跳蚤市场" '发件人的名称
jmail.mailserverusername = "info" '登录邮件服务器的用户名 (您的邮件地址)
jmail.mailserverpassword = "123123" '登录邮件服务器的密码 (您的邮件密码)
jmail.subject = "上海跳蚤市场今日推荐 "&year(date())&"年"&month(date())&"月"&day(date())&"日" '邮件的标题
jmail.body = msg '邮件的内容
jmail.priority = 3 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail.send("mail.51tiao.com") '执行邮件发送(通过邮件服务器地址)
jmail.close()
set jmail = nothing
end if
rs.movenext
next
rs.close : set rs = nothing
'记录日志在c:\jmail年月日.txt
const def_fsostring = "scripting.filesystemobject"
dim fso,txt
set fso = createobject(def_fsostring)
set txt=fso.createtextfile("c:\jmail"&datevalue(date())&".txt",true)
txt.write "邮件发送成功,共发送了"&total&"封邮件,发送于 "&now()&"<br><br>"
txt.write jmail.log
set txt = nothing
set fso = nothing
wscript.echo "邮件发送成功,共发送了"&total&"封邮件,发送于 "&now()
end sub
sub closedb()
if isobject(conn) then
conn.close : set conn = nothing
end if
end sub
'代码如下: 下载地址 http://www.51tiao.com/info.vbs
复制代码 代码如下:
dim connstr,conn
dim sql,rs,msg
sub opendb()
connstr = "dsn=51tiao.com;uid=sa;pwd=;"
if not isobject(conn) then
set conn = createobject("adodb.connection")
conn.open connstr
end if
end sub
opendb()
send()
closedb()
sub send()
on error resume next '有错继续执行
'邮件内容
msg = "<html><head><title>上海跳蚤市场今日推荐 "&date()&"</title>"&vbcrlf _
&"<meta name=""author"" content=""清风, qq: 110125707, msn: anwellsz@msn.com"">"&vbcrlf _
&"<style type='text/css'>"&vbcrlf _
&"<!--"&vbcrlf _
&"td,form,select,input,p,table,.font {font-size: 12px;line-height: 20px}"&vbcrlf _
&"a:link { color: #000000; font-size: 12px; text-decoration: none}"&vbcrlf _
&"a:visited { color: #000000; font-size: 12px; text-decoration: none}"&vbcrlf _
&"a:hover { color: #ff7f2c; font-size: 12px; text-decoration: underline}"&vbcrlf _
&"-->"&vbcrlf _
&"</style>"&vbcrlf _
&"</head><body>"&vbcrlf _
&"<table width=640>"&vbcrlf _
&"<tr><td align=right>今日推荐信息 "&year(date())&"年"&month(date())&"月"&day(date())&"日 <a href=""http://www.51tiao.com"" target=""_blank""><font size=3><b>上海跳蚤市场</b></font></a> </td></tr></table></div></td></tr></table>"&vbcrlf _
&"<table width=640>"&vbcrlf _
&"<tr bgcolor='#ff9d5c'><td height=3></td></tr><tr><td> </td></tr><tr>"&vbcrlf _
&"<td>"&vbcrlf _
&" <ul>"&vbcrlf _
&" <p>"
sql = "select distinct top 100 a.infoid,a.strtitle from newinfoarticle a "_
&"inner join newinfoprop b "_
&"on a.infoid = b.infoid and a.intgood = 1 and a.intshenhe = 1 and b.rid1 = 908 and datediff(d,createtime,getdate())=0 "_
&"order by a.infoid desc"
set rs = conn.execute(sql)
if rs.eof then
wscript.echo "没有记录!"
rs.close : set rs = nothing
exit sub
end if
do while not rs.eof
msg = msg&"★ <a href=""http://www.51tiao.com/4/show.asp?id="&rs("infoid")&""" title = """&rs("strtitle")&""" target=""_blank"">"_
&rs("strtitle")&"</a><br>"&vbcrlf
rs.movenext
loop
rs.close : set rs=nothing
msg = msg & "</ul></p>"&vbcrlf _
&"</td>"&vbcrlf _
&"</tr><tr><td> </td></tr><tr bgcolor='#ff9d5c'><td height=3></td></tr>"&vbcrlf _
&"<tr align=right><td><a href=""http://www.51tiao.com"" target=""_blank""><font face='arial black' size=3>51tiao.com</font></a> </td></tr>"&vbcrlf _
&"</table><p></p></body></html>"
'取得邮件地址
dim i,total,jmail
i = 1
dim badmail '不接收的邮件列表 格式 '邮件地址','邮件地址'
badmail = "'123@163.com','122@126.com'"
sql = "select distinct b.stremail from userinfo a inner join userinfo_1 b "_
&"on a.id = b.intuserid and b.stremail <> '' and (charindex('3',a.struserlevel)>0 or charindex('4',a.struserlevel)>0) "_
&"and b.stremail not in ("&badmail&") "_
&"order by b.stremail"
set rs = createobject("adodb.recordset")
rs.open sql,conn,1,1
total = rs.recordcount
if rs.eof then
wscript.echo "没有用户!"
rs.close : set rs = nothing
exit sub
end if
'每二十个邮件地址发送一次
for i = 1 to total
if i mod 20 = 1 then
set jmail = createobject("jmail.message") '建立发送邮件的对象
'jmail.silent = true '屏蔽例外错误,返回false跟true两值
jmail.logging = true '记录日志
jmail.charset = "gb2312" '邮件的文字编码
jmail.contenttype = "text/html" '邮件的格式为html格式或纯文本
end if
jmail.addrecipient rs(0)
if i mod 20 = 0 or i = 665 then
jmail.from = "info at 51tiao" '发件人的e-mail地址
jmail.fromname = "上海跳蚤市场" '发件人的名称
jmail.mailserverusername = "info" '登录邮件服务器的用户名 (您的邮件地址)
jmail.mailserverpassword = "123123" '登录邮件服务器的密码 (您的邮件密码)
jmail.subject = "上海跳蚤市场今日推荐 "&year(date())&"年"&month(date())&"月"&day(date())&"日" '邮件的标题
jmail.body = msg '邮件的内容
jmail.priority = 3 '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值
jmail.send("mail.51tiao.com") '执行邮件发送(通过邮件服务器地址)
jmail.close()
set jmail = nothing
end if
rs.movenext
next
rs.close : set rs = nothing
'记录日志在c:\jmail年月日.txt
const def_fsostring = "scripting.filesystemobject"
dim fso,txt
set fso = createobject(def_fsostring)
set txt=fso.createtextfile("c:\jmail"&datevalue(date())&".txt",true)
txt.write "邮件发送成功,共发送了"&total&"封邮件,发送于 "&now()&"<br><br>"
txt.write jmail.log
set txt = nothing
set fso = nothing
wscript.echo "邮件发送成功,共发送了"&total&"封邮件,发送于 "&now()
end sub
sub closedb()
if isobject(conn) then
conn.close : set conn = nothing
end if
end sub