手把手教你TestComplete_自动发邮件

来源:互联网 发布:泰达网络 编辑:程序博客网 时间:2024/05/01 04:31
 

  

 

         一看了这个标题您可能就笑了,这么简单的东东,TESTCOMPLETE里面提供了两种SendMail函数的:

 

第一个:BuiltIn中提供的

 

BuiltIn.SendMail(ToAddress, FromHost, FromName, FromAddress, Subject, Body, FileName1, FileName2..FileNameN) Parameters ToAddress [in] Required String 

 

参数说明:

FromHost [in] Required String 

FromName [in] Required String 

FromAddress [in] Required String 

Subject [in] Required String 

Body [in] Required String 

FileName1, FileName2..FileNameN [in] Optional String 

Result     Boolean  

 

 

第二个:TestComplete内置的SendMail函数

 

SendMail(ToAddress, FromHost, FromName, FromAddress, Subject, Body, FileName1, FileName2..FileNameN)

 

Parameter  Description 

ToAddress Required. The e-mail address to which the message will be sent. 

FromHost Required. The host from which the message will be sent. 

FromName Required. The "user-friendly" name of the e-mail sender. 

FromAddress Required. The e-mail address from which the message will be sent. 

Subject Required. The message subject. 

Body Required. The message body.

FileName1, FileName2..FileNameN Optional. The full paths to the files you want to send with the message.

 

 

都附带了例子的,非常简单。Don’t tell me the stuff from TESTCOMPLETE HELP.

 

刚看到的时候,我也很高兴,觉得已经搞定了,后来发现代码运行都能通过,但是邮箱里面就是收不到传说中已经发出的邮件。

 

一定是哪里出了问题,可能是我点背,要是哪位仁兄使用上面俩函数能成功发出邮件的,望不吝赐教。

 

 

         在折腾完上面两个函数后,不对,是在被上面两个函数折腾完后,开始了自己的山寨方法。

 

先down了一个绿色版本的命令行发邮件工具blat.exe。试了试他的使用方法,两个步骤:

blat.exe -install smtp.qq.com 123456@qq.com 3 25

blat.exe body.txt -to 54321@qq.com -u 123456@qq.com -pw 123456 -subject "标题" -attach "01.rar,02.rar"

说明:

1.         123456@qq.com是From邮箱

2.         3 25:代表的是发送不成功的话,可以重发三次,25是smtp的缺省端口;

3.         body.txt:是提前做好的文件内容文件;

4.         attach是附件

 

反正挺简单的,设置好了一跑,没两秒邮箱就收到邮件了。

 

这下就只需要在TestComplete中调用一下了,在此之前,因为我的附件不止一个,所以还需要先构建出attach后面的附件清单,如果没有的话,就直接取消attach的参数。

 

参考代码如下:

 

 

sub MySendMail

 

dim strDir,strExt,strDir1,strOut1

 

strDir=Project.Path & "\data\"

strExt="*.csv"

strOut1=FstrGetCsv(strDir,strExt)

 

strDir1=Project.Path & "\sendmail\"

 

dim strCommand

strCommand=strDir1 & "\blat.exe -install smtp.qq.com 123456@qq.com 3 25" & vbCrLf

strCommand=strCommand & strDir1 & "blat.exe " & strDir1 & "\body.txt -to 654321@qq.com "

 

if strOut1="" then

  strCommand=strCommand & " -s ""结果文件"" -u 123456@qq.com -pw 123456 -charset Gb2312" & vbcrlf & vbcrlf 

Else

  strCommand=strCommand & " -attach """ & strOut1 & """ -s ""结果文件"" -u 123456@qq.com -pw 123456 -charset Gb2312" & vbcrlf & vbcrlf

  call ExecCmd(strCommand) 

end If

 

end Sub

 

Sub ExecCmd(strCommand)

  Dim WshShellObj, WshShellExecObj, out

 

  Set WshShellObj = CreateObject("WScript.Shell")

  Set WshShellExecObj = WshShellObj.Exec("cmd.exe")

 

   ' Flush the stream

   out = readTillChar(WshShellExecObj, ">")

   Log.Message(out)

  

   ' Send the "ver" command and the new line character

   WshShellExecObj.StdIn.Write(strCommand)

   out = readTillChar(WshShellExecObj, ">")

   Log.Message(out)

End Sub

 

Function readTillChar(WshShellExecObj, endChar)

  Dim out, curChar

 

  Do While Not WshShellExecObj.StdOut.AtEndOfStream

   curChar = WshShellExecObj.StdOut.Read(1)

   out = out + curChar

   If (curChar = endChar) Then

   readTillChar = out

   Exit Function

   End If

  Loop

End Function

 

 

 blat附件:

http://download.csdn.net/detail/Testingba/3633399