风易博客

风萧萧兮易水寒, 壮士一去兮不复还。

« 打造自己的PJBLOGGoogle中国办公环境揭密(多图) »

Asp下定时发送邮件的思路 (VBS)

会说话的博客,点我试试:)

用VBS写个脚本,然后用WINDOWS平台下的计划任务来调用,每天定时群发邮件. 代码如下:

ASP/Visual Basic代码
  1. Dim connstr,conn   
  2. Dim sql,rs,msg   
  3.   
  4. Sub OpenDB()   
  5.     ConnStr = "DSN=51tiao.Com;UID=sa;PWD=;"  
  6.     If Not IsObject(Conn) Then  
  7.         Set conn = CreateObject("Adodb.Connection")   
  8.         Conn.Open ConnStr   
  9.     End If  
  10. End Sub  
  11.   
  12. OpenDB()   
  13. Send()   
  14. CloseDB()   
  15.   
  16. Sub Send()   
  17.     On Error Resume Next '有错继续执行   
  18.     '邮件内容   
  19.     msg = "<html><head><title>上海跳蚤市场今日推荐 "Date()"</title>"&VBCRLF _   
  20.     "<META NAME=""Author"" CONTENT=""清风, QQ: 110125707, MSN: anwellsz@msn.com"">"&VBCRLF _   
  21.     "<style type='text/css'>"&VBCRLF _   
  22.     "<!--"&vbcrlf _   
  23.     "td,form,select,input,p,table,.font {font-size: 12px;line-height: 20px}"&VBCRLF _   
  24.     "a:link {  color: #000000;  font-size: 12px; text-decoration: none}"&VBCRLF _   
  25.     "a:visited {  color: #000000; font-size: 12px; text-decoration: none}"&VBCRLF _   
  26.     "a:hover {  color: #ff7f2c; font-size: 12px; text-decoration: underline}"&VBCRLF _   
  27.     "-->"&VBCRLF _   
  28.     "</style>"&VBCRLF _   
  29.     "</head><body>"&VBCRLF _   
  30.     "<table width=640>"&VBCRLF _   
  31.     "<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 _   
  32.     "<table width=640>"&VBCRLF _   
  33.     "<tr bgColor='#FF9D5C'><td height=3></td></tr><tr><td> </td></tr><tr>"&VBCRLF _   
  34.     "<td>"&VBCRLF _   
  35.     "  <ul>"&VBCRLF _   
  36.     "    <p>"  
  37.     sql = "select distinct top 100 a.infoid,a.Strtitle from newinfoarticle a "_   
  38.     "inner join Newinfoprop b "_   
  39.     "on a.infoid = b.infoid and a.intgood = 1 and a.intshenhe = 1 and b.rid1 = 908 and datediff(d,createtime,getdate())=0 "_   
  40.     "order by a.infoid desc"  
  41.     Set rs = conn.execute(sql)   
  42.     If rs.eof Then  
  43.         Wscript.Echo "没有记录!"  
  44.         rs.close : Set rs = Nothing  
  45.         Exit Sub  
  46.     End If  
  47.     Do While Not rs.eof   
  48.         msg = msg"★ <a href=""http://www.51tiao.com/4/Show.asp?ID="&rs("infoid")""" title = """&rs("strtitle")""" target=""_blank"">"_   
  49.         &rs("Strtitle")"</a><br>"&VBCRLF   
  50.     Rs.MoveNext   
  51.     Loop  
  52.     Rs.close : set Rs=Nothing  
  53.     msg = msg &  "</ul></p>"&VBCRLF _   
  54.     "</td>"&VBCRLF _   
  55.     "</tr><tr><td> </td></tr><tr bgColor='#FF9D5C'><td height=3></td></tr>"&VBCRLF _   
  56.     "<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 _   
  57.     "</table><p></p></body></html>"  
  58.        
  59.     '取得邮件地址   
  60.     Dim i,total,jmail   
  61.     i = 1   
  62.     Dim BadMail '不接收的邮件列表 格式 '邮件地址','邮件地址'   
  63.     BadMail = "'123@163.com','122@126.com'"    
  64.     sql = "Select distinct b.stremail From userinfo a inner join userinfo_1 b "_   
  65.     "on a.id = b.intuserid and b.stremail <> '' and (charindex('3',a.StruserLevel)>0 or charindex('4',a.StruserLevel)>0) "_   
  66.     "and b.stremail not in ("&BadMail") "_   
  67.     "order by b.stremail"  
  68.     Set rs = CreateObject("Adodb.Recordset")   
  69.     rs.open sql,conn,1,1   
  70.     total = rs.recordcount   
  71.     If rs.eof Then    
  72.         Wscript.Echo "没有用户!"  
  73.         rs.close : Set rs = Nothing  
  74.         Exit Sub  
  75.     End If  
  76.   
  77.     '每二十个邮件地址发送一次   
  78.     For i = 1 To total   
  79.         If i Mod 20 = 1 Then  
  80.             Set jmail = CreateObject("JMAIL.Message")   '建立发送邮件的对象   
  81.             'jmail.silent = true    '屏蔽例外错误,返回FALSE跟TRUE两值   
  82.             jmail.Logging = True    '记录日志   
  83.             jmail.Charset = "GB2312"     '邮件的文字编码   
  84.             jmail.ContentType = "text/html"    '邮件的格式为HTML格式或纯文本   
  85.         End If  
  86.         jmail.AddRecipient rs(0)    
  87.         If i Mod 20 = 0 Or i = 665 Then  
  88.             jmail.From = "info At 51tiao"   '发件人的E-MAIL地址   
  89.             jmail.FromName = "上海跳蚤市场"   '发件人的名称   
  90.             jmail.MailServerUserName = "info"     '登录邮件服务器的用户名 (您的邮件地址)   
  91.             jmail.MailServerPassword = "123123"     '登录邮件服务器的密码 (您的邮件密码)   
  92.             jmail.Subject = "上海跳蚤市场今日推荐 "&Year(Date())"年"&Month(Date())"月"&Day(Date())"日"    '邮件的标题    
  93.             jmail.Body = msg      '邮件的内容   
  94.             jmail.Priority = 3      '邮件的紧急程序,1 为最快,5 为最慢, 3 为默认值   
  95.             jmail.Send("mail.51tiao.com")     '执行邮件发送(通过邮件服务器地址)   
  96.             jmail.Close()      
  97.             set jmail = Nothing  
  98.         End If  
  99.     rs.movenext   
  100.     Next  
  101.     rs.close : Set rs = Nothing  
  102.        
  103.     '记录日志在C:\jmail年月日.txt   
  104.     Const DEF_FSOString = "Scripting.FileSystemObject"  
  105.     Dim fso,txt   
  106.     Set fso = CreateObject(DEF_FSOString)   
  107.     Set txt=fso.CreateTextFile("C:\jmail"&DateValue(Date())".txt",true)   
  108.     txt.Write "邮件发送成功,共发送了"&total"封邮件,发送于 "&Now()"<Br><Br>"  
  109.     txt.Write jmail.log   
  110.     Set txt = Nothing  
  111.     Set fso = Nothing  
  112.     Wscript.Echo "邮件发送成功,共发送了"&total"封邮件,发送于 "&Now()   
  113. End Sub  
  114.   
  115. Sub CloseDB()   
  116.     If IsObject(conn) Then  
  117.         Conn.close : Set Conn = Nothing  
  118.     End If  
  119. End Sub  

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80722 Code detection by Codefense  theme by BokeZhuti

Copyright © 2005 - 2008 风易博客 1982Y.Net.Cn All Rights Reserved.  由Wind88.Net提供优质主机空间
沪ICP备05006417号