【问题标题】:Attempt at authenticated SMTP in Ruby尝试在 Ruby 中经过身份验证的 SMTP
【发布时间】:2011-11-10 04:20:32
【问题描述】:

我查看了所有 SMTP ruby​​-docs,但不知道哪里出错了:

def send(username, password, data, toAddress, fromAddress)

    smtp = Net::SMTP.new('my.smtp.host', 25)
    smtp.start('thisisunimportant', username, password, "plain") do |sender|                                                                       
        sender.send_message(data, fromAddress, toAddress)
    end

end

send(user, pass, rcpt, "Hey!")

给出一种意想不到的错误:

/usr/lib/ruby/1.9.1/net/smtp.rb:725:in authenticate': wrong number of arguments (3 for 4) (ArgumentError) from /usr/lib/ruby/1.9.1/net/smtp.rb:566:indo_start' 来自 /usr/lib/ruby/1.9.1/net/smtp.rb:531:in start' from gmx_pop.rb:24:insend' 来自 gmx_pop.rb:30:in `'

我试过几次踢我的电脑,但问题仍然存在。

【问题讨论】:

  • 踢电脑总是个好主意!继续这样做!
  • 检查修改后的答案 - 适用于我的 Google 邮件

标签: ruby


【解决方案1】:

这里是 Net::SMTP#start 调用的描述:

http://ruby-doc.org/stdlib-1.9.1/libdoc/net/smtp/rdoc/Net/SMTP.html#method-i-start

该页面提到您只需执行 SMTP.start 即可一次完成所有操作。

看起来您缺少端口参数。尝试使用端口 587 进行安全身份验证,如果这不起作用,请使用端口 25。(查看下面提到的教程)

您的电话应如下所示:

message_body = <<END_OF_EMAIL
From: Your Name <your.name@gmail.com>
To: Other Email <other.email@somewhere.com>
Subject: text message

This is a test message.
END_OF_EMAIL


server = 'smtp.gmail.com'
mail_from_domain = 'gmail.com'
port = 587      # or 25 - double check with your provider
username = 'your.name@gmail.com'
password = 'your_password'

smtp = Net::SMTP.new(server, port)
smtp.enable_starttls_auto
smtp.start(server,username,password, :plain)
smtp.send_message(message_body, fromAddress, toAddress)    # see note below!

重要:

  • 请注意,您需要在 message_body 中添加 To: 、 From: 、 Subject: 标头
  • 您的 SMTP 服务器将添加 Message-Id: 和 Date: 标头

还要检查:


从 Ruby 发送电子邮件的另一种方式:

您可以使用 Rails 中的 ActionMailer gem 从 Ruby 发送电子邮件(不使用 Rails)。 起初这似乎有点矫枉过正,但这样会更容易,因为您不必使用 To: 、 From: 、 Subject: 、 Date: 、 Message-Id: Headers 来格式化邮件正文.

# usage:                                                                                                                                                                                               
#   include Email                                                                                                                                                                                       
#                                                                                                                                                                                                                                            
# TEXT EMAIL :   
#   send_text_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', 'some body text' )                                                               
# HTML EMAIL :
#   send_html_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', '<html><body><h1>some title</h1>some body text</body></html>' )                  


require 'action_mailer'

# ActionMailer::Base.sendmail_settings = {
#   :address => "Localhost",
#   :port => 25, 
#   :domain => "yourdomain.com"
# }

ActionMailer::Base.smtp_settings = {        # if you're using GMail
  :address              => 'smtp.gmail.com',  
  :port                 => 587,  
  :domain               => 'gmail.com',  
  :user_name            => "your-username@gmail.com"
  :password             => "your-password"
  :authentication       => "plain",  
  :enable_starttls_auto => true  
}

class SimpleMailer < ActionMailer::Base

  def simple_email(the_sender, the_recepients, the_subject, the_body , contenttype = nil)
    from the_sender
    recipients the_recepients
    subject the_subject
    content_type     contenttype == 'html' ? 'text/html' : 'text/plain'
    body the_body
  end
end

# see http://guides.rails.info/action_mailer_basics.html                                                                                                                                               
# for explanation of dynamic ActionMailer deliver_* methods.. paragraph 2.2                                                                                                                            

module Email
  # call this with a message body formatted as plain text                                                                                                                                              
  #                                                                                                                                                                                                    
  def send_text_email( sender, recepients, subject, body)
    SimpleMailer.deliver_simple_email( sender , recepients , subject , body)
  end

  # call this with an HTML formatted message body                                                                                                                                                      
  #                                                                                                                                                                                                    
  def send_html_email( sender, recepients, subject, body)
    SimpleMailer.deliver_simple_email( sender , recepients , subject , body, 'html')
  end
endsubject , body, 'html')
  end
end

例如如果您想使用 Gmail 的 SMTP 服务器通过您的 Gmail 帐户发送电子邮件,则上面的代码可以工作。其他 SMTP 服务器可能需要 :port、:authentication 和 :enable_starttls_auto 的其他值,具体取决于 SMTP 服务器设置

【讨论】:

  • 嘿,感谢您提供的所有信息!我尝试了您建议的一切:切换端口,创建一个真实的电子邮件字符串而不是“嘿!”,并尝试在 start() 调用中抛出所有身份验证信息。我仍然得到与之前报告的完全相同的错误:-/
  • 我知道 gem 是如何让它变得更简单的,但请原谅我,这个异常让我很困惑,我想解决它!
【解决方案2】:

试试这个代码

Net::SMTP.smtp.start('my.smtp.host', 25, 'mail.from.domain', username, password, :plain) do |smtp|                                                                       
    smtp.send_message data, fromAddress, toAddress
end

【讨论】:

  • 我在尝试发布的代码之前尝试过这个,它导致了相同的结果。我将 start() 和 auth() 分开,因为它给了我关于 auth 的同样错误,我想隔离它。
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 2017-09-26
  • 2016-11-17
  • 2018-09-28
  • 2010-10-27
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多