QTP也可以实现从outlook自动发送邮件,如果要做邮件自动化测试,这是个很好的方法。
注意:需要设置outlook,选项-信任中心-编程访问,设置为:从不发出可疑警告,自动化测试方可完成。
关键代码如下:
Function sendMailFromOutLook(SendTo,Subject,MailBody,Attachment,MailNumber)
Set oWsh = CreateObject("WScript.Shell")
oWsh.Run "OutLook" 'start outlook
wait 5
Dim i
oWsh.SendKeys "{ESC}" 'close the warning box
Set oWsh = Nothing
For i = 1 To MailNumber
Set oMail = CreateObject("Outlook.Application")
Set Mail = oMail.CreateItem(0)
Mail.to = SendTo
Mail.Subject = Subject
Mail.Body = MailBody
If Attachment<>"" Then
Mail.Attachments.Add(Attachment)
End If
Mail.send
'print "第 "&i&" 封邮件已发送"
Set Mail = Nothing
Set oMail = Nothing
wait 5
Next
End Function
Set oWsh = CreateObject("WScript.Shell")
oWsh.Run "OutLook" 'start outlook
wait 5
Dim i
oWsh.SendKeys "{ESC}" 'close the warning box
Set oWsh = Nothing
For i = 1 To MailNumber
Set oMail = CreateObject("Outlook.Application")
Set Mail = oMail.CreateItem(0)
Mail.to = SendTo
Mail.Subject = Subject
Mail.Body = MailBody
If Attachment<>"" Then
Mail.Attachments.Add(Attachment)
End If
Mail.send
'print "第 "&i&" 封邮件已发送"
Set Mail = Nothing
Set oMail = Nothing
wait 5
Next
End Function