1
votes

I have a list of persons with individual emails (on different kind of mail services, for example gmail and hotmail). I want to send mail from their respective email addresses, like this:

mailService.sendMail {
   from "[email protected]"
}

In order to send mail I must set the configuration in Config.groovy. Should I maintain all emails configuration in Config.groovy file? or some other solution exist for this problem?

2

2 Answers

2
votes

The configuration only allows the sending from one SMTP server. The account that sends the email is not necessarily the "from" address even though it is being emailed from that account. You should be able to use one account as the SMTP server and change the "from" as needed.

1
votes

The configuration item sets the "default" from address for outgoing messages. The plugin provides a DSL that is used to specify the components of the message, including a specific From address if you want. If you don't provide a from specification in the message DSL, then it uses the configuration specified value.

Here is a snippet of code that I use in my messaging system to set a user account supplied from address on outgoing messages:

        mailMessage = mailService.sendMail {
            multipart true
            if (toAddresses) { to toAddresses }
            if (ccAddresses) { cc ccAddresses }
            if (bccAddresses) { bcc bccAddresses }
            from messageSpecification.from
            subject messageSpecification.subject
            if (messageSpecification.plainText) { text messageSpecification.plainText }
            if (messageSpecification.htmlText) { html messageSpecification.htmlText }
            messageSpecification.attachments.each {
                attach(it.filename, it.mediaType, it.data)
            }
        }

Simply replace the messageSpecification.from reference to your specific from address and you are good to go.