Grails备份电子邮件用于grails邮件插件

时间:2018-03-13 01:27:39

标签: grails grails-plugin grails-2.0 grails-2.4

即时通讯使用grails 2.4.5并考虑设置备用电子邮件的方法,如果主电子邮件无法发送。

我正在寻找一种方法来确定电子邮件是否已成功发送,但未包含在文档中https://grails.org/plugin/mail?skipRedirect=true

我尝试使用try catch但我不起作用(特别是在构建版本中)。

这是我的服务功能

class EmailService {

    def grailsApplication
    def mailService

    def sendMail(Map dataMap){

        Boolean sent = true
        // Store the default mail settings in variables
        def defaultFrom = grailsApplication.config.grails.mail.default.from
        String defaultHost = grailsApplication.config.grails.mail.host
        def defaultPort = grailsApplication.config.grails.mail.port
        String defaultUsername = grailsApplication.config.grails.mail.username
        String defaultPassword = grailsApplication.config.grails.mail.password
        def defaultProps = grailsApplication.config.grails.mail.props

        if(!dataMap?.model) dataMap.model = [:]
        if(dataMap?.to?.getClass() == String) dataMap.to = [dataMap?.to]

        try {
            mailService.sendMail {
                to(dataMap?.to?.toArray())
                subject(dataMap?.subject)
                body(view: dataMap?.view, model: dataMap?.model)
            }
        }
        catch (Exception e) { // Change the properties here; send the email
            def backUpMail = grailsApplication.config.grails.backUpMail

            grailsApplication.config.grails.mail.default.from = backUpMail.default.from
            grailsApplication.config.grails.mail.host = backUpMail.host
            grailsApplication.config.grails.mail.port = backUpMail.port
            grailsApplication.config.grails.mail.username = backUpMail.username
            grailsApplication.config.grails.mail.password = backUpMail.password
            grailsApplication.config.grails.mail.props = backUpMail.props

            try {
                mailService.sendMail {
                    to(dataMap?.to?.toArray())
                    subject(dataMap?.subject)
                    body(view: dataMap?.view, model: dataMap?.model)
                }
            } catch (Exception err) {
                sent = false
            }

        }
        // Set the original settings back
        finally {
            grailsApplication.config.grails.mail.host = defaultHost
            grailsApplication.config.grails.mail.port = defaultPort
            grailsApplication.config.grails.mail.default.from = defaultFrom
            grailsApplication.config.grails.mail.username = defaultUsername
            grailsApplication.config.grails.mail.password = defaultPassword
            grailsApplication.config.grails.mail.props = defaultProps
        }

        return sent
    }

}

当我禁用我的互联网连接时,我能够捕获异常,但它不能解决其他错误,如身份验证错误等。

另一个我遇到的问题是当第一封电子邮件的连接被切断时(在构建版本中)。它停了下来 DEBUG SMTP:尝试连接主机... 它不会进入catch块,因为没有发生错误

0 个答案:

没有答案