Last active
September 16, 2020 15:44
-
-
Save ansig/fdb7fbfd66419a068077273a61f2d3cc to your computer and use it in GitHub Desktop.
Send a mail from Jenkins in Groovy Script Console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.mail.Session | |
import javax.mail.Message | |
import javax.mail.Transport | |
import javax.mail.internet.MimeMessage | |
import javax.mail.internet.InternetAddress | |
def descriptor = Jenkins.instance.getDescriptor("hudson.tasks.Mailer") | |
Session session = descriptor.createSession(); | |
MimeMessage msg = new MimeMessage(session); | |
InternetAddress fromAddress = new InternetAddress(descriptor.getAdminAddress()); | |
msg.setFrom(fromAddress) | |
msg.setRecipients(MimeMessage.RecipientType.TO, (InternetAddress[]) [new InternetAddress('[email protected]')].toArray()); | |
String charset = descriptor.getCharset(); | |
msg.setSubject("Test", charset); | |
msg.setText("Hello from Jenkins!", charset) | |
Transport transporter = session.getTransport("smtp"); | |
transporter.connect(); | |
transporter.send(msg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When i am using this script direct from execute system groovy command or from script console it works!
But when i put it to Config File Management and use this groovy file on job, it fails.