Forum Discussion

ManuT's avatar
ManuT
Occasional Contributor
7 years ago

Issue in Outlook when I am trying to send an email

Hi All,

 

I am getting this error when i am trying to send a text document through email using outlook from TestComplete. It worked for one time and I received email. But, every time I am getting this error that is attached as screenshot. It is not at all working. I am not sure why it worked for only once. Please, help me with this.

 

Thanks,

Manu

4 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    Are you closing Outlook at the end of your test?  If you're opening it every time and not closing it, that would explain why it can't open after the first time.

    • ManuT's avatar
      ManuT
      Occasional Contributor

      Thank you so much Marsha for the response.

      I mean it just worked onetime randomly in the middle. I am not sure about the reason. Well, i would try as you said. Could you  tell me what i need to add in the below code which is in javascript.

       

      // MS Outlook constants
      var
      olMailItem = 0;
      olByValue = 1;
      olContactItem = 2;

      var MsOutlook, NS, ContactFldr;

      function Outlook_Test()
      {
      if (!InitializeOutlook())
      Log.Warning("Unable to initialize MS Outlook or its folders.", "", pmHigher);
      else
      {
      SendReport("source email", "destination email", "company",
      "D:\\Data\\TestDocument.txt");
      }
      }

      // Initializes MS Outlook
      function InitializeOutlook()
      {
      try
      {
      try
      {
      MsOutlook = Sys.OleObject("Outlook.Application");
      }
      catch(exception)
      {
      MsOutlook = Sys.OleObject("Outlook.Application.16");
      }
      // NS = MsOutlook.GetNamespace("MAPI");
      // ContactFldr=NS.AddressLists;
      //ContactFldr = NS.Folders("Personal Folders").Folders("Contacts");
      }
      catch(exception)
      {
      Log.Warning(exception+"Unable to initialize MS Outlook.", "", pmHigher);
      return false;
      }
      return true;



      }

       

      // Sends a report to the user
      function SendReport(Name1, Name2, Company, FileNameToAttach)
      {
      var ObjItem, MI, i, CRLF;

      CRLF = "\n";

      // Creates a new message
      MI = MsOutlook.CreateItem(olMailItem);


      // Sets the message subject
      MI.Subject = "Test Results";

       

      MI.Body= "Sample test";

      // Specifies the destination address
      MI.To = "Destination address";

      // Attaches the script code
      MI.Attachments.Add(FileNameToAttach, olByValue, MI.Body.length, "Script");

      MI.Display(); // This line displays the new mail on screen.
      // To send the mail, use the Send method
      MI.Send();


      }

       

      • NisHera's avatar
        NisHera
        Valued Contributor

        To me code seems to be OK

         

        Just make sure in your registary (regedit .exe)  \HKEY_CLASSES_ROOT\Outlook.Application\CurVer is 1.6

         

        since it says server execution failed it seems that your out look can not get connected to mail server

         

        One thing you have to aware that if outlook already open it can not open another instance.

        try before initializing outlook something like...

         if  (Sys.Process("OUTLOOK").Exists){  

               Sys.Process("OUTLOOK").Terminate}

         

        Also try opening TC as administrator there could be privileged issue  

         

        Other thing may be your antivirus or system admin rule restricts you.