Sahi Pro - Sending Emails

We recommend using Ant's email tasks to send out emails at the end of a run. That said, there can be genuine cases where you need to send an email at the end of a script.
There is a small program to send emails which is bundled with Sahi. It is just a thin wrapper over JavaMail. To call it, mail.jar, activation.jar and ant-sahi.jar need to be added to Sahi's classpath.
  1. Get mail.jar: Download javamail1_4_5.zip from http://www.oracle.com/technetwork/java/index-138643.html.oracle.com/technetwork/java/index-138643.html, unzip it and extract mail.jar
  2. Get activation.jar: If you are using Java 1.6 or greater, you do not need activation.jar. If you are using Java version less than 1.6, download jaf-1_1_1.zip from http://www.oracle.com/technetwork/java/jaf11-139815.html.oracle.com/technetwork/java/jaf11-139815.html, unzip it and extract activation.jar
  3. Copy mail.jar and activation.jar into sahi/userdata/extlib/mail folder.
  4. Add these jars to Sahi's classpath. You need to edit sahi/userdata/bin/start_dashboard.bat or sahi/userdata/bin/start_sahi.bat (or the ant or .sh files, depending on which you use to start Sahi. More details here: "Adding jars to Sahi's classpath":adding-jars-to-sahis-classpath)
  5. SET SAHI_EXT_CLASS_PATH=
    %SAHI_USERDATA_DIR%\extlib\mail\mail.jar;%SAHI_USERDATA_DIR%\extlib\mail\activation.jar;%SAHI_HOME%\lib\ant-sahi.jar;
  6. Copy the below Sahi function for sending emails:
  7. function sendEmail($emailSubject, $emailBody) {
      var $host = "smtp.gmail.com";
      var $port = 465;
      var $username = "from@gmail.com";
      var $password = "password";
      var $isSSL = true; // set to true if you use SSL
      var $mailer = new Packages.net.sf.sahi.ant.Mailer($host, $port, $username, $password, $isSSL);
      var $from = "from@gmail.com";
      var $to = "to@example.com";
      $mailer.send($from, $to, $emailSubject, $emailBody);
    }
  8. Call it directly as
  9. sendEmail("Mail from Sahi", "All izz well");
  10. To trigger on end of script, add a onScriptEnd function to your script:
  11. function onScriptEnd(){
      var $status = _scriptStatus(); // "FAILURE" or "SUCCESS"
      var $scriptName = _scriptName();
      var $scriptPath = _scriptPath(); // Script name with full file path
      sendEmail($status + ": " + $scriptName, "Script: " + $scriptPath + "\nStatus: " + $status);
    }
    More details on "Sahi call back functions":sahi-call-back-functions