Using Groovy to Send Emails

New programming languages have recently become an increasingly important discussion point. Emerging dynamic and scripting languages are being used in both client and server frameworks to achieve greater flexibility, agility, and productivity. At the same time, Domain Specific Languages (DSLs) are being used to provide greater expressivity with reduced scaffolding code and language syntax noise.

In the midst of these evolving trends sit several relatively recent dynamic languages, including Groovy. Groovy is one of the more popular dynamic languages designed to run on the JVM. It has a syntax that is very familiar to Java programmers, supports all the latest features and paradigms present in the dynamic language world, reduces large amounts of scaffolding code, and integrates seamlessly with Java. In essence, it allows you to gain from the emerging trends in language evolution while still leveraging the skills, infrastructure, and libraries you already have from the Java world.

As tempting as it might be to just rave about Groovy's great design or do a feature-by-feature comparison with other languages, in this tutorial, I try to dispense with hype (the last paragraph is more than enough!). I instead take a well-known topic (sending email) and show several ways you can do it with Groovy. Along the way we'll be looking at some of Groovy's features, and gain some appreciation of how dynamic languages can make otherwise mundane programming tasks both more productive and more fun.

A Testing Prologue

Test-driven development advocates writing a failing test before writing the production code in order to make the test pass. Let's give that a try for our email example. First, we'll write a little helper class to assist with our testing.

We have wrapped the Wiser email testing library inside a fixture class. Wiser acts as an SMTP server on your system and makes the messages it receives directly available to your Groovy (or Java) program. This saves you from having to use a real messaging service, keeping our test insulated from unreliable and slow external systems. Wiser's details are all hidden away inside our fixture, so you could easily swap it for an alternative like Dumbster or some other library with similar functionality.

Some points of interest:

You should note that the syntax is very similar to Java. Perhaps the first obvious difference is that semicolons are missing. They are optional unless you have multiple statements on one line.

We also don't need to statically type variables and methods. Variables wiser and message, method assertEmailArrived, and the method argument args don't specify a type. We can use a static type if we want, as shown with the constructor argument port.

The assert statement is turned on by default in Groovy, whereas it is off by default in Java.

Groovy has some neat shorthands, too. The expression message?.subject is equivalent to the Java expression message.getSubject() but also guards against null pointer exceptions. The expression args['subject'] is equivalent to args.get('subject'). Minor improvements here but they all add up.

Our test is making sure that the email arrives with the correct from address and subject, but we could extend this to other parts of the email content if we wished. Note that we are using named parameters with the assertEmailArrived method. This allows us to reorder our parameters or add new parameters without breaking existing code. Under the covers, a java.util.Map is being passed into our fixture from our calling code, but that is transparent to the Groovy developer.

I should point out that the details being sent in our email examples aren't important. We are mimicking the kind of functionality that is already built into many build systems to notify interested parties of successful (and potentially failing) builds. The same kind of functionality is needed in many ecommerce scenarios.

Streamlining Apache Commons Net

Apache Jakarta Commons Net implements the client side of over a dozen basic Internet protocols including the email protocols SMTP and POP3. The library concentrates on providing a uniform way of interacting with a wide range of protocols at a fairly low level rather than on implementing powerful higher-level abstractions. We will just be using its SMTP facilities here. It would be a good choice if you only have simple needs or if you need to interact with some of the other protocols it supports.

Groovy helped a little here, but the code is still a bit tedious. The Commons Net SMTP API does provide some more-powerful abstractions, which would have reduced the amount of code we needed to write, but none of these exactly matched our requirements. We had to operate at the API's lowest level.

Some points of interest:

We can use the << operator when working with streams or readers.

Double-quoted strings in Groovy are known as GStrings. Variable expansion (in our code, the org variable) automatically occurs within GStrings.

We are using a Java library here, but because Groovy has the same object model as Java, we don't need to worry about any API mismatch as we would with some other popular scripting/dynamic languages that integrate with Java.