views:

199

answers:

4

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 = "[email protected]"
grails.mail.port = "465"
grails.mail.ssl = "on"
grails.mail.username = "[email protected]"
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 the 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.

+1  A: 

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
Aaron Saunders
still the same exception even without this line.
cripox
see edit above.
Aaron Saunders
+1  A: 

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 = "[email protected]"
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.

Olexandr
Same Exception after trying what you said. I did tried port 587 and not using ssl="on" before posting here on stackoverflow. Something is very wrong and I curious if somebody else had this issue before.
cripox
@cripox did you try removing the quotes from your port as I suggested? I am running an application using the config below and it works fine
Aaron Saunders
unfortunately is still not working- tried from home computer and also deployed on a vps
cripox
have you tried mail.smtp.starttls.required : 'true' ? and does it changed the exception message ?
Olexandr
@Olexandr : yes, I tried also with starttls.required:"true" but the Exception is the same
cripox
@Aaron , @Olexandr - please see my edit
cripox
+2  A: 

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 = "[email protected]"
     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.

Javid Jamae
I'm closing this thread. I didn't find a solution but I see that the question is too old and nobody answer anymore.
cripox