【问题标题】:Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at ):Net::SMTPAuthenticationError(需要 530-5.5.1 身份验证。在 了解更多信息):
【发布时间】:2015-12-25 06:41:53
【问题描述】:

我正在关注第 10 章的 Rails 教程。 由于我没有信用卡,我正在尝试发送电子邮件。我是编程新手,所以我只想看看它是如何工作的。

但是,我收到了这个错误。

    Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at
    ):
      app/models/user.rb:65:in `send_password_reset_email'
      app/controllers/password_resets_controller.rb:13:in `create'

  Rendered /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_source.erb (5.3ms)
  Rendered /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_trace.html.erb (2.6ms)
  Rendered /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/_web_console.html.erb (0.9ms)
  Rendered /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/diagnostics.html.erb within rescues/layout (30.9ms)

谁能告诉我我错过了哪里?

开发.rb

  config.cache_classes = false
  config.eager_load = false
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => 'mail.google.com',
    :user_name            => ENV['#I hardcoded it here'], # I put my my email which I used regularly here.
    :password             => ENV['#I hardcoded it here'], # I put the password here.
    :authentication       => 'plain',
    :enable_starttls_auto => true
  }

  config.active_support.deprecation = :log
  config.active_record.migration_error = :page_load
  config.assets.debug = true
  config.assets.digest = true
  config.assets.raise_runtime_errors = true

user_mailer.rb

class UserMailer < ApplicationMailer
  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Account activation"
  end

  def password_reset(user)
    @user = user
    mail to: user.email, subject: "Password reset"
  end
end

application_mailer.rb

class ApplicationMailer < ActionMailer::Base
  #default from: "from@example.com"
  default from: "xxxxxx@gmail.com" # I put my my email which I used regularly here.
  layout 'mailer'
end

password_reset_controller.rb

class PasswordResetsController < ApplicationController
  before_action :get_user,         only: [:edit, :update]
  before_action :valid_user,       only: [:edit, :update]
  before_action :check_expiration, only: [:edit, :update]

  def new
  end

  def create
    @user = User.find_by(email: params[:password_reset][:email].downcase)
    if @user
        @user.create_reset_digest
        @user.send_password_reset_email
        flash[:info] = "Email sent with password reset instructions"
        redirect_to root_url
    else
        flash.now[:danger] = "Email address not found"
        render 'new'
    end
  end

  def edit
  end

  def update
    if params[:user][:password].empty?
        @user.errors.add(:password, "can't be empty")
        render 'edit'
    elsif @user.update_attributes(user_params)
        log_in @user
        flash[:success] = "Password has been reset."
        redirect_to @user
    else
        render 'edit'
    end

  end

  private

  def user_params
    params.require(:user).permit(:password, :password_confirmation)
  end

  def get_user
    @user = User.find_by(email: params[:email])
  end

  # Confirms a valid user.
  def valid_user
    unless (@user && @user.activated? && @user.authenticated?(:reset, params[:id]))
        redirect_to root_url
    end
  end

  # Checks expiration of reset token.
  def check_expiration
    if @user.password_reset_expired?
        flash[:danger] = "Password reset has expired."
        redirect_to new_password_reset_url
    end
  end
end

【问题讨论】:

  • 通过在发件人电子邮件中打开不太安全的应用程序选项来解决问题。我不知道这是否是一个好习惯。但它现在有效。谢谢!
  • 完全忘了提哈哈.. :) 很高兴终于解决了..

标签: ruby-on-rails railstutorial.org


【解决方案1】:

我想建议你跟进

一定能帮到你

编辑更新

Allow less secure apps to access accounts

Google 可能会阻止某些应用或设备的登录尝试 不使用现代安全标准。由于这些应用程序和设备 更容易侵入,阻止它们有助于确保您的帐户安全。

一些不支持最新安全的应用示例 标准包括:

  • 装有 iOS 6 或更低版本的 iPhone 或 iPad 上的邮件应用程序邮件应用程序

  • 在 8.1 版本之前的 Windows 手机上的一些桌面邮件

  • Microsoft Outlook 和 Mozilla Thunderbird 等客户端

【讨论】:

  • 我删除了用户名和密码中的 ENV[],就像 Rajarshi Das 的建议一样。它给了我这个错误:Net::SMTPAuthenticationError (534-5.7.14 accounts.google.com/…):
【解决方案2】:

希望你不要在环境变量ENV中设置用户名和密码,而是写ENV["the actual user name"]

所以它没有通过。

如果您想通过 gmail 发送邮件,请尝试使用 gmail.com

你可以使用

config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => "gmail.com",
    :user_name            => "test@gmail.com" #your gmail id
    :password             => "1234" #your gmail password
    :authentication       => :plain,
    :enable_starttls_auto => true
  }

或者如果你想在 env 中设置密码

config.action_mailer.smtp_settings = {
  address: "smtp.gmail.com",
  port: 587,
  domain: "gmail.com",
  authentication: "plain",
  enable_starttls_auto: true,
  user_name: ENV["GMAIL_USERNAME"],
  password: ENV["GMAIL_PASSWORD"]
}

如果您熟悉 Unix,您可能有过设置的经验 环境变量。 Unix 环境变量通常设置在 启动交互式 shell 时读取的文件(~/.bashrc bash shell 的文件)。

为了狂欢 shell,编辑 ~/.bashrc 文件并添加:

export GMAIL_USERNAME="myname@gmail.com" 
export GMAIL_PASSWORD="mypassword"

【讨论】:

  • 重启?该应用程序使用gmail.com检查您的gmail和pssword
  • 重启?该应用使用 gmail.com 检查您的 gmail 和 pssword
  • 它给了我这个错误:Net::SMTPAuthenticationError (534-5.7.14 accounts.google.com/Conti):
  • 我应该使用 " 或 ' 像 "mypassword" 或 'mypassword' 吗?
  • 你可以使用任何一种语法都没有问题 -
猜你喜欢
  • 2017-12-05
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2020-08-19
  • 2012-07-28
  • 1970-01-01
相关资源
最近更新 更多