# 错误 - 534-5.7.9 Application-specific password required

**URL:** <https://www.shiwantech.xyz/t/topic/25>\
**Category:** Discourse\
**Created:** [2023 年10 月 19 日 16:28 UTC](https://www.shiwantech.xyz/t/topic/25 "2023-10-19T16:28:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![baiyunshenchu2060](https://www.shiwantech.xyz/letter_avatar_proxy/v4/letter/b/7c8e57/32.png) [@baiyunshenchu2060](https://www.shiwantech.xyz/u/baiyunshenchu2060)\
发布日期： [2023 年10 月 19 日 16:28 UTC](https://www.shiwantech.xyz/t/topic/25/1 "2023-10-19T16:28:36Z")

</div>

使用ActionMailer透過Gmail寄信  
參考ihower的Ruby on Rails實戰聖經的ActionMailer - E-mail 發送章節。

在development.rb裡加入寄信的設定。

```auto
...
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host:"http://localhost:3000" }
  config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => "587",
    :domain => "gmail.com",
    :authentication => "plain",
    :user_name => "我的帳號@gmail.com",
    :password => "我的密碼",
    :enable_starttls_auto => true
  }
  ...

```

建立Mailer寄信程式，並嘗試寄給自己

```auto
> rails generate mailer UserMailer notify_comment

# In user_maiiler.rb
class UserMailer < ActionMailer::Base
    default :from => "我的帳號@gmail.com"

    def notify_comment
        mail(:to => "我的帳號@gmail.com", :subject => "New Comment")
    end
end

# 寄信
> rails c
> UserMailer.notify_comment.deliver_now!

```

但是出現錯誤Net::SMTPAuthenticationError: 534-5.7.9 Application-specific password required.

這是因為我使用了二階段驗證，如果要用gmail寄信，必須先從google設定App Password（也就是需要设定 **应用专用密码** ）。

以下是解法網址  
Sign in using App Passwords  
[https://support.google.com/accounts/answer/185833?hl=en](https://support.google.com/accounts/answer/185833?hl=en)

和設定App Password的網址。  
[https://security.google.com/settings/security/apppasswords](https://security.google.com/settings/security/apppasswords)

得到密碼後，直接取代掉development.rb中的:password =\> "我的密碼"即可。
