1

圣杯项目。我使用电子邮件插件并在尝试调用方法“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

配置.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?'
                }

我究竟做错了什么?

解决方案

4

3 回答 3

1

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

于 2012-10-03T16:42:12.507 回答
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'
    }
}

也不要忘记在你的班级中注入邮件服务

def mailService

这会将服务注入您的班级

这个配置足以发送邮件

于 2014-06-29T16:01:00.687 回答
0

您是否尝试过删除

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

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

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

于 2012-10-03T18:45:44.920 回答