1. Introduction

In this case, I will use Email Extension plugins. This plugin will help you send email notifications via SMTP to you.

2. Prerequisites

3. Email Extension plugins Installations in Jenkins

Go to Manage Jenkins > Plugins.
Search for the “Email Extension” plugin in the “Installed Plugins” section. If you followed the suggested plugins during the Jenkins server setup, it’s likely already installed.

4. SMTP Server Configurations

In this step, we need an SMTP server. If we don’t have our own or our organization’s SMTP server, we can use the following SMTP services:

Gmail SMTP — If you don’t have a work email, AWS, or SendGrid account, you can use Gmail SMTP.
Learn more -> https://support.google.com/a/answer/176600?hl=en

I will demonstrate the SMTP configuration using Amazon SES and SMTP2GO.

4.1 SMTP Server Configuration in Amazon SES

Prerequisite: AWS account

We require SMTP information, including:
SMTP server address
SMTP port
SMTP username
SMTP password

Go to the SES Console.

Go to SMTP Settings > Create SMTP Credentials.

It will create the IAM user.

Please note the SMTP Credentials.

We can check in IAM Consoles.

(Optional) If you forget to note the SMTP credential, you can create a new access key to use as an SMTP credential.

Go to the Security credentials tab > Scroll down to Access keys > Create access key.

Now, we have the following SMTP details:
SMTP server address: SMTP endpoint
SMTP ports: STARTTLS Port (587)
SMTP username: Access key of the Created IAM user
SMTP password: Secret access key of the Created IAM user

Port 587 is the submission port for email clients (MUAs) to submit outgoing emails to a mail server (MTA).
Port 587 typically requires authentication (username and password) before allowing email submission.

We need to verify our email or organization domain in Identities.

Go to Identities > Create Identity.

In this case, I will add my email address.

You will see Email Verification pending state.
You need to verify from your email inbox.

Check your inbox or update email folder.

Click on the verification link.

Refresh and check again your email status.

You can send a test email.

Check your inbox.

Now we have successfully configured the SMTP using Amazon SES.

4.2 SMTP Server Configuration in SMTP2GO.

Prerequisite:
- SMTP2GO account
- Work email

We require SMTP information, including:
SMTP server address
SMTP port
SMTP username
SMTP password

I will show you some settings for SMTP2GO.

SMTP information
SMTP server address: SMTP Server: mail.smtp2go.com
SMTP ports: SMTP Ports/Alternative ports
SMTP username: waiyan (you can create the new user)
SMTP password: smtpUserPassword>

Verified Senders Settings (similar to Identities in Amazon SES).

Can send a test email with SMTP testing tools.

5. SMTP Configuration in Jenkins

You can use Amazon SES SMTP credentials, your own, or your organization’s SMTP credentials.

In this scenario, I will use SMTP2GO SMTP credentials.
SMTP server address: SMTP Server: mail.smtp2go.com
SMTP ports: Alternative ports: (587)
SMTP username: waiyan
SMTP password: smtppassword>

Port 587 is the submission port for email clients (MUAs) to submit outgoing emails to a mail server (MTA).
Port 587 typically requires authentication (username and password) before allowing email submission.

5.1 Create Jenkins credentials with the SMTP username and password.

Before we begin the configurations, we need to create Jenkins credentials with the SMTP username and password.

Go to the Manage Jenkins > Credentials > Global > Add credentials

Please add the SMTP username and password.
In this case, I will use the SMTP2GO SMTP Credentials.

5.2 Configure SMTP Server Address.

Go to Manage Jenkins > System.

Don’t forget to replace the System Admin e-mail address with your own (from email address).

Scroll down to locate the “Extended Email Notification” section, or you can use the browser’s search function by pressing Ctrl+F and typing “Extended Email Notification.”

Please add your SMTP Server Address, SMTP Port and Credentials.
In this case, I will use SMTP2GO SMTP server address — mail.smtp2go.com
Save and Apply.

Port 587 is the submission port for email clients (MUAs) to submit outgoing emails to a mail server (MTA).
Port 587 typically requires authentication (username and password) before allowing email submission.

Now, we have completed the SMTP configurations in Jenkins.

6. Send email notifications in Jenkins Pipeline.

Prerequisite:
Setup the Jenkins Pipeline: If you haven’t set up Jenkins yet, you can refer to my related post on setting up the Jenkins pipeline for detailed instructions.
- [Jenkins] — Deploying a React sample application on Docker using Jenkins and Docker Compose.
- [Jenkins] — Streamlining Jenkins Pipelines: Integrating GitHub or Bitbucket Webhook for Seamless Automation.

Now, we need to modify the Jenkinsfile. We can use these blocks for email notifications:

 post success emailext body: "Build $ succeeded", 
subject: "$ - Build #$ - Successful",
to: 'your_email@example.com',
attachLog: true
>
failure emailext body: "Build $ failed",
subject: "$ - Build #$ - Failed",
to: 'your_email@example.com',
attachLog: true
>
unstable emailext body: "Build $ is unstable",
subject: "$ - Build #$ - Unstable",
to: 'your_email@example.com',
attachLog: true
>
always emailext body: "Build $ has finished with status $",
subject: "$ - Build #$ - $",
to: 'your_email@example.com',
attachLog: true
>
>

In this case, this is my Jenkinsfile.

pipeline agent label 'agent-via-ssh' 
>
environment compose_service_name = "react-jenkins-docker"
workspace = "/home/devops/project/react-jenkins-docker/"
>
stages stage('Checkout Source') steps ws("$") checkout scm
>
>
>
stage('Docker Comopse Build') steps ws("$") sh "docker compose build --no-cache $"
>
>
>
stage('Docker Comopse Up') steps ws("$") sh "docker compose up --no-deps -d $"
>
>
>
>
post success emailext body: "Build $ succeeded",
subject: "$ - Build #$ - Successful",
to: 'your_email@example.com',
attachLog: true
>
failure emailext body: "Build $ failed",
subject: "$ - Build #$ - Failed",
to: 'your_email@example.com',
attachLog: true
>
>
>

Check the pipeline update or run the pipeline manually.
You can check in Jenkins pipeline or Blue Ocean.

Please check your email inbox or spam folder for the successful email notification.

I just made a mistake by adding the wrong image name in the Dockerfile.

Check the pipeline update or run the pipeline manually.
You can check in Jenkins pipeline or Blue Ocean.

Please check your email inbox or spam folder for the Failed email notification.

6.1 Additional Settings

You can use the email body content type with HTML.

Don’t forget to set HTML in the Default Content Type.
(Manage Jenkins > System)

This is an example Jenkinsfile and screenshots of emails.

 post success emailext body: """ 

Build Success Notification


The build $ #$ has succeeded.


Click job/$/$/">here to view the build.



""",
subject: "$ - Build #$ - Successful",
to: 'your_email@example.com'
>
failure emailext body: """

Build Failure Notification


The build $ #$ has failed.


Click job/$/$/">here to view the build.



""",
subject: "$ - Build #$ - Failed",
to: 'your_email@example.com'
>
>

Now, email notifications can be successfully sent in Jenkins via SMTP.

In summary, we have effectively configured the SMTP server and set up the necessary credentials within Jenkins. This ensures seamless communication and enhances the functionality of Jenkins by enabling email notifications. With these configurations in place, Jenkins can now efficiently utilize SMTP for sending notifications, enhancing the overall workflow and communication within the system.

Thank you for taking the time to read this article! Feel free to connect and share your thoughts or questions. I will also share more on the topic of Jenkins.