how to call variable from config.groovy

566 views Asked by At

I am using below code in order to use mail plugin in grails

      try {
                CH.config.grails.mail.default.from = "${parent_personal_data.email}"
                mailSender.username = "${parent_personal_data.email}"
                mailSender.password = "${parent_data.password}"
                mailService.sendMail {
                to "${employee_personal_data.email}"
                subject "new task"
                body "you have been added to project and you are given a task"
          }
      }
      catch (Exception e) {
      // catch block code
      }
      // Set the original settings back
      finally {
          CH.config.grails.mail.default.from = defaultFrom
          mailSender.username = oldUsername
          mailSender.password = oldPassword
      }

how ever it keep on showing me the error NO such property: CH no such property :mailSender

1

There are 1 answers

0
Gregg On

I have no idea what CH is in your code or what version of grails you're using, but the appropriate way to get configuration elements is either via grailsApplication or Holders.

In a controller or service, you can inject the grailsApplication.

class SomeService {
  def grailsApplication

  def someMethod() {
    def from = grailsApplication.config.grails.mail.default.from
    ...
  }
}

Or you can use the grails.util.Holders class.

class SomeService {
  def grailsApplication

  def someMethod() {
    def from = Holders.config.grails.mail.default.from
    ...
  }
}

Read some docs.