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

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

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

最后使用系统用户发邮件给所有的管理员,正文里就列出这些要做修改的账号的基本信息。

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

CDO.Message

想通过vbs脚本来发邮件,就需要用到CDO.Message这个对象,然后配置它的属性,比如邮件服务器,端口,认证方式,账号密码等,同时也可以对邮件本身的属性做设置,比如邮件紧急度,乱码等。下面是代码:

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") = '使用网络上的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") = '邮件服务器端口
cdoConf.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = '服务器认证方式
cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx@126.com" '发件人账号
cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "" '发件人登陆邮箱密码
cdoConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = '连接服务器的超时时间
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") =
cdoMail.Fields.Item("urn:schemas:httpmail:importance") =
cdoMail.Fields.Update '发送邮件
dim sleepSeconds
sleepSeconds =
cdoMail.Send
WScript.Sleep( * sleepSeconds) Set cdoMail = nothing
Set cdoConf = nothing
End function

然后就可以调用它来发邮件了

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

下面是解析excel的代码:

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= to row
varacc=oSheet.cells(i,colAccount)
varama=CStr(oSheet.cells(i,colAcctMgrAction))
if (instr(varacc,strFilter)=) then
temp = temp + varacc +"&"+varama+","
end if
Next
dim j,spit, tmp
spit=split(temp,",")
for j= to ubound(spit)-
tmp = split(spit(j),"&")
if tmp() = Empty or tmp() = "" or IsNull(tmp()) then
getExpireAcc = getExpireAcc + tmp() + "_"
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= to roww
for n= to ubound(expacc)-
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 = To Dict.Count -
for k= 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 = To Dict.Count -
if instr(out2,trim(DictItems(Counter)))> 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, , 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=nothing

WScript.Quit(0)

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

用VBS脚本发邮件的更多相关文章

  1. 【VBS】发邮件

    Sub SendMail(pMailFrom, pMailTo, pSubject, pMailBody, pMailSmtpServer) On Error Resume Next Dim objS ...

  2. python smtp 发邮件 添加附件

    # -*- coding:utf-8 -*- # __author__ = 'justing' import os import smtplib from email.mime.multipart i ...

  3. [转]简单三步,用 Python 发邮件

    https://zhuanlan.zhihu.com/p/24180606 0. 前言 发送电子邮件是个很常见的开发需求.比如你写了个监控天气的脚本,发现第二天要下雨,或者网站上关注的某个商品降价了, ...

  4. SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享

    SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...

  5. 【Python系列】Python自动发邮件脚本-html邮件内容

    缘起 这段时间给朋友搞了个群发邮件的脚本,为了防止进入垃圾邮件,做了很多工作,刚搞完,垃圾邮件进入率50%,觉得还不错,如果要将垃圾邮件的进入率再调低,估计就要花钱买主机了,想想也就算了,先发一个月, ...

  6. vbs脚本实现qq定时发消息(初级)

    vbs脚本实现QQ消息定时发送 目标 批处理又称为批处理脚本,强大的强大功能可以高效得实现很多功能,例如批量更改文件格式,批量进行文件读写,今天我们的目标是用vbs脚本编写可以发送qq消息的脚本,并利 ...

  7. Windows下bat脚本自动发邮件

    摘要:说明:很多木马会利用自身的程序截取系统敏感文件或信息发往指定的邮箱,而blat并不是木马,它小巧却有强大的发邮件功能,可不要用它做违法事,感觉和木马功能有一拼!下面先看个具体的实例(在blat同 ...

  8. #题目:有10 台被监控主机、一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次

    #题目:有10 台被监控主机.一台监控机,在监控机上编写脚本,一旦某台被监控机器/ 分区适用率大于80%, 就发邮件报警放到crontab 里面, 每10 分钟检查一次 #测试机器:虚拟机Linux ...

  9. 发邮件的python脚本

    1. 编写一个最简单的发邮件的python脚本   #coding: utf-8 import smtplib from email.mime.text import MIMEText from em ...

随机推荐

  1. Android 动画的重复播放

    如果你要的加载xml方式设置动画,而且在xml定义了好几个动画元素比如:<set>    <alpha android:repeatCount="-1" andr ...

  2. unity3d泰斗破坏神2----课程列表

    免费 课时1:泰斗破坏神第一支预告视频 01:32免费 课时2:泰斗破坏神第二支预告视频 01:58第 1 章 : 游戏开始 用户登录 服务器选择课时3:游戏开始 用户登录 服务器选择课时4:素材介绍 ...

  3. Linux中断处理体系结构分析

    Linux中断处理体系结构分析(一) 异常,就是可以打断CPU正常运行流程的一些事情,比如外部中断.未定义指令.试图修改只读的数据.执行swi指令(Software Interrupt Instruc ...

  4. openfire开发

    openfire github地址:https://github.com/igniterealtime/Openfire 1.下载源代码:http://www.igniterealtime.org/d ...

  5. Xcode学习

    http://www.cnblogs.com/ygm900/p/3488881.html

  6. 【POJ】3523 The Morning after Halloween

    1. 题目描述$m \times n$的迷宫(最大为$16 \times 16$)包含最多3个点,这些点可以同时向相邻方向移动或保持停止,使用小写字母表示起始位置,使用大写字母表示中止位置.求最少经过 ...

  7. Word 中没有Endnote工具栏的解决方法

    环境:Windows XP + Word 2003 + EndNote 6 以下各方法可以依次试一下,需要重启Word后才能看到是否可行.1 视图 -- 工具栏 -- EndNote,是否打勾.2 w ...

  8. R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties

    Warning message: In ks.test(x, y) : p-value will be approximate in the presence of ties   The warnin ...

  9. 基于XMPP的即时通信系统的建立(三)— 程序设计概览

    XMPP与HTTP的比较 XMPP的优势 Ÿ   1. 推送数据 HTTP只能从服务器哪里请求数据,除非服务器正在响应客户端请求,否则不能向客户端发送数据.但XMPP连接是双向的,任何一方在任何时候都 ...

  10. UVa 11427 (期望 DP) Expect the Expected

    设d(i, j)表示前i局每局获胜的比例均不超过p,且前i局共获胜j局的概率. d(i, j) = d(i-1, j) * (1-p) + d(i-1, j-1) * p 则只玩一天就就不再玩的概率Q ...