title: Jenkins Pipeline Groovy Script: Use `mail` in Jenkinsfile
body: <p>I'm trying to send an email when a <strong>Pipeline</strong> build on Jenkins fails. An example can be found here: <a href="https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify/Jenkinsfile" rel="nofollow noreferrer">https://github.com/jenkinsci/pipeline-examples/blob/master/jenkinsfile-examples/nodejs-build-test-deploy-docker-notify/Jenkinsfile</a></p>
  
  <p>My concrete <strong>groovy</strong> script looks as follows:</p>
  
  <pre><code>#!groovy
  node('') {
     def env = ["JAVA_HOME=${tool 'jdk1.8.0_131'}", "PATH+MAVEN=${tool 'maven_3.1.1'}/bin:${env.JAVA_HOME}/bin", "PATH+GRADLE=${tool 'gradle_4.1'}/bin:${env.JAVA_HOME}/bin" ]
  
     def err = null
     currentBuild.result = "SUCCESS"
  
    try {
     stage('errorStage') {
          dir('error') {
              git url: "unknown", branch: "master"
              withEnv(env) {
                  sh "mvn -Pjenkins-build clean deploy"
              }
          }
      }
    } catch (caughtError) {
        println "caught error :" + caughtError
        err = caughtError
        currentBuild.result = "FAILURE"
        mail (body:
              "Pipeline error: ${err}\nFix me.",
              from: 'jenkins@x.com',
              subject: 'Pipeline build failed',
              to: 'recipient@x.com')
    } finally {
        /* Must re-throw exception to propagate error */
        if (err) {
            throw err
        }
    }
  
  }
  </code></pre>
  
  <p>Actually, nothing ever happens, although the exception is being caught correctly and the build fails. Is there anything required to be able to use <code>mail</code>?! Maybe another Jenkins plugin or something?</p>