现在的位置: 首页 > 综合 > 正文

让Google-Gmail 能够自动化发送邮件

2018年05月11日 ⁄ 综合 ⁄ 共 1844字 ⁄ 字号 评论关闭

 

      Google的GMAIL相信大家都知道,基本上人人拥有,它以简约的风格和方便的操作赢得了许多专业人士的好评,这两天没事自己用IE自动化模型以及HTML DOM写了一个自动化GMAIL的VBScript脚本。

必要条件:

  • 因为是VBS,所有电脑都支持,不需要安装任何工具
  • 需要有Gmail帐号和密码

脚本如下:

'**********************定义变量**********************

Dim gmail_username ' GMAIL用户名

Dim gmail_password  ' GMAIL密码

Dim mail_to        ' 需要发送的邮箱
Dim mail_subject    ' 需要发送的标题

Dim mail_content    ' 需要发送的内容

'********************** 初始化 **********************

gmail_username=""  
gmail_password=""
mail_to= "zzxxbb112@163.com"
mail_subject="test"
mail_content="hi,zzxxbb112,您好"

'********************** script **********************

On Error Resume Next
'错误继续执行
Set oIE=CreateObject("InternetExplorer.Application")

'创建对象
oIE.Visible = True
'设置可见
oIE.Navigate "http://www.gmail.com"
'跳转URL
While oIE.Busy: Wend
'等待页面刷新完毕
With oIe.Document
    .GetElementById("Email").value=gmail_username
    .GetElementById("Passwd").value=gmail_password
    .GetElementsByName("signIn")(0).click
End With
'登录GMAIL
While oIE.Busy: Wend
'等待页面刷新完毕
WScript.Sleep 5000
'等待2秒
Set oWsh=CreateObject("wscript.shell")
oWsh.SendKeys "{enter}"
Set oWsh=Nothing
'安全框确定
oIE.Navigate "https://mail.google.com/mail/?ui=html&zy=e"

'跳转URL 标准HTML版本
While oIE.Busy: Wend
'等待页面刷新完毕
For Each a In oIE.Document.GetElementsByTagName("A")

    If a.innertext="撰写邮件" Then
        a.click
        Exit For
    End If
Next
'点击撰写邮件
While oIE.Busy: Wend
'等待页面刷新完毕
With oIE.Document
    .GetElementByID("to").value=mail_to
    .GetElementsByName("subject")(0).value=mail_subject
    .GetElementsByName("body")(0).innertext=mail_content
    .GetElementsByName("nvp_bu_send")(0).click
End With
'写MAIL
WScript.Sleep 3000
While oIE.Busy: Wend
'等待页面刷新完毕
For Each a In oIE.Document.GetElementsByTagName("A")

    If a.innertext="退出" Then
        a.click
        Exit For
    End If
Next
'点击退出
oIE.Quit
'关闭浏览器
Set oIE=Nothing
'释放资源
'********************** end **********************

使用方法:

  • 修改初始化中的5个参数,具体说明看脚本中的注释。
  • 另存为以上脚本为 *.vbs文件,双击直接运行。

总结:

此脚本同样适合学习自动化测试的朋友,IE自动化模型和HTML DOM对于WEB自动化测试是非常重要的两种技术,感兴趣的朋友也可以去W3C学习下。

 

抱歉!评论已关闭.