Errno :: ECONNREFUSED(连接被拒绝 - 连接(2)for“localhost”端口587)动作邮件

时间:2015-10-20 07:55:01

标签: ruby-on-rails ruby ruby-on-rails-4

当应用发送电子邮件时出现此错误:

Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 587):
  app/controllers/incidents_controller.rb:66:in `block in create'
  app/controllers/incidents_controller.rb:62:in `create'


  Rendered /home/jeremymontesinos/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.4ms)
  Rendered /home/jeremymontesinos/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.9ms)
  Rendered /home/jeremymontesinos/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /home/jeremymontesinos/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (16.8ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x000000033b4ef0 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x000000033b4bf8 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x000000033ad330 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

我的development.rb:

config.action_mailer.default_url_options = {:host => "localhost"}
config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  adress: 'auth.smtp.1and1.fr',
  port: '587',
  user_name: 'foo@bar.fr',
  password: 'password',
  authentication: :plain,
  enable_starttls_auto: true}
  config.action_mailer.raise_delivery_errors = true

Environment.rb:

# Load the Rails application.
require File.expand_path('../application', __FILE__)
ActionMailer::Base.default :content_type => "text/html"

# Initialize the Rails application.
Rails.application.initialize!

我的控制器(我发送发送电子邮件的命令):

  def create
    @incident = Incident.new(incident_params)
    @incident.user_id ||= current_user.id
    @incident.incident_state_id_for_user = 1
    @incident.incident_state_id_for_tech = 1


    respond_to do |format|
      if @incident.save
    @response = Response.new(content: "Incident créé par #{current_user.name} #{current_user.surname}", incident_id: @incident.id, sender_id: @incident.user_id)
    @response.save!
        AppMailer.incident_created.deliver_now
        format.html { redirect_to @incident, notice: 'Incident crée.' }
        format.json { render :show, status: :created, location: @incident }
      else
        format.html { render :new }
        format.json { render json: @incident.errors, status: :unprocessable_entity }
      end
    end
  end

App_mailer.rb: class AppMailer&lt; ApplicationMailer

  default from: "foo@bar.fr"

  def incident_created()

    mail(to: "bar@baz.com", subject: "Succefull !", body: "You're really cool!")

  end

end

和application_mailer.rb:

class ApplicationMailer < ActionMailer::Base
  default from: "foo@bar.fr" #Here is a fake mail adress
  layout 'mailer'
end

所以我使用LinuxMint 17.2,服务器Puma on rails,rails 4.2.3 ......

编辑:我尝试使用以下命令ping服务器:telnet auth.smtp.1and1.fr 587并且可以正常运行

我尝试将端口更改为:25和465并且无法正常工作

1 个答案:

答案 0 :(得分:2)

development.rb adress配置更改为address

这只是一个错字! ;-)

正如您从错误中看到的那样,尝试的连接是在正确的端口上,但在错误的主机上,所以问题是在主机名配置上搜索(至少,这就是我发现它的方式)