Reputation: 10435
Is there a way to send an email from Jenkins job using Groovy Postbuild action? Similar to how it can be done using Jenkins pipeline plugin
mail to: '[email protected]',
subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
body: "Please go to ${env.BUILD_URL}."
Upvotes: 4
Views: 13021
Reputation: 10435
Thanks to https://stackoverflow.com/a/37194996/4624905 I figured out that I can use JavaMail API directly. It helped me.
import javax.mail.* import javax.mail.internet.* def sendMail(host, sender, receivers, subject, text) { Properties props = System.getProperties() props.put("mail.smtp.host", host) Session session = Session.getDefaultInstance(props, null) MimeMessage message = new MimeMessage(session) message.setFrom(new InternetAddress(sender)) receivers.split(',').each { message.addRecipient(Message.RecipientType.TO, new InternetAddress(it)) } message.setSubject(subject) message.setText(text) println 'Sending mail to ' + receivers + '.' Transport.send(message) println 'Mail sent.' } Usage Example: sendMail('mailhost', messageSender, messageReceivers, messageSubject, messageAllText)
Upvotes: 4
Reputation: 10675
Yes you can.
Setup SMTP configuration for your jenkins by going to jenkins -> configuration
. If you have your own(company) SMTP server that would be great otherwise you can use google's smtp server and your gmail account (you can create new gmail account for the jenkins).
Add post build action (Email editable notification) and fill the form accordingly.
NB: Because we are putting the username and password of our gmail account on jenkins gmail will notify you that your account is being used by an app and it is less secure; In that case you have to tell gmail to allow for less secure access follow the instructions here. If you did not enable less secure apps to access your apps your emails might not be delivered appropriately.
Upvotes: 0