0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails】gmailを使ってメールを送信しようとすると、Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted.とかいう地獄の対処法【Mailer】

Last updated at Posted at 2021-12-03

症状

RailsのMailerを使い、gmailを通してメール送信設定をしようとしたところ、以下のエラーが表示されました。 翻訳すると「ユーザー名とパスワードは受け入れられません」でした
[ActiveJob] [ActionMailer::MailDeliveryJob] [af6c5cd6-78e7-40b7-a56f-8e982a58fa84] Error performing ActionMailer::MailDeliveryJob (Job ID: af6c5cd6-78e7-40b7-a56f-8e982a58fa84) from Async(mailers) in 1184.31ms: Net::SMTPAuthenticationError (535-5.7.8 Username and Password not accepted. Learn more at
):
C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:1015:in `check_auth_response'
C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:776:in `auth_plain'  
C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:768:in `public_send' 
C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:768:in `authenticate'C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:604:in `do_start'    
C:/Ruby30-x64/lib/ruby/3.0.0/net/smtp.rb:557:in `start'       
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:109:in `start_smtp_session'     
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:100:in `deliver!'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/mail-2.7.1/lib/mail/message.rb:2159:in `do_delivery'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/mail-2.7.1/lib/mail/message.rb:260:in `block in deliver'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/base.rb:589:in `block in deliver_mail'        
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications.rb:180:in `block in instrument'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications.rb:180:in `instrument'        
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/base.rb:587:in `deliver_mail'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/mail-2.7.1/lib/mail/message.rb:260:in `deliver'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/message_delivery.rb:115:in `block in deliver_now'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/rescuable.rb:17:in `handle_exceptions'        
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/message_delivery.rb:114:in `deliver_now'      
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/actionmailer-6.0.4.1/lib/action_mailer/mail_delivery_job.rb:23:in `perform'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/execution.rb:40:in `block in perform_now'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:112:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/i18n-1.8.10/lib/i18n.rb:314:in `with_locale'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/translation.rb:9:in `block (2 levels) in <module:Translation>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `instance_exec'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/core_ext/time/zones.rb:66:in `use_zone'     
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/timezones.rb:9:in `block (2 levels) in <module:Timezones>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `instance_exec'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/logging.rb:25:in `block (4 levels) in <module:Logging>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications.rb:180:in `block in instrument'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/notifications.rb:180:in `instrument'        
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/logging.rb:24:in `block (3 levels) in <module:Logging>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/logging.rb:45:in `block in tag_logger'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/tagged_logging.rb:80:in `block in tagged'   
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/tagged_logging.rb:28:in `tagged'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/tagged_logging.rb:80:in `tagged'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/logging.rb:45:in `tag_logger'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/logging.rb:21:in `block (2 levels) in <module:Logging>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `instance_exec'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:139:in `run_callbacks'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/execution.rb:39:in `perform_now'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/execution.rb:25:in `block in execute'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:112:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/railtie.rb:43:in `block (4 levels) in <class:Railtie>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/execution_wrapper.rb:88:in `wrap'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/reloader.rb:72:in `block in wrap'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/execution_wrapper.rb:88:in `wrap'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/reloader.rb:71:in `wrap'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/railtie.rb:42:in `block (3 levels) in <class:Railtie>'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `instance_exec'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activesupport-6.0.4.1/lib/active_support/callbacks.rb:139:in `run_callbacks'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/execution.rb:23:in `execute'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/activejob-6.0.4.1/lib/active_job/queue_adapters/async_adapter.rb:70:in `perform'     
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:363:in `run_task'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:352:in `block (3 levels) in create_worker'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:335:in `loop'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:335:in `block (2 levels) in create_worker'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `catch'
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `block in create_worker'

今回の解決方法

アプリのアプリパスワードの設定方法を ・アプリ選択:「メール」 ・デバイス選択:「windows」 にしたら解決しました。(2段階認証ON状態)

image.png

今まで、その他で設定していたため、弾かれていたみたい。。。

やったことけど、解決しなかったこと(念のため残す)

IMAP有効

以下で紹介されているgmailのIMAPを有効にしてみた。しかし、変化なし。。。

安全性の低いアプリを許可

デフォルトでは安全性の低いアプリケーションからGmailへのアクセスが制限されているそうで、2段階認証を外して安全性の低いアプリを許可しました。しかし、変化なし。。。

環境間違えてない?

念のため、config/environments/のdevelop、test、productionのそれぞれに設定を追加。
production
  config.action_mailer.perform_deliveries = true 
  config.action_mailer.default_options = { from: ENV['EMAIL_ADDRESS'] }
  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,
    user_name: ENV['EMAIL_ADDRESS'],
    password: ENV['EMAIL_PASSWORD'],
    authentication: 'plain',
    enable_starttls_auto: true
  }

ちなみに、ENVファイルは以下で設定。

ENV
EMAIL_ADDRESS=example@gmail.com
EMAIL_PASSWORD=アプリパスワード

すべてのメールで POP を有効にする

以下のサイトを参考にして、POP設定を有効にしたものの変化なし。。。

figaro

環境変数のENV[`EMAIL_PASSWORD]などをfigaroを使わずに使っていたので、installしてサーバー再起動を実行。 しかし、変化なし。。。 debbug.loggerで参照してみると、普通に値は取れたので機能していることは確認できた。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?