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

<h2>症状</h2>
RailsのMailerを使い、gmailを通してメール送信設定をしようとしたところ、以下のエラーが表示されました。
翻訳すると「ユーザー名とパスワードは受け入れられません」でした

```terminal
[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'

```

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/927880/0499cd27-db64-7a97-eebe-30fd32a2594e.png)

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

<h2>やったことけど、解決しなかったこと(念のため残す)</h2>
<h3>IMAP有効</h3>
以下で紹介されているgmailのIMAPを有効にしてみた。しかし、変化なし。。。

https://qiita.com/gazayas/items/9fe0cce69c2785e8b1b9#%E6%AC%A1%E3%81%AE%EF%BC%92%E3%81%A4%E3%81%AF%E5%BF%85%E8%A6%81%E3%81%8B%E3%81%A9%E3%81%86%E3%81%8B%E3%81%AF%E5%88%86%E3%81%8B%E3%82%89%E3%81%AA%E3%81%84%E3%81%91%E3%81%A9%E7%89%B9%E3%81%AB%EF%BC%91%E7%95%AA%E7%9B%AE%E3%82%92%E3%82%84%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%9F%E6%96%B9%E3%81%8C%E3%81%84%E3%81%84%E3%81%A8%E6%80%9D%E3%81%84%E3%81%BE%E3%81%99

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

https://qiita.com/cigalecigales/items/f4274088f20832252374#2-gmail%E3%81%AE%E8%A8%AD%E5%AE%9A

<h3>環境間違えてない？</h3>
念のため、config/environments/のdevelop、test、productionのそれぞれに設定を追加。

```terminal: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ファイルは以下で設定。

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

<h3>すべてのメールで POP を有効にする</h3>
以下のサイトを参考にして、POP設定を有効にしたものの変化なし。。。

https://qiita.com/k-kou5/items/1ed178680a3a45c1edf7

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

<h3></h3>

<h2>参考</h2>

https://qiita.com/cigalecigales/items/f4274088f20832252374#2-gmail%E3%81%AE%E8%A8%AD%E5%AE%9A

https://qiita.com/k-kou5/items/1ed178680a3a45c1edf7

https://github.com/laserlemon/figaro