title: Connection refused - connect(2) Ruby on Rails Mail Setup
body: <p>I have set smtp settings in both <code>config/environments/production.rb</code> and <code>development.rb</code> also i have added the settings in `config/initializers/setup_mail.rb</p>
  
  <pre><code>config.action_mailer.default_url_options = { :host =&gt; 'ipaddress' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              =&gt; 'smtp.gmail.com',
    :port                 =&gt; 587,
    :domain               =&gt; 'gmail.com',
    :user_name            =&gt; 'EMAIL_ADDRESS@gmail.com',
    :password             =&gt; 'pass',
    :authentication       =&gt; :plain,
    :enable_starttls_auto =&gt; true,
    :openssl_verify_mode  =&gt; 'none'
  }
  </code></pre>
  
  <p>config/initializers/setup_mail.rb</p>
  
  <pre><code>ActionMailer::Base.smtp_settings = { 
    :address              =&gt; 'smtp.gmail.com',
    :port                 =&gt; 587,
    :domain               =&gt; 'gmail.com',
    :user_name            =&gt; 'EMAIL_ADDRESS@gmail.com',
    :password             =&gt; 'pass',
    :authentication       =&gt; :plain,
    :enable_starttls_auto =&gt; true,
    :openssl_verify_mode  =&gt; 'none'
  } 
  
  ActionMailer::Base.default_url_options[:host] = "ipaddress" 
  </code></pre>
  
  <p>i am getting the error <code>Connection refused - connect(2)</code></p>
  
  <p>while i have working in localhost with below configuration I haven’t get any error and also mail has been sent.</p>
  
  <p>config/initializers/setup_mail.rb (localhost)</p>
  
  <pre><code>ActionMailer::Base.smtp_settings = { 
    :address              =&gt; 'smtp.gmail.com',
    :port                 =&gt; 587,
    :domain               =&gt; 'localhost',
    :user_name            =&gt; 'EMAIL_ADDRESS@gmail.com',
    :password             =&gt; 'pass',
    :authentication       =&gt; 'plain',
    :enable_starttls_auto =&gt; true
  } 
  
  ActionMailer::Base.default_url_options[:host] = "localhost:3000" 
  </code></pre>
  
  <p>Errors are show below while running in console, </p>
  
  <pre><code>Errno::ECONNREFUSED: Connection refused - connect(2)
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `initialize'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `open'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:541:in `tcp_socket'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `block in do_start'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:69:in `timeout'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:550:in `do_start'
  from /home/attuser/.rvm/rubies/ruby-1.9.3-p545/lib/ruby/1.9.1/net/smtp.rb:520:in `start'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:415:in `block in deliver_mail'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `block in instrument'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/activesupport-3.2.9/lib/active_support/notifications.rb:123:in `instrument'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/actionmailer-3.2.9/lib/action_mailer/base.rb:413:in `deliver_mail'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
  from (irb):28
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
  from /home/attuser/.rvm/gems/ruby-1.9.3-p545/gems/railties-3.2.9/lib/rails/commands.rb:41:in `&lt;top (required)&gt;'
  from script/rails:6:in `require'
  </code></pre>
  
  <p>controller</p>
  
  <pre><code>def sendResume
  @name =params[:name]
  @email_id = params[:email_id]
  @mob_no = params[:ph_no]
  
  attachments = params[:resume]
  if simple_captcha_valid?
  
   if params[:resume]
       filename=attachments.original_filename   
  
      extname = File.extname(filename)[1..-1]
      mime_type = Mime::Type.lookup_by_extension(extname)
      content_type = mime_type.to_s unless mime_type.nil?
  
  
          if content_type !="application/pdf"
             flash[:error]= "Only pdf files are allowed"
             redirect_to :action=&gt;"careers"
          else
           File.open(Rails.root.join('tmp', 'uploads', attachments.original_filename), 'w') do |file|
            re = attachments.read
            file.write(re.force_encoding("utf-8"))
            @attached_path = file.path
          end
  
  
          begin
            ResumeMailer.sendResume(@name, @email_id, @mob_no, @attached_path, attachments.original_filename).deliver
            flash[:notice] = "Your resume has been submitted successfully"
            redirect_to :action=&gt;"careers"
          rescue Exception =&gt; e
            puts e.message
  
            logger.warn "error sending mail"
            flash[:error]= "Error in submitting resume"
            redirect_to :action=&gt;"careers"
          end
  
       end
      else
        flash[:error]= "Please upload your resume"
        redirect_to :action=&gt;"careers"
      end
  else
  
    flash[:error]= "Incorrect captcha"
     redirect_to :action=&gt;"careers"
  end
  
  end
  </code></pre>
  
  <p>After installing the <code>postfix</code> the error goes away in console mode and the mails send from console but in graphical mode (in browser) i am getting the error <code>Connection refused - connect(2)</code>. </p>
  
  <p>what is wrong,</p>
  
  <p>thanks in advance.</p>