Multiple email addresses as override addresses in Grails

Posted By : Manish Kumar Narang | 30-Sep-2017

In one of my previous blogs, I talked about the mailing functionality in Grails framework. One may read it here   . In that blog i also mentioned how the parameters namely grails.mail.overrideAddress, grails.mail.default.from, can only take one email address. But there may be scenarios where we would want multiple addresses to be contained in these parameters. Take, for example, if the project is tested by multiple qualitative analysts then we should be able to have multiple addresses to override the email addresses in the testing environment. Another scenario which we encountered was when we want to test email functionality for multiple addresses. Since the email addresses are overridden by only one email address, the functionality seems to work even if there-there are multiple email addresses in to, from, cc and bcc. But that may not be so.

So, now let me discuss how we can add multiple addresses in the above-mentioned parameters. For that, we may need to look into the inner workings of the mail plugin and specifically at the message builder factory. So, let's look at the MailMessageBuilder.groovy file. There are two functions which are of interest to us. 


The first method MailMessaageBuilder makes copies of the parameters defined in the config.groovy file. Here the local copies of overrideAddress, defaultFrom and default are of String type. The variables are then used for overriding the email addresses through the second method. So, if we modify the second method according to our requirements we are done.

 MailMessageBuilder(MailSender mailSender, ConfigObject config, MailMessageContentRenderer mailMessageContentRenderer = null) 
        this.mailSender = mailSender
        this.mailMessageContentRenderer = mailMessageContentRenderer

        this.overrideAddress = config.overrideAddress ?: null
        this.defaultFrom = overrideAddress ?: (config.default.from ?: null)
        this.defaultTo = overrideAddress ?: ( ?: null)
         protected String[] toDestinationAddresses(addresses) {
        if (overrideAddress) {
            addresses = addresses.collect { overrideAddress }
        addresses.collect { it?.toString() } as String[]


Since the to destination addresses method takes the email addresses as the argument and returns the array of overridden email addresses, if we change the return array, our job is done. Suppose I wanted to add email1 and email2 as overridden email addresses, I did it as follows -

     protected String[] toDestinationAddresses(addresses) {
       def overriddenAddresses = {email1, email2}
       def tempAddresses = addresses
        addresses = []
        if (overrideAddress) {
            overriddenAddresses.each{String overriddenAddress -> 
            tempAddresses = tempAddresses.collect { overriddenAddress }
        addresses.collect { it?.toString() } as String[]


About Author

Author Image
Manish Kumar Narang

Manish is an experienced Backend Developer with several years of industry experience in the IT field. He possesses a wide range of skills, including expertise in Backend languages like Core Java, J2EE, Hibernate, Spring/Spring Boot, and Python. Manish is also proficient in relational databases such as MySQL, PostgreSQL, and Oracle. He has hands-on experience in API implementations, web services development, testing, and deployments. Manish has contributed to various internal and client projects, including PMO, Catalyst, Communication-Scaffold, Oodles-Dashboard, and Devops Support, delivering significant business value. He is known for his innovative mindset and excellent problem-solving abilities. He keeps himself updated with new technologies by reading about them. He is skilled at collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and manage expectations. Manish conducts regular project status meetings, ensuring regular updates to clients and stakeholders regarding project progress, risks, and issues. Additionally, he serves as a mentor and coach to junior developers, offering guidance on project management best practices and fostering their skills development.

Request for Proposal

Name is required

Comment is required

Sending message..