I was asked by a customer if Urbancode Deploy could send a generic email as part of the deploy process. Urbancode Deploy has notification capabilities but it relies on the notification schemes that are built in. You can’t simply add a step in a process to send an email.
So I set out to write a plugin to accomplish this. Whenever I tackle a new “programming” exercise, I always resort to two principles. 1) Google to find what others have already done. And 2) reuse anything you already have at your disposal.
Urbancode Deploy already takes advantage of an SMTP server to be able to send emails, and you specify the SMTP settings on the System Settings page.
So the first problem to solve is how to get at these values. Here is where reuse comes in. There is an existing Urbancode plugin that accomplishes this. The Udeploy-System plugin has a task that gets all of the System Configuration information, which includes the SMTP server values. So the first step is to tear apart this plugin and learn how it gets the information. It turns out there is a Rest Client jar file that implements a call to get the Urbancode Deploy system configuration. So I copied most of the Udeploy-System plugin contents into my plugin so I could reuse it.
The second problem then is how to send an email. A quick Google search for “Groovy script to send email” produced numerous results that I could easily
steal re-use. And to actually carry out the sending of the email, I had to go and grab JavaMail mail.jar and its corresponding activation.jar and put them in the lib directory of the plugin.
The resulting plugin works great. There are three parameters that the step takes, the recipient list, the email subject, and the email body.
Here is the resulting Groovy script that I came up with. Note that this version does not deal with authentication as most SMTP servers don’t require it, but I am sure it is an easy addition.
The above link is the code content in a Word doc.
Give it a try.