Jenkins: Integrating Email Service
Setting up email notifications of completed builds from your Jenkins CI is an essential but surprisingly complex process.
Join the DZone community and get the full member experience.Join For Free
Jenkins is one open-source tool that can be used to perform Continuous Integration and build automation. Using it, all development work can be integrated as early as possible. The resulting artifacts are automatically created and tested, and as a result, the process of identification of errors becomes faster.
There must be a way that the build status and testing results can be reported to the team. That's when email service comes into the picture.
How can an email service be integrated with Jenkins?
I will be discussing two approaches to configure email services.
Default email notifier. This is what comes default with Jenkins. It contains a default message containing the build number and its status.
Email extension plugin. This plugin allows you to configure every aspect of email notifications. You can customize when an email is sent, who should receive it, and what the email says.
Configuring Default Email Notifier
To provide the configuration, go to the Jenkins System configuration page (Jenkins > Manage Jenkins > Configure System).
You will be able to see the below screen to add email configurations:
Below are details for some of the fields:
SMTP server: Denotes the name of the mail server. Leave it empty to use the default server (which is normally the one running on localhost). Jenkins uses JavaMail for sending out e-mails, and JavaMail allows additional settings to be given as system properties to the container.
SMTP port: Defines the port number for the mail server. Leave it empty to use the default port for the protocol.
Once the mail configurations are set, you can test whether it is working fine or not by checking Test configuration by sending test email.
To allow your projects to send by email, you need to add Post Build Action and select “Email Notification from the drop-down list.
This will provide you the below interface, where you can add a list of email addresses that the email is required to be sent to.
Configuring Email Extension Plugin
This plugin extends Jenkins built-in email notification functionality by giving you more control. It provides customization of three areas.
Triggers: Select the conditions that should cause an email notification to be sent.
Content: Specify the content of each triggered email’s subject and body.
Recipients: Specify who should receive an email when it is triggered.
Before using email-ext on a project, you must configure some global settings. Go to the Jenkins System configuration page (Manage Jenkins > Configure System.).
The section titled Extended E-mail Notification is where you can configure global email-ext properties. The properties here should match the settings for your SMTP mail server. This section is set up to mirror Jenkins' own email Publisher Reporter (a different extension point).
However, there are a few additions.
The inputs labeled Default Subject and Default Content allow you to configure the email content on a global level. The input labeled Default Recipients can be used to set a default list of email addresses for all projects using the plugin (it can be overridden at the project level). This can be used to greatly simplify the configuration you need to do for all projects.
For a project to use the email-ext plugin, you need to enable it in the project configuration page. Select the checkbox Editable Email Notification in the Post Build Actions section and you will see something like this:
There are three fields that you can edit when the plugin is enabled.
Global recipient list: This is a comma- or whitespace-separated list of email recipients that allows you to specify a single recipient list for each email that is sent.
Default subject: This allows you to configure a token (more about tokens later) that can be used to easily configure all email subjects for the project.
Default content: Same as default subject, but for the email body instead of the subject.
To see the advanced configuration for the plugin, click on the Override Global settings checkbox, then click the Advanced button. This section allows you to specify recipients for each type of email trigger as well as a pre-send script that can be used to modify the email just prior to sending.
By default, the only trigger configured is the Failure trigger. To add more triggers, select one from the dropdown and it will be added to the list. Once you have added a trigger, you have several options.
Published at DZone with permission of Sangeeta Gulia, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.