2014-03-27 127 views
0

enter image description here我想在testcomplete中使用vb腳本語言發送Outlook郵件。我可以發送,但如果outlook在我的電腦中打開,那麼只有郵件將被髮送,並且如果在我的電腦中未打開郵件,我的郵件將不會被髮送,直到執行此腳本後仍然打開郵件。如何通過testcomplete在PC中打開outlook展望郵件outlook郵箱

這裏是我的代碼:

Function SendMail 

Dim objOutLook, NamespaceMAPI,objNewMail, fso, SendReceiveControls 
Dim strTo,strCc ,strBcc ,strSubject, AccountName,strAttachmentPath 

strSubject="test" 
[email protected] 
[email protected] 
strBcc [email protected] 
strAttachmentPath="c:\text.txt" 


If strTo ="" and strCc = "" and strBcc =""Then 
Exit function 
ElseIf strSubject =""Then 
Exit function 
End If 

Set objOutLook = CreateObject("Outlook.Application")  
Set NamespaceMAPI = objOutLook.GetNamespace("MAPI") 
Set objNewMail = objOutLook.CreateItem(olMailItem) 
objOutLook.DisplayAlerts =True 
objNewMail.TO = strTo 
objNewMail.CC = strCc 
objNewMail.BCC=strBcc 
objNewMail.Subject = strSubject 
objNewMail.Body = strMsg 

If strAttachmentPath <> "" Then 
Set fso =CreateObject("Scripting.FileSystemObject") 
If fso.FileExists(strAttachmentPath) Then 
    objNewMail.Attachments.Add(strAttachmentPath) 

    objNewMail.display 

Else 
    msgbox "Attachment File Does not exists" 
End If 
End If 
AccountName="[email protected]" 
' Finding the "Send/Receive" control 
    Set SendReceiveControls = NamespaceMAPI.GetDefaultFolder("Inbox")._ 
       CommandBars("STANDARD").Controls("Send/Receive") 

    Set Item = Nothing 
    'msgbox "send:"&SendReceiveControls.Controls.Count 
For I = 1 To SendReceiveControls.Controls.Count 

    If InStr(SendReceiveControls.Controls(I).Caption, AccountName) > 0 Then 
     Set Item = SendReceiveControls.Controls(I) 
     'msgbox "send1"&SendReceiveControls.Controls(I) 
     Exit For 
    End If 

Next 
' Executing the "Send/Receive" action 
    Item.Controls(1).Execute() 

objOutLook.Quit 

''''''' Releasing objects ''''''' 
Set objOutLook =Nothing 
Set objNewMail = Nothing 
Set fso = Nothing 
End Function 

請建議我如何處理這...在此先感謝

回答

0

嘗試objNewMail.Body.Send,而不是試圖執行的發送和接收按鈕。這裏有一個簡單的例子:

Function SendMail() 
    Dim objOutlook, objNewMail 
    Set objOutLook = CreateObject("Outlook.Application")  
    Set objNewMail = objOutLook.CreateItem(olMailItem) 
    objNewMail.TO = "[email protected]" 
    objNewMail.Subject = "test" 
    objNewMail.Body = "test" 
    objNewMail.Send 
End Function 
+0

是的,我已經使用了上面的函數,但我在Outlook中彈出一個窗口(請參閱上面提供的屏幕截圖),我怎樣才能通過VB腳本來處理這個問題。是否需要發送郵件的服務器地址,如果它需要然後如何提供smtp服務器地址和端口id到該腳本發送電子郵件到outlook由testcomplete –

+0

要解決彈出我使用下面的代碼,但它得到運行時錯誤。設置omailWindow = Description.Create #here我得到運行時錯誤 omailWindow( 「標題」)值= emailcaption sTitle = 「稱號:=微軟Office Outlook中」。 如果窗口(omailWindow).Window(sTitle)。存在然後 Window(omailWindow).Window(sTitle).winbutton(「text =&Allow」)。點擊 EndIf請幫助我如何通過vb腳本處理彈出窗口 –

3

你真的需要用outlook發送嗎?

我用這個JavaScript代碼來發送電子郵件,然後在Outlook中,您只需將其標記爲非垃圾郵件,否則,就直接進入到垃圾郵件的收件箱

function SendEmail(mFrom, mTo, mSubject, mBody, username, password) 
{ 
    var i, schema, mConfig, mMessage; 

    try 
    { 
    schema = "http://schemas.microsoft.com/cdo/configuration/"; 
    mConfig = Sys.OleObject("CDO.Configuration"); 
    mConfig.Fields.Item(schema + "sendusing") = 2; // cdoSendUsingExchange 
    mConfig.Fields.Item(schema + "smtpserver") = "STMP SERVER ADDRESS HERE"; // SMTP server 
    mConfig.Fields.Item(schema + "smtpserverport") = 25; // Port number 
    mConfig.Fields.Item(schema + "smtpauthenticate") = 1; // Authentication mechanism 
     mConfig.Fields.Item(schema + "sendusername") = username; // User name (if needed) 
    mConfig.Fields.Item(schema + "sendpassword") = password; // User password (if needed) 
    mConfig.Fields.Update(); 

    mMessage = Sys.OleObject("CDO.Message"); 
    mMessage.Configuration = mConfig; 
    mMessage.From = mFrom; 
    mMessage.To = mTo; 
    mMessage.Subject = mSubject; 
    mMessage.HTMLBody = mBody; 

    mMessage.Send(); 
    } 
    catch (exception) 
    { 
    Log.Error("E-mail cannot be sent", exception.description); 
    return false; 
    } 
    Log.Message("Message to <" + mTo + "> was successfully sent"); 
    return true; 
} 
相關問題