In this blog, let’s learn setting up Email Notification in Jenkins.

In real time, we build jobs periodically for Jobs that we create in Jenkins. As and when job is completed, if we want to be notified to know about job execution status [Success or Failure or Unstable] then it is a good feature to be added into Jenkins Job Configuration.

To achieve Email Notification in Jenkins, now let’s see how can we do it.

We need to install below plugins:

1. Email Extension Plugin.                                                           

2. Email Extension Template Plugin.

Go to Jenkins Dashboard.                                                         

1. Click on Manage Jenkins.                                                     

2. Click on Manage Plugins. 

In Manage Plugins, go to Available section and search by “Email” and install suggested email plugins related to Email Notification that are Email Extension Plugin and Email Extension Template Plugin.

In my case, as Email Extension and Email Extension Template Plugins are already installed, both plugins are appearing in installed section.

Once plugins are installed, then we are good to configure job for Email Notifications in Jenkins.

Let’s now see configuring job for Email Notifications in Jenkins after plugins are installed.

Go to Manage Jenkins.

Click on Configure System.

Scroll down to E-mail Notification in Configure System Section.

 

Follow above simple steps to do Email Notification Configuration.

Here I’m using Gmail for achieving Email Notification in Jenkins.   

1. SMTP server – For gmail, it is mandatory to provide as smtp.gmail.com.                                                                     

2. Use SMTP Authentication should be checked – Only then we should be able to see User Name and Password sections.             

3. Provide your Gmail Credentials in User Name and Password Fields. 

4. Use SSL should be checked.

5. Give SMTP Port Number as 465. [If 465 doesn’t work for you, try with 587]

Once setup is done for Email Notification.

Let’s do Test Configuration by Sending Test Email.

Once Test configuration by sending test e-mail is Checked, you should be able to see Test e-mail recipient option as shown below.

Now enter Gmail ID to which you want to send out a Test Mail and click on Test Configuration. 

If everything configured as expected then a Test Mail to should be triggered into given Gmail ID and followed message should be displayed “Email was successfully sent” as shown below.

Test E-Mail Recipient could be any Gmail ID – Now you can Check for Test Mail in Given Mail Account.

If in case, Test Configuration is Failed.                                        Go to your Gmail Account for which Email Notification was configured.             

Click on Security.                                                                   

Now Go to Less secure app access and Allow Turn on Access to Less secure app access. [By default it should be selected to Off]

Now Go to Manage Jenkins.                                                    

Click on Configure System.                                                  

Scroll down to Extended E-mail Notification in Configure System Section.     

Enter all Required Fields as shown in below Screenshots.

Note:

In Extended E-mail Notification section, you just need to provide SMTP server details as smtp.gmail.com.   

Do not change anything from Default Subject and Default Content Fields that are suggested by Jenkins. 

Now Click on Apply and Save.

Wow, now we are all set to use Email Notification Feature in Jobs.

Now go to Jenkins Job. 

Click on Configure. 

 Go to Build Settings section and provide Email IDs as shown below to which Email Notifications has to be sent.

Note:

Here Email Notifications will be sent only and only for Unstable and Failed Builds.

Surprise:   

 If you want to achieve Email Notification through Jenkins Pipeline. Follow below simple steps.       

 1. Configurations for E-mail Notification and Extended E-mail Notification should be done in Configure System section. 

2. Add below code inside Jenkins Pipeline Step.

stage('Gmail')
{
	steps
	{
		emailext body: "*${currentBuild.currentResult}:* Job Name: 
                ${env.JOB_NAME} || Build Number: ${env.BUILD_NUMBER}\n More 
                information at: ${env.BUILD_URL}",
		subject: 'Declarative Pipeline Build Status',
		to: 'Pavankrishnan1993@gmail.com'
	}
}

Above code will send a Mail with followed information as below:

1. emailext body:     

A. Current Build Status [Success or Failure or Unstable]. 

B. Job Name as specified in Jenkins Job.

C. Build Number that is executed in Jenkins.   

2. Subject: In this section, you can provide Subject name as per your Requirement.       

3. to: In this section, you can provide Mail IDs to which Email Notification should be sent.

Note: 

The above code can be used in different steps for Job Success, Failure or Unstable..!!

Conclusion: In this blog, we have seen how can we configure Email Notification in Jenkins for Jobs with simple steps for Builds and Pipeline.

Cheers!

Naveen AutomationLabs