Net::SMTPAuthenticationError 502 5.5.2 in Rails ActionMailer

我只是一个虾纸丫 提交于 2019-12-02 04:39:59

问题


i'm trying to send confirmation email to the user.

But i get following error:

Net::SMTPAuthenticationError (502 5.5.2 Error: command not recognized

Configuration in production.rb is following:

# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true

# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp

# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
  :address        => 'path_to_address_specified_by_my_hoster',
  :port           => 25,
  :domain         => 'my_domain.com',
  :authentication => :plain,
  :user_name      => 'signup@my_domain.com',
  :password       => 'password'
}

I have created a mailbox in user profile at my hosting provider, named "signup@my_domain.com"

For created mailbox, they issued to me login and password:

login = verbose_login

password = verbose_password

I did't completely understood the exact format of :user_name.

Should i use

:user_name => "signup@my_domain.com"

or:

:user_name => "signup"

or:

:user_name => "verbose_login"

Or this field is specific to the mail server, and i must ask support of hosting provider ?

And what the difference between :authentication => :plain and :login ?

Thanks.


回答1:


This works well for me:

config.action_mailer.smtp_settings = {
    :address              => 'smtp.gmail.com',
    :port                 => 587,
    :domain               => 'gmail.com',
    :user_name            => 'my_nick@gmail.com',
    :password             => 'secret_password',
    :authentication       => 'login',
    :enable_starttls_auto => true
  }

more info here

Petr




回答2:


I have got the same error recently, and the reason was incorrect format of recipients

recipient = IO.readlines(emails_filename).first
mail(:to => recipient, :subject => subject)

Don't forget to add strip to get clean email addresses.




回答3:


I had the same problem, but it is just a google configuration issue.

For some reason Google was blocking access from unknown location (app in production), so to solve this you can go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps).

On the other hand, you could login to your gmail account, then go to https://www.google.com/settings/security/lesssecureapps and enable the access to less secure applications, which was the solution for me!




回答4:


Please try to login with browser once. There might be some issue with login (it will ask captcha etc).

Once you successfully logged-in, then try with Rails Mailer. It should work now.

This issue generally happens with test account as we do not login via browser usually. Then mail providers ask for confirmation like captcha, dob or answer of security question etc.




回答5:


You've to use ActionMaliler::Base instead of config

   ActionMailer::Base.smtp_settings   #try this

  config.action_mailer.smtp_settings   #instead of this

Change your smtp code to below. It should work now.

      ActionMailer::Base.smtp_settings = {
         :address        => 'path_to_address_specified_by_my_hoster',
         :port           => 25,
         :domain         => 'my_domain.com',
         :authentication => :plain,
         :user_name      => 'signup@my_domain.com',
         :password       => 'password'
          }


来源:https://stackoverflow.com/questions/3373130/netsmtpauthenticationerror-502-5-5-2-in-rails-actionmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!