Sending Email From Scripts

Applies to TestComplete 15.47, last modified on January 20, 2023

The sections below describe how you can send email messages from your script tests:

By Using the SendMail Function

TestComplete includes a built-in SendMail function. You can use it to send email messages, including messages with files attached to them.

The function has the following syntax:

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

Parameter Description
ToAddress Required. The email 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 email sender.
FromAddress Required. The email 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.

SendMail returns True if the message has been sent successfully, else - False.

The following example shows how you can use the SendMail function in your tests:

Note: Replace the mockup host name and email credentials in the sample code below with the actual host name and credentials to make the sample work correctly.

JavaScript, JScript

function Test()
{
  if (SendMail("[email protected]", "mail.johnsmithcorp.com", "John Smith", "[email protected]", "Notification", "Hello Clare, Your application is nice.", "C:\\File1.txt", "C:\\File2.txt"))
    Log.Message("Mail was sent");
  else
    Log.Warning("Mail was not sent");
}

Python

def Test():
  if SendMail("[email protected]", "mail.johnsmithcorp.com", "John Smith", "[email protected]", "Notification", "Hello Clare, Your application is nice.", "C:\\File1.txt", "C:\\File2.txt"):
    Log.Message("Mail was sent")
  else:
    Log.Warning("Mail was not sent")

VBScript

Sub Test
  If SendMail("[email protected]", "mail.johnsmithcorp.com", "John Smith", "[email protected]", "Notification", "Hello Clare, Your application is nice.", "C:\File1.txt", "C:\File2.txt") Then
    Log.Message "Mail was sent"
  Else
    Log.Warning "Mail was not sent"
  End If
End Sub

DelphiScript

procedure Test;
begin
  if SendMail('[email protected]', 'mail.johnsmithcorp.com', 'John Smith', '[email protected]', 'Notification', 'Hello Clare, Your application is nice.', 'C:\File1.txt', 'C:\File2.txt') then
    Log.Message('Mail was sent')
  else
    Log.Warning('Mail was not sent');
end;

C++Script, C#Script

function Test()
{
  if (SendMail("[email protected]", "mail.johnsmithcorp.com", "John Smith", "[email protected]", "Notification", "Hello Clare, Your application is nice.", "C:\\File1.txt", "C:\\File2.txt"))
    Log["Message"]("Mail was sent");
  else
    Log["Warning"]("Mail was not sent");
}

By Using Collaboration Data Objects (CDO)

Collaboration Data Objects (CDO) is a collaboration component for Microsoft Exchange Server. You can use CDO to send email messages from your tests. This way you will be able to send messages to SMTP servers that require authentication. You can also specify a port number when using this approach.

Notes:

  • If your server requires authentication, you need to specify the user name and password in CDO.Configuration. Some servers require two-step verification. In this case, generate an application-specific password for your account and use it in your script instead of the account password.

  • If you send messages from a Gmail address, the Allow less secure apps option must be enabled for your account:

    See instructions

The sample code below demonstrates how to send an email message with several attachments. Attachment paths are specified in a list and are separated by commas. To parse the list, we use the aqString.ListSeparator, aqString.GetListLength and aqString.GetListItem functions built into TestComplete. Replace the mockup server name, server port and email addresses in the sample code below with the actual server name, port and addresses to make the sample work correctly.

JavaScript

function SendEmail(mFrom, mTo, mSubject, mBody, mAttach)
{
  var schema, mConfig, mMessage;

  try
  {
    schema = "http://schemas.microsoft.com/cdo/configuration/";
    mConfig = getActiveXObject("CDO.Configuration");
    mConfig.Fields.$set("Item", schema + "sendusing", 2); // cdoSendUsingPort
    mConfig.Fields.$set("Item", schema + "smtpusessl", 1); // Use SSL
    mConfig.Fields.$set("Item", schema + "smtpserver", "ServerName"); // SMTP server
    mConfig.Fields.$set("Item", schema + "smtpserverport", 465); // Port number

    // If you use Gmail --
    // Enable the Allow less secure apps option for your account
    // mConfig.Fields.$set("Item", schema + "smtpserver", "smtp.gmail.com");
    // mConfig.Fields.$set("Item", schema + "smtpserverport", 465);

    // If you use Outlook --
    // mConfig.Fields.$set("Item", schema + "smtpserver", "smtp-mail.outlook.com");
    // mConfig.Fields.$set("Item", schema + "smtpserverport", 587);

    // If you use Office365 --
    // mConfig.Fields.$set("Item", schema + "smtpserver", "smtp.office365.com");
    // mConfig.Fields.$set("Item", schema + "smtpserverport", 587);

    mConfig.Fields.$set("Item", schema + "smtpauthenticate", 1); // Authentication mechanism

    // User name (if needed)
    // mConfig.Fields.$set("Item", schema + "sendusername", "[email protected]");

    // User password (if needed)
    // mConfig.Fields.$set("Item", schema + "sendpassword", "********");

    mConfig.Fields.Update();

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

    aqString.ListSeparator = ",";
    for(let i = 0; i < aqString.GetListLength(mAttach); i++)
      mMessage.AddAttachment(aqString.GetListItem(mAttach, i));
    mMessage.Send();
  }
  catch (exception)
  {
    Log.Error("Email cannot be sent", exception.message);
    return false;
  }
  Log.Message("Message to <" + mTo + "> was successfully sent");
  return true;
}

function MainTest()
{
  if (SendEmail("[email protected]", "[email protected]", "Subject",
             "Message body", "c:\\File1.txt,c:\\File2.txt,C:\\File3.txt"))
    // Message was sent
  else
    // Message was not sent
}

JScript

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

  try
  {
    schema = "http://schemas.microsoft.com/cdo/configuration/";
    mConfig = Sys.OleObject("CDO.Configuration");
    mConfig.Fields.Item(schema + "sendusing") = 2; // cdoSendUsingPort
    mConfig.Fields.Item(schema + "smtpusessl") = 1; // Use SSL
    mConfig.Fields.Item(schema + "smtpserver") = "ServerName"; // SMTP server
    mConfig.Fields.Item(schema + "smtpserverport") = 465; // Port number

    // If you use Gmail --
    // Enable the Allow less secure apps option for your account
    // mConfig.Fields.Item(schema + "smtpserver") = "smtp.gmail.com";
    // mConfig.Fields.Item(schema + "smtpserverport") = 465;

    // If you use Outlook --
    // mConfig.Fields.Item(schema + "smtpserver") = "smtp-mail.outlook.com";
    // mConfig.Fields.Item(schema + "smtpserverport") = 587;

    // If you use Office365 --
    // mConfig.Fields.Item(schema + "smtpserver") = "smtp.office365.com";
    // mConfig.Fields.Item(schema + "smtpserverport") = 587;

    mConfig.Fields.Item(schema + "smtpauthenticate") = 1; // Authentication mechanism
    // mConfig.Fields.Item(schema + "sendusername") = ""; // User name (if needed)
    // mConfig.Fields.Item(schema + "sendpassword") = ""; // 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;

    aqString.ListSeparator = ",";
    for(i = 0; i < aqString.GetListLength(mAttach); i++)
      mMessage.AddAttachment(aqString.GetListItem(mAttach, i));
    mMessage.Send();
  }
  catch (exception)
  {
    Log.Error("Email cannot be sent", exception.description);
    return false;
  }
  Log.Message("Message to <" + mTo + "> was successfully sent");
  return true;
}

function MainTest()
{
  if (SendEmail("[email protected]", "[email protected]", "Subject",
             "Message body", "c:\\File1.txt,c:\\File2.txt,C:\\File3.txt"))
    // Message was sent
  else
    // Message was not sent
}

Python

def SendEmail(mFrom, mTo, mSubject, mBody, mAttach):
  try:
    schema = "http://schemas.microsoft.com/cdo/configuration/"
    mConfig = Sys.OleObject["CDO.Configuration"]
    mConfig.Fields.Item[schema + "sendusing"] = 2 # cdoSendUsingPort
    mConfig.Fields.Item[schema + "smtpusessl"] = 1 # Use SSL
    mConfig.Fields.Item[schema + "smtpserver"] = "ServerName" # SMTP server
    mConfig.Fields.Item[schema + "smtpserverport"] = 465 # Port number
    
    # If you use Gmail --
    # Enable the Allow less secure apps option for your account
    #mConfig.Fields.Item[schema + "smtpserver"] = "smtp.gmail.com"
    #mConfig.Fields.Item[schema + "smtpserverport"] = 465

    # If you use Outlook --
    #mConfig.Fields.Item[schema + "smtpserver"] = "smtp-mail.outlook.com"
    #mConfig.Fields.Item[schema + "smtpserverport"] = 587

    # If you use Office365 --
    #mConfig.Fields.Item[schema + "smtpserver"] = "smtp.office365.com"
    #mConfig.Fields.Item[schema + "smtpserverport"] = 587
    
    mConfig.Fields.Item[schema + "smtpauthenticate"] = 1 # Authentication mechanism
    # mConfig.Fields.Item[schema + "sendusername"] = "" # User name (if needed)
    # mConfig.Fields.Item[schema + "sendpassword"] = "" # 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

    aqString.ListSeparator = ",";
    for i in range(0, aqString.GetListLength(mAttach)):
      mMessage.AddAttachment(aqString.GetListItem(mAttach, i))
    mMessage.Send();

  except Exception as e:
    Log.Error("E-mail cannot be sent", str(e))
    return False
  Log.Message("Message to <" + mTo + "> was successfully sent")
  return True

def MainTest():
  if SendEmail("[email protected]", "[email protected]", "Subject", \
             "Message body", "c:\\File1.txt,c:\\File2.txt,C:\\File3.txt"):
    # Message was sent
    ...
  else:
    # Message was not sent
    ...

VBScript

Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)
  Dim i, schema, mConfig, mMessage

  Err.Clear
  On Error Resume Next

  schema = "http://schemas.microsoft.com/cdo/configuration/"
  Set mConfig = Sys.OleObject("CDO.Configuration")
  mConfig.Fields.Item(schema + "sendusing") = 2 ' cdoSendUsingPort
  mConfig.Fields.Item(schema + "smtpusessl") = 1 ' Use SSL
  mConfig.Fields.Item(schema + "smtpserver") = "ServerName" ' SMTP server
  mConfig.Fields.Item(schema + "smtpserverport") = 465 ' Port number

  ' If you use Gmail --
  ' Enable the Allow less secure apps option for your account
  ' mConfig.Fields.Item(schema + "smtpserver") = "smtp.gmail.com"
  ' mConfig.Fields.Item(schema + "smtpserverport") = 465

  ' If you use Outlook --
  ' mConfig.Fields.Item(schema + "smtpserver") = "smtp-mail.outlook.com"
  ' mConfig.Fields.Item(schema + "smtpserverport") = 587

  ' If you use Office365 --
  ' mConfig.Fields.Item(schema + "smtpserver") = "smtp.office365.com"
  ' mConfig.Fields.Item(schema + "smtpserverport") = 587

  mConfig.Fields.Item(schema + "smtpauthenticate") = 1 ' Authentication mechanism
  ' mConfig.Fields.Item(schema + "sendusername") = "" ' User name (if needed)
  ' mConfig.Fields.Item(schema + "sendpassword") = "" ' User password (if needed)
  mConfig.Fields.Update

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

  aqString.ListSeparator = ","
  For i = 0 To aqString.GetListLength(mAttachment) - 1
    mMessage.AddAttachment aqString.GetListItem(mAttachment, i)
  Next

  mMessage.Send

  If Err.Number > 0 Then
    Log.Error "Email cannot be sent", Err.Description
    SendEMail = False
  Else
    Log.Message "Message to <" + mTo + "> was successfully sent"
    SendEMail = True
  End If
End Function

Sub MainTest
  If SendEmail("[email protected]", "[email protected]", "Subject", _
             "Message body", "c:\File1.txt,c:\File2.txt,c:\File3.txt") Then
    ' Message was sent
  Else
    ' Message was not sent
  End If
End Sub

DelphiScript

function SendEmail(mFrom, mTo, mSubject, mBody, mAttach);
var i, schema, mConfig, mMessage;
begin
  try
    schema := 'http://schemas.microsoft.com/cdo/configuration/';
    mConfig := Sys.OleObject('CDO.Configuration');
    mConfig.Fields.Item(schema + 'sendusing') := 2; // cdoSendUsingPort
    mConfig.Fields.Item(schema + 'smtpusessl') := 1; // Use SSL
    mConfig.Fields.Item(schema + 'smtpserver') := 'ServerName'; // SMTP server
    mConfig.Fields.Item(schema + 'smtpserverport') := 465; // Port number

    // If you use Gmail --
    // Enable the Allow less secure apps option for your account
    // mConfig.Fields.Item(schema + 'smtpserver') := '"smtp.gmail.com';
    // mConfig.Fields.Item(schema + 'smtpserverport') := 465;

    // If you use Outlook --
    // mConfig.Fields.Item(schema + 'smtpserver') := 'smtp-mail.outlook.com';
    // mConfig.Fields.Item(schema + 'smtpserverport') := 587;

    // If you use Office365 --
    // mConfig.Fields.Item(schema + 'smtpserver') := 'smtp.office365.com';
    // mConfig.Fields.Item(schema + 'smtpserverport') := 587;

    mConfig.Fields.Item(schema + 'smtpauthenticate') := 1; // Authentication mechanism
    // mConfig.Fields.Item(schema + 'sendusername') := ''; // User name (if needed)
    // mConfig.Fields.Item(schema + 'sendpassword') := ''; // 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;
      
    aqString.ListSeparator := ',';
    for i := 0 to aqString.GetListLength(mAttach) - 1 do
      mMessage.AddAttachment(aqString.GetListItem(mAttach, i));

    mMessage.Send;
  
    Log.Message('Message to <' + mTo + '> was successfully sent');
    Result := True;
  except
    Log.Error('Email cannot be sent', ExceptionMessag);
    Result := False;
  end;
end;

procedure MainTest;
begin
  if SendEmail('[email protected]', '[email protected]', 'Subject',
             'Message body', 'c:\File1.txt,c:\File2.txt') then
    // Message was sent
  else
    // Message was not sent
end;

C++Script, C#Script

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

  try
  {
    schema = "http://schemas.microsoft.com/cdo/configuration/";
    mConfig = Sys["OleObject"]("CDO.Configuration");
    mConfig["Fields"]["Item"](schema + "sendusing") = 2; // cdoSendUsingPort
    mConfig["Fields"]["Item"](schema + "smtpusessl") = 1; // Use SSL
    mConfig["Fields"]["Item"](schema + "smtpserver") = "ServerName"; // SMTP server
    mConfig["Fields"]["Item"](schema + "smtpserverport") = 465; // Port number

    // If you use Gmail --
    // Enable the Allow less secure apps option for your account
    // mConfig["Fields"]["Item"](schema + "smtpserver") = "smtp.gmail.com";
    // mConfig["Fields"]["Item"](schema + "smtpserverport") = 465;

    // If you use Outlook --
    // mConfig["Fields"]["Item"](schema + "smtpserver") = "smtp-mail.outlook.com";
    // mConfig["Fields"]["Item"](schema + "smtpserverport") = 587;

    // If you use Office365 --
    // mConfig["Fields"]["Item"](schema + "smtpserver") = "smtp.office365.com";
    // mConfig["Fields"]["Item"](schema + "smtpserverport") = 587;

    mConfig["Fields"]["Item"](schema + "smtpauthenticate") = 1; // Authentication mechanism
    // mConfig["Fields"]["Item"](schema + "sendusername") = ""; // User name (if needed)
    // mConfig["Fields"]["Item"](schema + "sendpassword") = ""; // 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;

    aqString["ListSeparator"]=",";
    for(i = 0; i < aqString["GetListLength"](mAttach); i++)
      mMessage["AddAttachment"](aqString["GetListItem"](mAttach, i));
    mMessage["Send"]();
  }
  catch (exception)
  {
    Log["Error"]("Email cannot be sent", exception.description);
    return false;
  }
  Log["Message"]("Message to <" + mTo + "> was successfully sent");
  return true;
}

function MainTest()
{
  if (SendEmail("[email protected]", "[email protected]", "Subject",
             "Message body", "c:\\File1.txt,c:\\File2.txt,C:\\File3.txt"))
    // Message was sent
  else
    // Message was not sent
}

For a detailed description of the CDO.Configuration object, see documentation on Collaboration Data Objects in the MSDN library.

Via Microsoft Outlook

An example of sending an email message with attachments via Microsoft Outlook is given in the <TestComplete Samples>\Common\MSOffice sample project that interacts with Microsoft Outlook (for the VBScript, JScript, и DelphiScript languages). These samples apply only to desktop Outlook versions.

Notes:

See Also

SendMail Method
Sending Results via E-Mail - About

Highlight search results