Noel Yap
Noel Yap

Reputation: 19768

How to set programmatically recipients of Jenkins Email-ext plugin?

I'm trying to set the recipients of the Email-ext (aka Editable Email Notficiation) to the owners of failed tests. Since the owners can't be calculated until after a build fails, the Inject Environment Variables plugin can't be used.

How can this be done?

Upvotes: 2

Views: 4696

Answers (3)

Noam Manos
Noam Manos

Reputation: 16971

In recent Jenkins versions (e.g. 2.3), importing "javax.mail.Message" in email-ext script, might fail on:

groovy.lang.MissingMethodException: No signature of method: jakarta.mail.internet.MimeMessage.addRecipients() is applicable for argument types: (javax.mail.Message$RecipientType, java.lang.String)

In such case, import "jakarta.mail.Message" instead, and then you can set emails with a simple String:

import jakarta.mail.Message
msg.setRecipients(Message.RecipientType.TO, "[email protected],[email protected]")

Upvotes: 0

DJ83
DJ83

Reputation: 87

If you need to read list of recipients from file on remote agent, expand above answer with FilePath:

import javax.mail.Message
import javax.mail.internet.InternetAddress

fp = new FilePath(build.workspace, build.workspace.toString() + "/recipients.txt")
emails = fp.readToString().split("\n")

for (email in emails) {
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email))
}

Upvotes: 0

Noel Yap
Noel Yap

Reputation: 19768

In the Advanced... section create the following Pre-send Script:

import javax.mail.Message
import javax.mail.internet.InternetAddress

msg.addRecipient(Message.RecipientType.TO, new InternetAddress('[email protected]'))

You'll also need to set Project Recipient List (maybe to some dummy value) since if it's empty, the plugin decides there's nothing to do.

The script runs on the master so you'll need to ssh onto the slave from the master if you need to process its workspace.

Upvotes: 2

Related Questions