开发者

Grails Mail plugin not working

I am trying to send mails from a Grails application, but without any success.

I've used gmail and other smtp server (without ssl!) but the same error occurs:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

I am using in Config.groovy (example for gmail):

grails.mail.host = "smtp.gmail.com"
grails.mail.from = "xxx@gmail.com"
grails.mail.port = "465"
grails.mail.ssl = "on"
grails.mail.username = "xxx@gmail.com"
grails.mail.password = "xxx"
grails.mail.props = ["mail.smtp.auth": "true",
        "mail.smtp.socketFactory.port": "465",
        "mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
        "mail.smtp.socketFactory.fallback": "false",
        "mail.smtp.starttls.enable": "true",
        "mail.debug": "true"]

EDIT: I made a simple app with just the mail plugin and a controller and th开发者_如何学编程e config posted by Javid Jamae works (3rd answer, also I think the other should work).

BUT even if I just copy-paste the same config and the same sending mail code, on my primary project it still gives me the same exception! I think this can be caused by Nimble plugin (Mail plugin was installed by it). My configuration is:

Grails version: 1.3.4

Groovy version: 1.7.4

JVM version: 1.6.0_21

jquery - 1.4.2.5

mail - 0.9

shiro - 1.0.1

nimble - 0.4-SNAPSHOT

FINAL EDIT : I resolved the issue: it seems that I have to use the same settings in the Nimble plugin also, in NimbleConfig.groovy -> mail { ... (must have "from = ...") } .

Stupid issue, but waisted a lot of time on it.


I'm not using SSL and I have the following defined at the bottom of my Config.groovy (not under the environments section):

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

I'm using:

app.grails.version=1.2.1
plugins.mail=0.9

This works for me.


You have enabled SSL:

grails.mail.ssl = "on"

And got exception

javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?.

So disable SSL (my config):

host = "smtp.gmail.com"
port = 465
username = "username@gmail.com"
password = "password"
javaMailProperties = ['mail.smtp.auth': 'true',
        'mail.smtp.socketFactory.port': '465',
        'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
        'mail.smtp.socketFactory.fallback': 'false']

Anyway, if you want to enable SSL - try 587 port.

Also try to set

mail.smtp.starttls.required : 'true'

Because if server not supports secure connection or client doesn't accept server's certificate secure connection will not started and you will got your exception. But after setting starttls.required = true and secure connection is impossible whole connection will fails so you got proper exception message.

P.S. Take a note that SSL and TLS - is different protocols.


I dont have this line in my configuration

"mail.smtp.starttls.enable": "true"

and my connection is working

also the port should not be in quotes

grails.mail.port = 465


I ran into a similar strain of this issue as well, so I'll share my experience. When using the Nimble plugin (which uses the Mail plugin as a dependency), Nimble defines its own mail settings in grails-app/conf/NimbleConfig.groovy.

The settings in NimbleConfig.groovy appear to overwrite those set in Config.groovy (presumably because NimbleConfig is executed after Config).

The solution here is conditional:

  • If you're using Nimble, set the mail properties in grails-app/conf/NimbleConfig.groovy; you do not need to set them in grails-app/conf/Config.groovy

  • If you're not using Nimble, then just follow the Mail plugin instructions for configuration (or use Javid Jamae's answer)

This seems to be what the OP alluded to in his/her edits, but I just thought I'd confirm the edits with my understanding of what's happening.

Update:

For a reference, here are the NimbleConfig.groovy settings that worked for me:

nimble {
    ...
    messaging {
        ...
        mail {
            host = 'smtp.gmail.com'
            port = 465
            username = '...@gmail.com'
            password = '...'
            props = [
                'mail.smtp.auth': 'true',
                'mail.smtp.socketFactory.port': '465',
                'mail.smtp.socketFactory.class': 'javax.net.ssl.SSLSocketFactory',
                'mail.smtp.socketFactory.fallback': 'false'
            ]
        }
    }
}


You can verify your config settings at runtime by checking the mailService.mailSender properties.Something like this: mailService.mailSender.properties.each{println} It will yield the host, port, username, password and a few other values. If you're sure they are all correct, I would suspect a firewall issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜