如何使用ruby Mail gem通过gmail发送电子邮件

时间:2015-10-25 20:55:42

标签: ruby email smtp

2012年之前question的回答表明,以下内容可以通过Gmail发送电子邮件。

require 'mail'

Mail.defaults do
  delivery_method :smtp, {
    address: "smtp.gmail.com",
    port: 587,
    user_name: <username>,
    password: <password>,
    authentication: :plain,
    enable_starttls_auto: true
  }
end

Mail.deliver do
  from    <email>
  to      <email>
  subject 'This is a test email'
  body    'body'
end

然而,我试过了,我得到了

/home/<username>/.rvm/rubies/ruby-2.1.6/lib/ruby/2.1.0/net/smtp.rb:969:in `check_auth_response': 534-5.7.14 <https://accounts.google.com/ContinueSignIn?<url_parameters>(Net::SMTPAuthenticationError)
        from /home/<username>/.rvm/rubies/ruby-2.1.6/lib/ruby/2.1.0/net/smtp.rb:740:in `auth_plain'
        from /home/<username>/.rvm/rubies/ruby-2.1.6/lib/ruby/2.1.0/net/smtp.rb:732:in `authenticate'
        from /home/<username>/.rvm/rubies/ruby-2.1.6/lib/ruby/2.1.0/net/smtp.rb:567:in `do_start'
        from /home/<username>/.rvm/rubies/ruby-2.1.6/lib/ruby/2.1.0/net/smtp.rb:520:in `start'
        from /home/<username>/.rvm/gems/ruby-2.1.6/gems/mail-2.6.3/lib/mail/network/delivery_methods/smtp.rb:112:in `deliver!'
        from /home/<username>/.rvm/gems/ruby-2.1.6/gems/mail-2.6.3/lib/mail/message.rb:2141:in `do_delivery'
        from /home/<username>/.rvm/gems/ruby-2.1.6/gems/mail-2.6.3/lib/mail/message.rb:238:in `deliver'
        from /home/<username>/.rvm/gems/ruby-2.1.6/gems/mail-2.6.3/lib/mail/mail.rb:140:in `deliver'
        from mail.rb:14:in `<main>'

实现这一目标的现代方法是什么?我已经尝试为不太安全的应用程序启用访问权限。

0 个答案:

没有答案