Trying out Spring’s email support, I wanted to send a simple message to a single recipient using code like:
1 2 3 4
To my astonishment this threw an exception:
1 2 3 4 5 6 7 8 9 10 11 12 13
Taking a closer look, I recognized that the
setTo method of
SimpleMailMessage is overloaded as follows:
So the second method allows for passing a list of recipients.
My intuition told me to check the bytecode the Groovy compiler has generated for setting the
to property, using the wonderful Procyon decompiler:
Not having the book at hand at that time but vaguely remembering a Java Puzzler
null to an overloaded method, I searched the Java Language Specification for how methods are chosen in my case.
I found out and refreshed my memory that Java chooses the most specific method.
As an array parameter is more specific than its single reference counterpart, the
setTo(String) method is called.
With the Groovy logic to transform a String to a List of that String’s characters when that String is passed to a method with a String array parameter, this results figuratively in an invocation like:
As each of those array entries is handled like a recipient, each is passed within a loop to InternetAddress.checkAddress(..).
The looping terminates while its third iteration as the “recipient”
@ is passed to checkAddress(..) - the AddressException is thrown, stating
Missing local name in string ``@''. Finally, this message makes more sense, doesn’t it?
By the way, to solve the problem I just wrote:
All in all, isn’t that a groovy Java Puzzler?