使用VBS发邮件

直接上代码吧!~

NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
set Email = CreateObject("CDO.Message")
Email.From = "zmaxxxxxxx@126.com" '发信人地址
Email.To = "4965655555548@qq.com" '收信人地址(qq也可)
Email.Subject = "花三千" '邮件主题
x="E:\基础知识\一键收发邮件.txt" '发信内容写在e:\111.txt中
y="E:\小试牛刀\bat2.txt" '这是需发送的附件
Set fso=CreateObject("Scripting.FileSystemObject")
Set myfile=fso.OpenTextFile(x,1,Ture)
c=myfile.readall
myfile.Close
Email.Textbody = c
Email.AddAttachment y
with Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = "smtp.126.com" '这是126邮箱的服务器地址
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = "zmaxxxxxxx" '发信人用户名
.Item(NameSpace&"sendpassword") = "mmaxxxxxxx" '发信人密码
.Update
end with
Email.Send
Set Email=Nothing
出处:http://oraclequ.blog.163.com/blog/static/1901071022008726360560/

=======================================================================================

原来的代码是这样的:
'By Jerry Ye
On Error Resume Next
f=InputBox("From Email:","msg","username@163.com")
smtp=InputBox("SMTP:","msg","smtp.163.com")
u=InputBox("User:","msg","username")
p=InputBox("Pass:","msg","password")
t=InputBox("To Email:","msg","username@126.com")
m=InputBox("邮件主题:","msg","Test send mail")
msg=InputBox("邮件内容:","msg","Send success")
j=InputBox("附件路径:","msg","C:\test.rar")
NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
Set Email = createObject("CDO.Message")
Email.From = f
Email.To = t
Email.Subject = m
Email.Textbody = msg
Email.AddAttachment j
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = smtp
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = u
.Item(NameSpace&"sendpassword") = p
.update
End With
Email.Send
出处:http://zhidao.baidu.com/link?url=cw_E07n8VhyxUqA2jWdLbnr1GbT4StICLYgH_GsZGZOmPv4m4NBAiI7kv789qFr1MclePeAiwLgP__LBpsHd_K

=======================================================================================

'用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>今日推荐信息&nbsp;&nbsp;"&Year(Date())&"年"&Month(Date())&"月"&Day(Date())&"日&nbsp; <a href=""http://www.51tiao.com"" target=""_blank""><FONT size=3><b>上海跳蚤市场</b></font></a>&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table></div></td></tr></table>"&VBCRLF _
    &"<table width=640>"&VBCRLF _
    &"<tr bgColor='#FF9D5C'><td height=3></td></tr><tr><td>&nbsp;</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>&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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://blog.csdn.net/flynetcn/article/details/1484471

 

=======================================================================================

使用批处理命令行自动发送邮件

因为工作中,涉及到对用户的数据库进行备份,为了把人从重复的工作中解放出来,同时也提高备份的准确性,需要一个自动备份并发送通知的工具,找了好久都没有现成方案,最后只能自己动手写,基本原理是通过批处理脚本 调用备份脚本后,拼写备份日志信息,并调用自动通知组件,将备份的日志信息发送到监控人邮件.监控人只要每天自动接收邮件就可以了.

DOC命令如下:

@rem 写入日志,永久存放 
echo. >> %cur_log%.txt 
echo *********************** start  
************************************** >> %cur_log%.txt 
echo %date% %time:~0,8%  >> %cur_log%.txt  
echo xcopy.exe %%~nxi %des_dir%  /f /y   >> %cur_log%.txt 
echo sueccess >> %cur_log%.txt 
echo. >> %cur_log%.txt  
echo xcopy.exe %%~nxi %des_dir2%  /f /y   >> %cur_log%.txt 
echo sueccess >> %cur_log%.txt 
echo *********************** end  
************************************** >> %cur_log%.txt 
echo. >> %cur_log%.txt 
  
@rem 写入邮件日志,作为邮件内容 
echo %cur_file%同步日志>> %maillog%.txt 
echo *********************** start  
************************************** >> %maillog%.txt 
echo %date% %time:~0,8%  >> %maillog%.txt  
echo xcopy.exe %%~nxi %des_dir%  /f /y  >> %maillog%.txt 
echo sueccess >> %maillog%.txt 
echo. >> %maillog%.txt  
echo xcopy.exe %%~nxi %des_dir2%  /f /y  >> %maillog%.txt 
echo sueccess >> %maillog%.txt 
echo *********************** end  
************************************** >> %maillog%.txt 
echo. >> %maillog%.txt 
  
SENDMAIL.vbs 
Exit 

自动发送邮件脚本
以上是调用发送邮件的批处理脚本,具体发送邮件的脚本我封装成了一个单独的文件,可以作为组件调用. 见SENDMAIL.vbs,代码如下:

  Set cdo = CreateObject("CDO.Message") '创建CDO.Message对象  
  strCfg = "http://schemas.microsoft.com/cdo/configuration/" '规定必须是这个 
  With cdo '用with关键字减少代码输入  
    '配置 
    .Configuration(strCfg & "SendUsing") = 2 '使用网络上的SMTP服务器而不是本地的SMTP服务器 
    .Configuration(strCfg & "smtpserver") = "****" 'SMTP服务器地址  
    .Configuration(strCfg & "sendusername") = "****" '发件人用户名 
    .Configuration(strCfg & "sendpassword") = "****" '发件人邮箱密码  
    .Configuration(strCfg & "smtpauthenticate")=1 '服务器认证方式  
    .Configuration(schema & "smtpserverport") = 465 'SMTP服务器端口  
    .Configuration(schema & "smtpusessl") = True '是否使用SSL  
    .Configuration(schema & "smtpconnectiontimeout") = 60 '连接服务器的超时时间  
    .Configuration.Fields.Update
          
    '邮件头 
    .Sender = "xxxxxx@sina.com" '发件人  
    .From = "xxxxxx@sina.com" '发件件人地址  
    .To = "xxxxxx@sina.com;t-zhangyong035.slyt@sina.com" '收件人地址,多个收件人使用英文分号分割
    .Fields("urn:schemas:mailheader:X-Priority") = 1    
    .Fields.Update    
   
    '邮件内容  
    '.Subject = "备份压缩同步日志"  
    '.TextBody = "备份情况"  
  
    '当前文件夹  
    curdir = left(wscript.scriptfullname,instrrev(wscript.scriptfullname,"\")-1) 
      
    '将文本内容写成邮件内容 
    x="mail_log.txt" 
      
    Set fso=CreateObject("Scripting.FileSystemObject") 
    Set myfile=fso.OpenTextFile(x,1,Ture) 
    firstline = myfile.readline 
    c=myfile.readall 
    myfile.Close 
    .Textbody = c 
      
    '邮件标题改成动态的,从文本里取第一行  
    .Subject = firstline &"日志"  
      
    '附件  必须是绝对路径  
    '.AddAttachment curdir &"\" & "mylog.txt"  
    '.AddAttachment curdir &"\" & "winrar400sc.exe" 
      
    '发送 
    .send   
  EndWith    
  'msgbox "发送完成"

 

根据不同监控要求,设定备份日期,调用备份脚本后,将备份结果自动发送到监控者邮箱,可大大方便备份的监控工作。 

 

出处:https://wenku.baidu.com/view/b45202c6dc88d0d233d4b14e852458fb760b385a.html

https://www.cnblogs.com/leanfish/p/4693236.html

=======================================================================================

能否用批处理实现自动发邮件

我想在一台Windows Server 2003上写个批处理,每隔一小时发一封email给我。在UNIX下很容易实现,一条mailx命令足矣。请问怎样在Windows下用批处理实现呢(这台服务器上有Outlook Express)?

---------------------------------------------------------------------------------------

在评论区中给出了方法

'把以下文件存为vbs文件,双击就可运行,也可以放到Job里去运行。
Set cdo = CreateObject("CDO.Message")
strCfg = "http://schemas.microsoft.com/cdo/configuration/"
With cdo
.Sender = "发送者邮件地址"
.From = "发送者邮件地址"
.To = "接收者邮件地址"   '多个收件人地址之间使用逗号分隔。
.Fields("urn:schemas:mailheader:X-Priority") = 1
.Fields.Update
.Subject = "邮件标题"
.TextBody = "邮件内容"   '如果需要支持HTML邮寄内容,把TextBody替换使用HtmlBody
.Configuration(strCfg & "smtpauthenticate") = 1
.Configuration(strCfg & "SendUsing") = 2
.Configuration(strCfg & "smtpserver") = "发送邮件服务器地址"
.Configuration(strCfg & "sendusername") = "发送者邮箱帐号"
.Configuration(strCfg & "sendpassword") = "发送者邮箱密码"
.Configuration.Fields.Update
.send
End With
msgbox "发送完成"

'以上程序因网络限制无法作测试,不知是否可用,请各位看官自行测试吧。

个人使用

这个也是我自己最常用来测试的 

将以上代码保存成sendEmail.vbs,文件编码使用ANSI格式,否则中文可能会有乱码,通过命令进入到该目录下,输入sendEmail.vbs执行。

VBScript在windows环境下调试:在命令中输入cscript sendemail.vbs //x,可以进入VS环境调试。

CDO对象的MSDN参考:
http://msdn.microsoft.com/en-us/library/ms526861%28v=exchg.10%29
CDOSYS就是IIS中的CDO,CDOEX就是exchange server中的CDO,具体看下面CDO对象路线图:
http://msdn.microsoft.com/en-us/library/ms978698.aspx

 

出处:https://bbs.csdn.net/topics/100177135?list=1125343

=======================================================================================

 

posted on 2013-11-11 14:02  jack_Meng  阅读(1421)  评论(0编辑  收藏  举报

导航