grails邮件不工作

时间:2012-10-03 15:45:50

标签: email grails

grails项目。我使用电子邮件插件并在尝试调用方法' sendMail'

时获得异常

错误:

Caused by: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp. Failed messages: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp; message exceptions (1) are:
Failed message 1: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp

Config.groovy中

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

发送示例

sendMail {
                    to "fred@gmail.com"
                    subject "Hello Fred"
                    body 'How are you?'
                }
我做错了什么?

solution

3 个答案:

答案 0 :(得分:1)

错误说Unable to locate provider for protocol: smtp。看来你错过了一些smtp协议的依赖,这看起来很奇怪,显然你缺少javamail(?)。因为代码对我来说似乎很好。

答案 1 :(得分:0)

您是否尝试删除

            "mail.smtp.port":"465",
            "mail.debug":"true",

来自你的配置?这是我在代码和官方示例之间看到的唯一区别。

更新:正如GalmWing已经说过的那样,你的java.mail jar文件似乎有问题。请检查此解决方案:Using Java to send emails on gmail account。听起来像你有同样的问题。你使用哪个java版本?

答案 2 :(得分:0)

添加到config.groovy

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

使用此代码发送邮件

def sendEmail(){

    mailService.sendMail {
        to "email","**email**"
        from "email"
        subject "Hi"
        body 'Hi'
    }
}

也不要忘记在你的课程中注入mailservice

def mailService

这会将服务注入您的班级

此配置足以发送邮件