Jenkins is an open source tool used to perform Continuous Integration (CI) and Continuous Development (CD) by automating build/test/deployments. Using Jenkins you can easily automate the complete process of build to deploy across different environments (Dev/SIT/UAT/Production) by creating different jobs or pipeline.
When you create these jobs there must be some way to get the team and yourself notified about the build/test/deploy status.This is where Email Notifications can be used. Jenkins provide plugins to Send Emails.You just need to install and configure the plugin correctly.
This tutorial will help you setting up the email notifications in few simple steps 🙂
You can configure email notifications in your jobs in two ways:
1) Default Email Notifier (Email-Notification)
2) Extended Email Notification (Editable E-mail Notification)
In Extended Email Notifications you can set triggers (e.g. build is unstable or before build), specify email subject, content and recipients.
Prerequisites to Install and Configure Email Notification in Jenkins
This tutorial is based on Jenkins version 2.194 installed on
localhost and using http port
Step 1 - Installing Email Extension Plugin.
Open Jenkins using the following URL: http://localhost:8080/ on any browser (in this tutorial port 8071 is used).
Click on Manage Jenkins.
Click on Manage Plugins.
Select Email Extension and Email Extension Template Plugin and click Install Without Restart.
Once the plugin is installed let's configure it with SMTP servers so that Jenkin emails can be routed via these SMTP servers.
Step 2 - Configure Email Notifications
Click on Manage Jenkins and then Configure system
Scroll below till E-mail Notification and click on advanced. Setup up as shown in below screenshot and save it.
You can test configurations by entering recipient email address and clicking on test configuration. If all is good it will show a message - Email sent successfully. You may get error while testing configurations, below is possible errors and solution to it.
In step 4 you will see the steps to configure email content and set triggers using Extended Email Notification.
Step 3 — Configuring Email-Notification in our jobs or pipeline
Go to your Jenkins home page and click on the created job (Here its gradlerun)
2) Click on Manage Plugins
3) Once your job configuration opens, scroll down to add post-build action and select Email Notifications.
4) Enter recipients and check Send Email for every unstable build and save it.
Here you go! Run your job and you start receiving emails.
Step 4 - Configure Extended Email Notification
From step 2, scroll below till Extended E-mail Notification and setup as shown in screenshot. You can set your own triggers also, every option or setting in self explanatory , configure it as per your requirement.
That's it you have successfully configured email settings, Lets set this up in our pipeline or jobs.
Step 5 — Configure Extended Email Notification in jenkins job
From step 3 in add post-build action and select Editable Email Notifications.
Once it is added in build action save it.
Now you will receive custom email for each of your job actions.
Problems that you might face while sending test email from Jenkins
Error 1 - Unable to connect to smtp server
java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) Caused: javax.mail.MessagingException: Could not connect to SMTP host: smtp.pepipost.com, port: 25;
Solution: Try telnetting SMTP server on different port 587, 2525, 25 etc. if that works change SMTP port in your configuration.
Error 2 - SSL connection is blocked
If using port 587, SSL must be turned on.
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. x8sm31507336pfn.106 - gsmtp
Solution: Turn on SSL from configurations.
If your jenkins is running on http port you may get below error.
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at sun.security.ssl.InputRecord.handleUnknownRecord(Unknown Source)
Solution : Either host Jenkins or HTTPS/change port to 465,
If you are using Gmail as SMTP server you may below error:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials r2sm51378882pfq.60 - gsmtp
Solution : If you have entered wrong password or username correct it. And in Gmail allow less secure apps to login or install Jenkins to use HTTPS.
Now you can successfully configure email notifications for your Jenkins build/test/deploy pipelines.