Grails Mail-Plugin and Custom EmailerService

Just had an old project that got both Grails Mail-plugin installed and also a custom EmailerService implemented. Problem was that both approaches are configuring a org.springframework.mail.javamail.JavaMailSenderImpl in two different ways.

Grails Mail-Plugin was doing it via following Closure:

grails {
  mail {
    host = "smtp.gmail.com"
    port = 465
    username = "username@test.com"
    password = "password"
    props = [
      "mail.smtp.auth":"true",
      "mail.smtp.socketFactory.port":"465",
      "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
      "mail.smtp.socketFactory.fallback":"false"
   ]
  }
}

Whereas EmailerService implementation was injected a dependency of type org.springframework.mail.javamail.JavaMailSenderImpl which was created in resources.groovy:

beans = {
  mailSender(org.springframework.mail.javamail.JavaMailSenderImpl) {
    host = 'smtp.gmail.com'
    port = 465
    username = 'test@test.com'
    password = 'password'
    javaMailProperties = [
      'mail.smtp.auth': 'true',
      'mail.smtp.socketFactory.port': '465',
      'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
      'mail.smtp.socketFactory.fallback': 'false'
   ]
  }
}

So we got confused because both approaches were providing an org.springframework.mail.MailSender thus overwriting each other when injecting the dependency.

One thought on “Grails Mail-Plugin and Custom EmailerService

  1. Pingback: Modularization: Dependency-injection versus grails.util.Environment checks

Comments are closed.