尝试在RoR应用程序中发送电子邮件时遇到SMTP错误

我一直在拼命尝试使用RoR中的Action Mailer类发送电子邮件,即使终端显示正在发送消息,它仍然会返回此错误:

et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response' from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain' from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'... 

这是我的邮件程序类在app / mailers / my_mailer.rb中的样子:

 class MyMailer < ActionMailer::Base default from: "from@example.com" def welcome_email(user) @user = user mail(to: user.email, from: 'example_email@gmail.com', subject: 'Welcome!') end end 

这是我的config / application.rb文件的样子:

 config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, #domain: 'gmail.com', user_name: 'example_email@gmail.com', password: 'my_password', authentication: 'plain', enable_starttls_auto: true } config.action_mailer.default_url_options = { :host => "localhost", :port => 3000 } 

在我的config / environments / development.rb文件中我也有这两行:

  config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_deliveries = true 

我的问题是每当我启动rails控制台(在开发中工作,不知道如何在生产中运行)时,将用户的电子邮件分配给用户变量,然后输入命令: MyMailer.welcome_email(user).deliver

终端说:

 Rendered my_mailer/welcome_email.html.erb (11.7ms) Rendered my_mailer/welcome_email.text.erb (0.5ms) Sent mail to abhas.arya@yahoo.com (923.1ms) Date: Fri, 08 Aug 2014 13:54:38 -0400 From: abhas.aryaa@gmail.com To: abhas.arya@yahoo.com Message-ID:  Subject: Welcome! Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f"; charset=UTF-8 Content-Transfer-Encoding: 7bit 

但然后呈现错误。 我的观点很简单,我认为我不需要在这里包含它们。 我拼命尝试了一切,包括解锁谷歌validation码,但仍然没有任何作用。 我很感激任何帮助,我想要的是将电子邮件发送到正确的收件箱。 如果你能解决这个问题,我会永远爱你!

您看到此错误的原因是Google已阻止您的IP。 您需要为此IP启用授权。

转到http://www.google.com/accounts/DisplayUnlockCaptcha ,然后点击“继续”。

然后再次尝试从应用程序发送电子邮件,它应该工作。 您需要在访问上述链接后的10分钟内从您的应用发送电子邮件。 通过访问上述链接,Google将授予访问权限,以便在10分钟内注册新应用。

我解决了以下问题:

1)登录Google Apps管理员门户,在安全首选项下选中“允许用户管理对安全性较低的应用的访问”,启用安全性较低的应用。

2)登录将用作邮件程序的帐户,并将“允许安全性较低的应用程序”设置为“开启”。

在线提供的大多数答案均参考第2步,但如果没有管理员启用该function,则无法在用户级别进行配置。

经过一番调查后,这个问题解决了我:

点击https://www.google.com/settings/u/0/security/lesssecureapps

通过使用smtp配置中提供的电子邮件地址登录,可以在此处启用对安全性较低的应用的访问。

这样做我解决了我的问题。

 config.action_mailer.default :charset => "utf-8" config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'mysite.com', user_name: myemail@gmail.com, password: mypassword, authentication: 'plain', enable_starttls_auto: true } 

如果您在帐户设置中关闭了安全性较低的应用,Google会尝试阻止您的登录。 因此,请按照此链接和“ 打开 ”访问权限,以获得安全性较低的应用。