在运行时更改邮件配置 [英] Changing mail configuration in runtime

查看:119
本文介绍了在运行时更改邮件配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始调查grails框架,第一个任务是要发送电子邮件。基本教程和社区答案提供了很多信息,以便快速启动,是的,我创建了简单的应用程序,可能发送电子邮件。但是,我的调查的下一个点是在运行时改变邮件配置。所以,首先我在 Config.grovy 中的配置是

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

}

带有值,所有的工作都是核心的,之后我试过重新配置
像这样

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

控制器,发现邮件正在从旧地址发送,调试后我发现邮件插件中有自动连线的实例,如code> mailSender ,一个明显的解决方案是重新创建 mailSender 并重新设置,但是根据Spring单例策略判断,这将是很难的解决方案,所以,我的问题是否有可能重新配置电子邮件在运行时没有类重新加载?



谢谢。

解决方案

修复使用重新启动 mailsender 实例,但等待其他解决方案,谢谢

  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);
}
})


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.

解决方案

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");
    }
})

这篇关于在运行时更改邮件配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆