Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 791
cripox Avatar asked Feb 26 '23 07:02

cripox


2 Answers

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.

like image 132
Javid Jamae Avatar answered Mar 08 '23 16:03

Javid Jamae


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.

like image 24
Oleksandr Avatar answered Mar 08 '23 17:03

Oleksandr