Search code examples
springemailgrails

Changing mail configuration in runtime


I've just started to investigate grails framework and first task I'm trying to resolve is sending email. Basic tutorial and community answers provided lot of information for quick start, and yes I've created simple app with possibility to send email. But, next point of my investigation was changing mail configuration in runtime. So, first my configuration in Config.grovy was

grails {
    mail {
        host = ""
        port = 0
        username = ""
        password = ""
        props = [""]
    }
}

with values, and all worked corectlly, after that I've tried to re-config it like that

grailsApplication.config.grails.mail.host = "smtp.gmail.com"
grailsApplication.config.grails.mail.port = 465
grailsApplication.config.grails.mail.username = ""

from controller, and found that mail is sending from old adress, after debugs I've found that there are auto-wired instances in mail plugins like mailSender and one obvious solution is recreate mailSender and re-set it, but judging to Spring singleton policy it will be hard solution, so, My question Are there possibilities to re-configure mail in runtime without class-reloading ?

Thanks.


Solution

  • Fixed using re-init mailsender instance but waiting for other solutions, Thanks

    mailSender.setHost("smtp.gmail.com")
    mailSender.setPort(465)
    mailSender.setJavaMailProperties(new Properties() {
        {
            put("mail.smtp.auth", "true");
            put("mail.smtp.socketFactory.port", "465");
            put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            put("mail.smtp.socketFactory.fallback", "false");
        }
    })