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 extends functionality of e-mail notifications. It’s basically informing user about some event or status or any information’s that need to be update to their concern users.
3 parts of Jenkins Email Notification
Given below are the 3 parts of jenkins email notification:
- Triggers: This is an defined condition which cause an e-mail notification to be sent.
- Content: Basically 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 want to configure a process and as part of process to send automated email to the Developers team if any build failure with the help of freestyle configurations. In order to send and receive emails we need to configure email servers in Jenkins. Basically in an organization they have their own email servers that is used to trigger. Emails, in our example we do not have our own server, so we are going to use Gmail SMTP server.
Configure Gmail SMTP Server in Jenkins
After successful installation of Jenkins, it comes with several built in functionalities and several plugins like mail sending. Just we need to configure any plugins or email sending plugin with defined necessary values as in case of email what SMTP server expected.
When the Jenkins email notification triggers, that is an event under which the even occurred and sent to the respective recipients. For Automated build job, there are many events such as success, failure, abort and so on. After clicking on triggers option is basically a selection of required event for which mail need 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 advanced button and below we have fields to be given to configure the SMTP server settings. Please find the below details to configure 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 email if build fails.
Open your job → Configure → Post Build Actions → Add post-build action → email notification
Under Recipients put team’s mail id and save the configuration.
After the successful configuration click on the build now option in order to trigger the build. Based on the build event occurred like success or failed, server send mail to notify.
We can get the build failure notification as failure event occurred at Jenkins server.
Find the use case to send email notification automatically with Pipeline script.
Let’s suppose in any organizations, it follows the devops practice and configured Jenkins as part of their build process. Basically devops team want to configure a process and as part of process to send automated email to the Developers team if build is success.
Configure Jenkins pipeline script job to trigger email if build success.
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: 'smahesh2305@gmail.com'
}
}
We have configured the below git repository in above pipeline script. In order to execute pipeline script we created Jenkins file and keep our script in it.
Please find the below screen shot for the configurations. We have to define all details under pipeline section. Basically under the repositories section, it was defining the Git repositories path where my Jenkins file was located. Also, we have to mark the branch which was having the updated code and ready for the build. At last needs to be defined the script path which is nothing but the Jenkins file.
We have defined our email content into the pipeline script, we have received these email content whenever there is any event occurred. As we can see that below email was received as soon as 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 ask for permission to allow the 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://www.google.com/accounts/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 –
4 Online Courses | 2 Hands-on Projects | 16+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses