Send e-mails from groovy scripts

13 views
Skip to first unread message

Oparlescu Vlad

unread,
Jun 20, 2017, 10:14:11 AM6/20/17
to Jenkins Users




Dear Jenkins users,Is there a way to send an e-mail from Jenkins Master using a Groovy script ?
I have an error which I don't know how to solve it:


FATAL: javax.mail.MessagingException: Unknown SMTP host: my.mail.host;
  nested exception is:
	java.net.UnknownHostException: my.mail.host
java.net.UnknownHostException: my.mail.host

The groovy script:

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.'
}


Thank you,
Vlad



Reply all
Reply to author
Forward
0 new messages