如何在不通过testcomplete在PC中打开Outlook的情况下发送Outlook邮件

时间:2014-03-27 13:04:04

标签: email vbscript outlook testcomplete

enter image description here我正在尝试使用testcomplete中的vb脚本语言发送Outlook邮件。我能够发送,但如果在我的电脑中打开前景,那么只会发送邮件,如果我的电脑中没有打开oulook,我的邮件将不会发送,直到执行此脚本后仍然打开outllok

这是我的代码:

Function SendMail

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

strSubject="test"
strTo=yyy@yy.com
strCc=XXX@XX.com
strBcc =zzz@zzz.com
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="XXXXXX@XXXX.com"
' 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 

请建议我如何处理这个...提前感谢

2 个答案:

答案 0 :(得分:3)

你真的需要发送它吗?

我使用此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;
}

答案 1 :(得分:0)

尝试objNewMail.Body.Send而不是尝试执行发送和接收按钮。这是一个简单的例子:

Function SendMail()
    Dim objOutlook, objNewMail
    Set objOutLook = CreateObject("Outlook.Application")    
    Set objNewMail = objOutLook.CreateItem(olMailItem)  
    objNewMail.TO = "myrecip@test.com"
    objNewMail.Subject = "test"
    objNewMail.Body = "test"
    objNewMail.Send
End Function