Updated April 27, 2023
Introduction to Jenkins Email Notification
Jenkins email notifications is the way to notify based on event occurred or some action happened. Jenkins email notifications is kind of message that is automatically sent to you and update that, there has been activity on one of your social media accounts like Google, slack. As email is the primary means of notification among other social media for Jenkins email notifications. Jenkins provided a plugin to extend the functionality of e-mail notifications. It’s basically informing the user about some event or status or any information that needs to be updated to their concerned users.
3 Parts of Jenkins Email Notification
Given below are the 3 parts of Jenkins email notification:
- Triggers: This is a defined condition which causes an e-mail notification to be sent.
- Content: Defined content for email subject and body.
- Recipients: We can mention the concerned user who is supposed to receive an e-mail when any event occurred.
Use Case of Jenkins Email Notification
In any organizations, it follows the devops practice and configured Jenkins as part of their build process. Basically devops team wants to configure a process and, as part of the process send automated email to the Developers team if any build fails with the help of freestyle configurations. In order to send and receive emails we need to configure email servers in Jenkins. Basically an organization, they have its own email servers that is used to trigger. Emails, in our example we do not have our server, so we are going to use Gmail SMTP server.
Configure Gmail SMTP Server in Jenkins
After the successful installation of Jenkins, it comes with several built-in functionalities and plugins like mail sending. Just we need to configure any plugins or email-sending plugins with defined necessary values, as in the case of the email that the SMTP server expected.
When the Jenkins email notification triggers, that is an event under which the event occurred and sent to the respective recipients. For an Automated build job, there are many events such as success, failure, abort, and so on. After clicking on the triggers option is basically a selection of the required event for which mail needs to trigger. Once all the changes are made, save the configurations.
Go to Jenkins dashboard.
Go to the Jenkins home page and click the ‘Manage Jenkins’ menu option. Then, select the ‘Configure System’ option.
Go to the E-mail notification section and enter the required SMTP server and user email-suffix details. Click the ‘Advanced’ button and then click the checkbox next to the ‘Use SMTP Authentication’ option. Now, set the following fields.
We have to provide SMTP server details in respective textbox, click on the advanced button, and below we have fields to be given to configure the SMTP server settings. Please find the below details to configure the email server.
Under E-mail Notification, put the mentioned details below:
- SMTP server → smtp.gmail.com
- Select Use SMTP Authentication
- Put your Gmail id
- Put your Gmail password
- Use SSL Select
- SMTP port 465
- Save the configuration
To verify the email notification functionality by clicking the checkbox below to the ‘Test configuration by sending Test e-mail recipient’ option.
Enter a valid email id and click the ‘Test configuration’ button to check whether the email id is valid or not.
Checking on the box that is used for SMTP Authentications, to provide the user credentials for the SMTP account.
Configure Jenkins job to trigger an email if the build fails.
Open your job → Configure → Post Build Actions → Add post-build action → email notification
Under Recipients, put the team’s mail id and save the configuration.
After the successful configuration click on the build now an option in order to trigger the build. Based on the build event that occurred like success or failed, server send mail to notify.
We can get the build failure notification as a failure event occurred at the Jenkins server.
Find the use case to send email notifications automatically with the Pipeline script.
Let’s suppose any organization, followed the devops practice and configured Jenkins as part of their build process. Basically, the devops team wants to configure a process and as part of the process, send automated emails to the Developer’s team if the build is success.
Configure Jenkins pipeline script job to trigger an email if the build is successful.
Please find the below pipeline script:
Code:
node{
stage('SCM Checkout'){
git 'https://github.com/Smaheshwar85/easeWithbase'
}
stage('Compile-Package'){
defmvnHome = tool name: 'Mavan3', type: 'maven'
sh "${mvnHome}/bin/mvn package"
}
stage('Email Notification'){
mail bcc: '', body: '''Build successful!!!!
Thanks,
Mahesh''', cc: '', from: '', replyTo: '', subject: 'Build successfull', to: '[email protected]'
}
}
We have configured the below git repository in the above pipeline script. In order to execute the pipeline script we created a Jenkins file and keep our script in it.
Please find the below screenshot for the configurations. We have to define all details under the pipeline section. Basically, under the repositories section, it defined the Git repositories path where my Jenkins file was located. Also, we have to mark the branch which was having the updated code and is ready for the build. Last needs to be a defined script path which is nothing but the Jenkins file.
We have defined our email content into the pipeline script, and we have received this email content whenever there is any event occurred. As we can see the below email was received as soon as the build event was successful.
Troubleshooting Tip
If your build is getting failed and you are able to see the below error stack trace in console.
534 5.7.14 https://support.google.com/mail/answer/78754 p10sm11420422qkm.121 – gsmtp
- at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
- at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
- at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
- at javax.mail.Service.connect(Service.java:317)
- at javax.mail.Service.connect(Service.java:176)
- at javax.mail.Service.connect(Service.java:125)
- at javax.mail.Transport.send0(Transport.java:194)
- at javax.mail.Transport.send(Transport.java:124)
- at hudson.tasks.MailSender.run(MailSender.java:130)
- at hudson.tasks.Mailer.perform(Mailer.java:176)
- at hudson.tasks.Mailer.perform(Mailer.java:139)
- at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
- at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
- at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
- at hudson.model.Build$BuildExecution.post2(Build.java:186)
- at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
- at hudson.model.Run.execute(Run.java:1881)
- at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
- at hudson.model.ResourceController.execute(ResourceController.java:97)
- at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
In order to fix the above-mentioned error, kindly follow the below solutions:
This error usually asks for permission to allow insecure access from the apps.
Step 1: Login to the Gmail account you are sending mail from.
Step 2: Go to manage your Google Account -> Security -> Less secure app access -> Turn on access.
Or
Access the URL:
https://www.google.com/settings/security/lesssecureapps
Turn “Allow less secure apps: OFF” to “Allow less secure apps: ON”.
If the tips above didn’t help, visit https://accounts.google.com/DisplayUnlockCaptcha and follow the steps on the page.
Recommended Articles
This is a guide to Jenkins Email Notification. Here we discuss the 3 parts of Jenkins email notification, use case and configure Gmail SMTP server in Jenkins. You may also have a look at the following articles to learn more –