Ruby通过gmail smtp发送邮件

Kri*_*dás 9 ruby email

我正在创建一个ruby脚本来检查url的响应状态,如果它等于504,它会将邮件发送到另一个电子邮件地址.出于某种原因,我得到了这个:/usr/lib/ruby/1.9.1/net/smtp.rb:960:in 'check_auth_response': 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbv9z (Net::SMTPAuthenticationError) 我检查了身份验证数据并且它们是有效的.也许代码中可能存在错误:

require 'mail'

options = { :address              => "smtp.gmail.com",
            :port                 => 587,
            :user_name            => '<myusername>',
            :password             => '<mypassword>',
            :authentication       => 'plain',
            :enable_starttls_auto => true  }

Mail.defaults do
  delivery_method :smtp, options
end

Mail.deliver do
       to 'dude920228@gmail.com'
     from 'dude92@dude92.koding.com'
  subject 'Test'
     body 'Hurray!!! Test email!'
end
Run Code Online (Sandbox Code Playgroud)

哦,我收到谷歌的通知,一个不太安全的应用程序试图访问我的帐户,所以我设置不太安全的应用程序可以使用我的帐户.

rai*_*inz 18

如果您没有使用2因素身份验证

转到此链接:

https://www.google.com/settings/security/lesssecureapps

并选择:

"访问安全性较低的应用"

按照:

https://support.google.com/accounts/answer/6010255?hl=en

在这种情况下,您将使用正常的电子邮件和密码进行连接.

如果您正在使用双因素身份验证

您需要为应用程序创建特定于应用程序的密码.跟着这些步骤:

  1. 转到gmail
  2. 点击右上角的个人资料图标,然后选择"我的帐户"
  3. 点击"登录和安全"
  4. 向下滚动"登录和安全"页面,有一个名为"应用密码"的部分点击它.
  5. 您应该会看到标有"选择应用"的下拉列表.选择邮件.
  6. 对于"在我的设备上"下拉列表,选择"其他"并输入命令行或任何您想要调用该应用程序的内容.
  7. 点击"生成".将生成密码.复制该密码并使用生成的密码替换您在选项哈希中使用的密码:

    options = {:address =>"smtp.gmail.com",:port => 587,:user_name =>'',:password =>'',:authentication =>'plain',: enable_starttls_auto => true}

那应该是它.我刚试过这个,它对我有用.

另外,请确保您的用户名是完整的Gmail电子邮件地址.

您还可以在此处找到"官方文档":https://support.google.com/accounts/answer/185833?hl = zh-CN