拒绝连接 - 连接(2) Ruby on Rails邮件设置

8

我在config/environments/production.rbdevelopment.rb中设置了smtp设置,同时我还在`config/initializers/setup_mail.rb`中添加了这些设置。

config.action_mailer.default_url_options = { :host => 'ipaddress' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
}

config/initializers/setup_mail.rb

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'gmail.com',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode  => 'none'
} 

ActionMailer::Base.default_url_options[:host] = "ipaddress" 

我遇到了错误 Connection refused - connect(2)

在本地主机上使用以下配置工作时,我没有遇到任何错误,并且邮件已发送。

config/initializers/setup_mail.rb(本地主机)

ActionMailer::Base.smtp_settings = { 
  :address              => 'smtp.gmail.com',
  :port                 => 587,
  :domain               => 'localhost',
  :user_name            => 'EMAIL_ADDRESS@gmail.com',
  :password             => 'pass',
  :authentication       => 'plain',
  :enable_starttls_auto => true
} 

ActionMailer::Base.default_url_options[:host] = "localhost:3000" 

在控制台运行时,下面会显示错误信息:

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 `<top (required)>'
from script/rails:6:in `require'

控制器

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=>"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=>"careers"
        rescue Exception => e
          puts e.message

          logger.warn "error sending mail"
          flash[:error]= "Error in submitting resume"
          redirect_to :action=>"careers"
        end

     end
    else
      flash[:error]= "Please upload your resume"
      redirect_to :action=>"careers"
    end
else

  flash[:error]= "Incorrect captcha"
   redirect_to :action=>"careers"
end

end

安装了postfix之后,在控制台模式下错误消失了,可以从控制台发送邮件,但在图形化界面(浏览器中)出现了Connection refused - connect(2)错误。
可能有什么问题呢?
提前感谢。

现在应用程序中有什么?我的意思是,是否有用户部分之类的东西,因为如果有,它至少会有一些内容。它不可能是空的。 - Caffeine Coder
@Trip,现在它在控制台中可以运行,但在浏览器中却不能运行。 - Helphin
你能否从你的生产系统到smtp.gmail.com的587端口进行telnet测试?也许是服务提供商在干扰。 - BooVeMan
@BooVeMan,你能告诉我如何进行Telnet吗? - Helphin
请参考以下链接: https://dev59.com/JG855IYBdhLWcg3wfUZM - Milind
显示剩余24条评论
2个回答

10

我看到您已经在两个服务器上尝试了相同的设置,但只有在生产/非本地主机环境中遇到问题。

这表明环境本身的网络配置存在问题。

在与应用程序相同的服务器上的命令行中,请尝试以下命令。

telnet smtp.gmail.com 587

你应该看到类似下面的内容

telnet smtp.gmail.com 587
Trying 173.194.79.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtp
如果您看不到这个,很可能会收到连接错误。这意味着您的计算机无法访问Gmail服务器。可能的问题是:a)整体出站网络连接性问题;b)防火墙特别阻止所有出站连接;c)防火墙阻止/允许连接到特定端口或主机。
如果这样仍然不起作用,请尝试使用以下端口替代587端口。
telnet smtp.gmail.com 465
telnet smtp.gmail.com 25

如果其中一种更成功,请更改您的邮件服务器设置以相应地使用它。

编辑:我们在使用Gmail和带有我们自定义域名的Gmail时遇到了很多问题。一个可以帮助的方法是从您的配置文件中删除:domain行,并尝试不使用它。

供参考,这是我在使用Gmail配置生产环境时使用的自定义域名:

config.action_mailer.default_url_options = { :host => "my.website.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'website.com',
    user_name:            'user@website.com',
    password:             'password',
    authentication:       'plain',
    enable_starttls_auto: true
}

另外,如果你在Gmail账户上启用了双重身份验证,你可能需要禁用它,然后再试一次,以确认它不会使问题更加复杂。


我得到了类似于Trying 173.194.79.108... Connected to gmail-smtp-msa.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP dd5sm276863pbc.85 - gsmtp这样的东西。 - Helphin
拿到这个之后应该做什么? - Helphin
@Helphin 这意味着服务器已经开启。你能否尝试从配置文件中的所有位置删除 :domain 行? - Dave Satch
我尝试了很多次都没有成功。在对此进行了一些更改后,我终于成功了,感谢您的友善回答。 - Helphin
@Helphin,我也遇到了同样的错误。你是如何解决这个问题的?能否请你告诉我具体步骤?从配置中删除域名是否有帮助? - Rails Developer

1
你将域名设置为localhost。域名应该是gmail.com。 请更改config/initializers/setup_mail.rb文件如下: 'smtp.gmail.com', :port => 587, :domain => 'gmail.com', :user_name => 'EMAIL_ADDRESS@gmail.com', :password => 'pass', :authentication => 'plain', :enable_starttls_auto => true }> 在production.rb中,你不需要配置电子邮件地址。以下几行就足够了。
config.action_mailer.default_url_options = { :host => 'app_url' }
config.action_mailer.delivery_method = :smtp

谢谢您的回答,但是在更新后我仍然遇到了同样的错误。 - Helphin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接