Configuring Email Notifications in Jenkins Using Mailer and Email Extension Plugins
This article explains how to configure Jenkins to send build results via email, covering the default Mailer plugin setup, testing, and advanced Email Extension plugin usage with default, Groovy, and HTML templates, including pipeline script examples and best practices for reliable notifications.
Background
After offline deploying Jenkins, the article shows how to send deployment results via email.
Using the Default Mailer Plugin
Check Installation
Verify that the Mailer plugin is installed; if not, download and install it.
Configure Plugin
Set SMTP server, default suffix, sender address, password, and port in the global configuration.
http://
:8082/manage/configureTest Email
Send a test email to confirm the configuration.
Freestyle Job Email Settings
Configure email notifications for freestyle projects (on failure, unstable, etc.).
Pipeline Email Settings
Use a pipeline script to send email with an HTML template.
pipeline {
agent any
stages {
stage('获取最新代码') {
steps {
script { echo "获取最新代码" }
}
}
}
post {
always {
echo '构建结束...'
}
success {
mail subject: "'${env.JOB_NAME} [${env.BUILD_NUMBER}]' 执行成功",
body: """
...""",
charset: 'utf-8', from: '[email protected]', mimeType: 'text/html', to: "[email protected]"
}
failure { … }
}
}Using the Email Extension Plugin
The plugin allows custom Groovy or HTML templates.
Installation and Global Configuration
Install the plugin and set SMTP details.
Default Template
body: '''${SCRIPT,template="groovy-html.template"}'''Full Pipeline Example
pipeline {
agent any
stages { … }
post {
always {
emailext(
subject: "'构建通知: ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - ${currentBuild.currentResult}'",
recipientProviders: [developers(), requestor()],
body: '''${SCRIPT,template="groovy-html.template"}''',
to: '[email protected]',
mimeType: 'text/html'
)
}
}
}Custom Groovy Template
Create a .groovy file under ${JENKINS_HOME}/email-templates/ and reference it with ${SCRIPT,template="mytemplate.groovy"} .
Custom HTML Template
Read an external HTML file in the pipeline.
body: '''${FILE,path="/home/jenkins/email-template/email.html"}'''Include the HTML template content (sample provided) which displays build information, changes, and test results.
Summary
The article demonstrates configuring Jenkins email notifications using both the built‑in Mailer plugin and the more powerful Email Extension plugin, showing how to test, use pipeline scripts, and apply custom Groovy or HTML templates for flexible reporting.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.