用VBS脚本发邮件

需求是这样的:针对帐号的管理,若是发现该帐号的管理员给帐号加了批注,(好比要过时,修改密码,完善资料等),就须要找到这样的帐号及其管理的邮件,而后发邮件给他们的管理员同时抄送给帐号以达到提醒的目的。那么咱们的实际项目中是这样管理的:html

有三个表,第一张表用来存放帐号的全部信息,以及这个帐号的备注,第二张表存放了帐号信息以及他的管理员的名字等信息,第三张表就存放管理的信息以及管理员的邮件地址。都是excel表服务器

思路是这样:首先在表一里找到全部备注栏不为空的帐号,而后把这些帐号拿到第二张表里去搜索,若是找到了就继续找出它对应的管理的名字,最后吧获得的管理员的名字拿到第三张表去搜索找到它的邮件地址,同时也须要把帐号和管理员邮件记录下来。网络

最后使用系统用户发邮件给全部的管理员,正文里就列出这些要作修改的帐号的基本信息。函数

其实这里就有2部分,第一部分主要是excel的处理,这一块应该不复杂,我会直接贴出代码,这里主要说明第二部分,就是邮件的发送。ui

CDO.Messagespa

想经过vbs脚原本发邮件,就须要用到CDO.Message这个对象,而后配置它的属性,好比邮件服务器,端口,认证方式,帐号密码等,同时也能够对邮件自己的属性作设置,好比邮件紧急度,乱码等。下面是代码:excel

function sendEmail(strEmail_From, strEmail_To, strCC_List, strEmail_Subject, strEmail_Body)

     Set cdoMail = CreateObject("CDO.Message")  '建立CDO对象
     Set cdoConf = CreateObject("CDO.Configuration") '建立CDO配置文件对象
     cdoMail.From = strEmail_From
     cdoMail.To = strEmail_To
     cdoMail.CC = strCC_List
     cdoMail.Subject = strEmail_Subject
    '邮件正文
     cdoMail.HTMLbody = strEmail_Body & "</table></body></html>"
   
     cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2    '使用网络上的SMTP服务器而不是本地的SMTP服务器
     'cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "9.56.224.215"
     cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.126.com"    'SMTP服务器地址, 能够换成其余你要用的邮箱服务器或者ip
     cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25    '邮件服务器端口
     cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1    '服务器认证方式
     cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@126.com" '发件人帐号
     cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123456"    '发件人登录邮箱密码
     cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60    '链接服务器的超时时间
     cdoConf.Fields.Update  
     Set cdoMail.Configuration = cdoConf
     
     '设置邮件的重要度和优先级
     cdoMail.Fields.Item("urn:schemas:mailheader:X-MSMail-Priority") = "High"
     cdoMail.Fields.Item("urn:schemas:mailheader:X-Priority") = 2 
     cdoMail.Fields.Item("urn:schemas:httpmail:importance") = 2 
     cdoMail.Fields.Update
     
     '发送邮件
     dim sleepSeconds     
     sleepSeconds = 5
     cdoMail.Send
     WScript.Sleep(1000 * sleepSeconds)
     
     Set cdoMail = nothing
     Set cdoConf = nothing
End function

而后就能够调用它来发邮件了code

sendEmail "xxx@126.com", "zzz@qq.com", "yyy@qq.com", "提示邮件", "take action"

 

下面是解析excel的代码:server

Set oExcel= CreateObject("Excel.Application")

Set oWb1 = oExcel.Workbooks.Open("E:\Bluecare\AA team\115531\US-NiS3.20150909150006Copy.csv")
Set oSheetUSNi = oWb1.Sheets("US-NiS3.20150909150006Copy")

Set oWb2 = oExcel.Workbooks.Open("E:\Bluecare\AA team\115531\IBM Monthly Report.xlsx")
Set oSheetIMR = oWb2.Sheets("Sheet1")

Set oWb3 = oExcel.Workbooks.Open("E:\Bluecare\AA team\115531\Sponsor email.xls")
Set oSheetSPO = oWb3.Sheets("Sheet1")

dim Dit1:set Dit1 = CreateObject("Scripting.Dictionary")
dim Dit2:set Dit2 = CreateObject("Scripting.Dictionary")

'输出文件路径
dim directory1, directory2
directory1 = "C:\\temp\\sponsor_mail_found.txt"
directory2 = "C:\\temp\\sponsor_withoutMail.txt"
directory3 = "C:\\temp\\account_withoutSponsor.txt"

'for function getExpireAcc: 第一个参数是sheetname,第二个是帐号的列号,第三个是备注列号,第四个是查询帐号的规则(好比查询以a开头的帐号)
'for function getData: 第一个是sheetname,第二个是须要查找的帐号,第三个是帐号列号,第三个是管理员列号,第五个是返回值
'下面就能够调用函数执行了,执行完成后能够去输出目录里看最终结果
outSpoMail getData(oSheetIMR, getExpireAcc(oSheetUSNi, "C","M","acl"),"A","K",Dit1), oSheetSPO

'Get impending deactivation account list from URT response file
'@param oSheet, sheet name
'@param colAccount, the 'account' column
'@param colAcctMgrAction, the 'account manager action' column
'@param strFilter,  the string used to filter the accounts that impending deactivation, eg: "U8"
Function getExpireAcc(oSheet, colAccount, colAcctMgrAction, strFilter)
    dim row, i, varacc, varama, temp
    row=oSheet.usedRange.Rows.count
    for i=2 to row
        varacc=oSheet.cells(i,colAccount)
        varama=CStr(oSheet.cells(i,colAcctMgrAction))
        if (instr(varacc,strFilter)=1) then
            temp = temp + varacc +"&"+varama+","
        end if
    Next
    dim j,spit, tmp
    spit=split(temp,",")
    for j=0 to ubound(spit)-1
        tmp = split(spit(j),"&")
        if tmp(1) = Empty or tmp(1) = "" or IsNull(tmp(1)) then
            getExpireAcc = getExpireAcc + tmp(0) + "_"
        end if
    next
end Function

'** Get sponsor name list from IBM Monthly Report spreadsheet
'@param oSheet, sheet name
'@param sourceAcct, the accounts that impending deactivation
'@param colAcctID, the account ID column 'Network ID'
'@param colSponsorName, the sponsor name column 'Sponsor'
'@param dicAcct_SponsorName, the dictionary to store the account ID and its sponsor name
Function getData(oSheet, sourceAcct, colAcctID, colSponsorName, dicAcct_SponsorName)
    dim m,n,roww, expacc,res, out
    expacc = split(sourceAcct,"_")
    roww = oSheet.usedRange.Rows.count
    for m=2 to roww
        for n=0 to ubound(expacc)-1
            if trim((oSheet.cells(m,colAcctID))) = trim(expacc(n)) then
                if oSheetIMR.cells(m,colSponsorName) = Empty or oSheetIMR.cells(m,colSponsorName) = "" or IsNull(oSheetIMR.cells(m,colSponsorName)) then
                    out = out + expacc(n)&vbcrlf
                else
                    dicAcct_SponsorName.add expacc(n),oSheetIMR.cells(m,colSponsorName)
                end if            
            end if
        next
    next
    writeTxt directory3, out
    set getData = dicAcct_SponsorName
end Function

'Get the sponsor mail address list from 'Sponsor_email' spreadsheet and write it out
Function outSpoMail(Dict,oSheet)
    Dim DictKeys, DictItems, Counter, out1, row, k, out2, out3
    row=oSheet.usedRange.Rows.count
    DictKeys = Dict.Keys
    DictItems = Dict.Items
    For Counter = 0 To Dict.Count - 1
        for k=2 to row
            if trim(DictItems(Counter))=trim(oSheet.cells(k,"A")) then
                WScript.Echo _
                    "key: " & DictKeys(Counter) & _
                    " value: " & DictItems(Counter)
                out1 = out1 + oSheet.cells(k,"B")&vbcrlf
                Dit2.add DictKeys(Counter), oSheet.cells(k,"B")
            end if
        out2 = out2 + oSheet.cells(k,"A") + "_"
        next
    Next
    set outSpoMail = Dit2
    'writeTxt(out)
    'write the sponsor mail to directory1
    writeTxt directory1, out1
    
    For Counter = 0 To Dict.Count - 1
        if instr(out2,trim(DictItems(Counter)))>0 then
            'msgbox "exist:"+ DictItems(Counter)
        else
            'msgbox "not exist:"+ DictItems(Counter)
            out3 = out3 + DictItems(Counter)&vbcrlf
        end if
    next
    'write the sponsor name which not found in sponsor file to directory2
    writeTxt directory2, out3
End Function

'输出文件
Function writeTxt(directory, content)
    dim fso
    set fso = CreateObject("Scripting.FileSystemObject")
    set f = fso.OpenTextFile(directory, 2, true)
    f.write(content)
    f.close
    set f = nothing
    set fso = nothing
End Function

oWb1.Close
oWb2.Close
oWb3.Close
oExcel.Quit
set oExcel=nothing
set Dit1=nothing
set Dit2=nothinghtm

WScript.Quit(0)

 

经过这2个vbs就能够到达需求的目的的,也能够将他们放在一个vbs里使用。