user1587599
user1587599

Reputation: 221

Send email notification from Jenkins

I want to send email notification when any job gets done. Please let me know how can we propagate.

Upvotes: 22

Views: 57589

Answers (7)

Yogesh Chugh
Yogesh Chugh

Reputation: 1

If you wants to configure gmail account in Jenkins:

  1. Launch the Jenkins
  2. Click on Manage Jenkins from Left Pane.
  3. Click on Configure System under the System configuration. Under the Email Notification
  4. Put the SMTP server as smtp.gmail.com in SMTP server field.
  5. Click the Advance button and then select the "Use SMPT Authentication" checkbox to select it.
  6. Enter valid gmail id and password (16 digit generated password) in Username and password field.
  7. Select the "Use SSL" checkbox to select it.
  8. Put the port no. as 465 in SAMP Port field.
  9. Default the Charset as UTF-8.
  10. Select the "Test configuration by sending test e-mail" checkbox to select it
  11. Enter the same email id as entered in step 6 in "Test e-mail recipient" field.
  12. Then click the Test configuration button.
  13. If message displayed as "Email was successfully sent". it means you have successfully configured the email notification and perform the step 14. if the message displayed as "jakarta.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted..." so you have to need to generate 16 digit app password from google account
  14. click on apply and save button

For generate app password:

  1. Login with Gmail account.
  2. Go to Account Setting >> Security. Under the Signing in to Google
  3. Enabled the 2-step verification, if required.
  4. Click on App password
  5. Entered the valid password.
  6. click on select app dropdown and then select Other(Custom name) option
  7. Enter the any name in field and then click Generate button.
  8. copy the 16 digit code and paste the 16 digit code in password field in Jenkins Email configuration.
  9. Then again click on Test configuration button under the "Test configuration by sending test e-mail"

Now you have to successfully configured the Email account in Jenkins

Upvotes: 0

Hameed Syed
Hameed Syed

Reputation: 4275

All the above answers are about plug-in and its usage but not how to use it in pipeline execution. Assuming smtp server is configured in jenkins and https://plugins.jenkins.io/email-ext/ plug-in is installed , we can write or use the plug-in ij the below format.

def csproj_path = 'dotnetcore_sample.csproj'

pipeline {
    agent{
        node {
            label 'dotnet-31' 
        }
    }

    stages {
        stage ('build locally'){
            steps{
                sh "dotnet build ${csproj_path}"
            }
        }
         stage ('Prompt check'){
            steps {
            mail to: '[email protected]',
                 cc : '[email protected]'
                subject: "INPUT: Build ${env.JOB_NAME}", 
                body: "Awaiting for your input ${env.JOB_NAME} build no: ${env.BUILD_NUMBER} .Click below to promote to production\n${env.JENKINS_URL}job/${env.JOB_NAME}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
                timeout(time: 60, unit: 'MINUTES'){
                    input message: "Promote to Production?", ok: "Promote"
                }
            }
        }
        
    }
    
    post {
        failure {
              mail to: '[email protected]',
                 cc : '[email protected]'
                subject: "FAILED: Build ${env.JOB_NAME}", 
                body: "Build failed ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}.\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
    
    success{
            mail to: '[email protected]',
                 cc : '[email protected]'
                subject: "SUCCESSFUL: Build ${env.JOB_NAME}", 
                body: "Build Successful ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
        
        aborted{
            mail to: '[email protected]',
                 cc : '[email protected]'
                subject: "ABORTED: Build ${env.JOB_NAME}", 
                body: "Build was aborted ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}"
        }
    }
}

The above declarative pipeline consists of below features

  1. Send email for the respective user to promote the build to next level.
  2. Email when build is successful.
  3. Email when build is failed and also when it is aborted with link to the logs.

Upvotes: 3

jchaturvedi
jchaturvedi

Reputation: 71

  1. launch Jenkins.
  2. access Manage Jenkins link from the main page.

  3. go to the end of the page and fill Email notification details : enter image description here

  4. Click on Test notification to verify it is sending an email.

  5. Please see the email sent from Jenkins. enter image description here

enter image description here

Upvotes: 0

Nishith Pandey
Nishith Pandey

Reputation: 21

This answer is for sending mail using python script & outlook through Jenkins.

You need to have PsExec.exe for this. This runs the applications remotely.

create a freestyle project & in that run following dos shell command:

path\to\psexec.exe -u username -p password -i 1 cmd -accepteula /c python path\to\SendMail.py

Username & password is for the windows user account where outlook runs. path\to\SendMail.py is the location of the python script. SendMail.py kinda looks like this:

import win32com.client as win32
outlook=win32.Dispatch('outlook.application')
mail=outlook.CreateItem(0)
mail.To='[email protected]'
mail.Subject="Test Mail"
mail.HTMLBody="Hiii, This is just a test mail."
mail.Send()

Here I'm using wind32com for executing outlook. The mail will be sent using the default account logged in outlook.

You can trigger to build this project everytime after a job completes.

Hope this is of any help to anyone :)

Upvotes: 2

kalyani chaudhari
kalyani chaudhari

Reputation: 7849

  1. Go to : manage jenkins --> manage plugins --> 'available' tab --> select 'email extension plugin' --> click on button 'install without restart'

  2. manage jenkins --> configure system --> enter details in 'email notification'

Fill up the details as given below and save it:

enter image description here

  1. In the configuration of project/job , tick on the 'email notification' ---> enter the details ---> save it ---> build the job/project

Upvotes: 8

ppapapetrou
ppapapetrou

Reputation: 1663

You can configure Jenkins to send email when a job finishes either by using a plugin or not.

Built-in

First you have to configure your mail server settings by clicking on Manage Jenkins > Configure System and find the E-mail Notification section near the bottom of the page. Optionally also configure System Admin e-mail address in the Jenkins Location section.

Then for each job got to its configuration and Add post-build action, select E-mail Notification and configure as needed.

Plugin

The issue with default jenkins email mechanism is that it has very limited customization.

The alternate approach is to use the Email-Ext plugin, a powerful email notification mechanism. You can define some global triggers but you can also customize the settings for each job. Sending emails for success, failure or any other build status is supported.

Upvotes: 10

Henk Langeveld
Henk Langeveld

Reputation: 8456

There's a Jenkins email-ext plugin that adds triggers and recipients.

Upvotes: 6

Related Questions