Sahi Pro - Sending Emails

abstract Sahi can send emails at the end of a suite, or from anywhere inside a script.
To email playback reports at the end of a suite run, refer to Playback on Desktop.
This section explains how to send out emails from within 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/ folder. Create the extlib folder if it does not exist already. (This will automatically add these jars to Sahi's classpath)
  4. Copy the below Sahi function for sending emails:
  5. 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);
    }
    Call following function to use properties file for sending email.
    function sendEmailWithProps($emailSubject, $emailBody) {
      var $props = loadProperties(_resolvePath("email.properties"), false);
      var $mailer = new Packages.net.sf.sahi.ant.Mailer($props);
      var $subjectPrefix = $props.getProperty("mail.subject.prefix");
      if($subjectPrefix !== null){
    	$emailSubject = $subjectPrefix + $emailSubject;
      }
      $mailer.addBody($emailBody);
      $mailer.addSubject($emailSubject);
      $mailer.addAttachment("Attachment.zip", "d:/Attachment.zip");
      $mailer.send();
    }
    
    function loadProperties($path, $isXML) {
      var $props = new java.util.Properties();
      try {
        var $inStream = new java.io.FileInputStream($path);
        if ($isXML) {
          $props.loadFromXML($inStream);
        } else {
          $props.load($inStream);
        }
        $inStream.close();
      } catch (e) {
                    // do nothing
      }
      return $props;
    }
    Refer to Working with Java object directly in Sahi Script to see an example of reading property files via Java and Sahi Script
    Refer to Email.properties file explained for detailed explanation about email.properties.
  6. Call it directly as
  7. sendEmail("Mail from Sahi", "All izz well");
    or
    sendEmailWithProps("Mail from Sahi", "All izz well");
  8. To trigger on end of script, add a onScriptEnd function to your script:
  9. 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);
    }
    Refer to Sahi call back functions for more details on onScriptEnd