Rails 3.1 smtp gmail Errno::ECONNREFUSED Connection refused

October 9, 2011


This post was originally published in the Rambling Labs Blog on October 9, 2011.


I was trying out the rails 3.1 smtp mailer on the sandbox application I use to test all the new stuff and it was throwing this error:

Errno::ECONNREFUSED in TestController#create

Connection refused - connect(2)

And I had this configuration on config/environments/development.rb:

Demo::Application.configure do
  # ...
  config.action_mailer.delivery_method :smtp
  config.action_mailer.smtp_settings = {
    enable_starttls_auto: true,
    address: 'smtp.gmail.com',
    port: 587,
    domain: 'gmail.com',
    authentication: 'plain',
    user_name: '<email@yourdomain.com>',
    password: '<password>'
  }
  # ...
end

I tried quite a number of combinations for a couple of hours. I added the tsl: true, changed the domain to ‘ramblinglabs.com’, changed to my personal email, so on and so forth, and nothing worked. I saw several questions in stack overflow, but there was one that called my attention, ‘cause it was the same issue I was having.

In my frustration I kept changing things, forgetting one essential matter… You have to restart your rails application for the changes to get applied. Yeah, I know…

So, the only thing that I really had to do was removing the domain: 'gmail.com' and restart the rails application. The configuration left was this:

Demo::Application.configure do
  # ...
  config.action_mailer.delivery_method :smtp
  config.action_mailer.smtp_settings = {
    enable_starttls_auto: true,
    address: 'smtp.gmail.com',
    port: 587,
    authentication: 'plain',
    user_name: '<email@yourdomain.com>',
    password: '<password>'
  }
  # ...
end

And that was it! I posted an answer to the stack overflow question too.