2011-11-09 67 views
2

我看了所有的SMTP红宝石文档并不能找出我要去哪里错了身份验证的SMTP:尝试在红宝石

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:in do_start' from /usr/lib/ruby/1.9.1/net/smtp.rb:531:in start' from gmx_pop.rb:24:in send' 从gmx_pop.rb:30:在''

我试过踢我的电脑几次b问题依然存在。

+1

踢电脑总是一个好主意!继续这样做! – Tilo

+0

检查修改后的答案 - 适用于我的Google邮件 – Tilo

回答

5

这里的网:: SMTP#开始通话的描述:

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 <[email protected]> 
To: Other Email <[email protected]> 
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 = '[email protected]' 
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! 

重要:


另一种方式在Ruby中发送电子邮件:

可以使用从导轨的ActionMailer宝石从红宝石发送电子邮件(不包括滑轨)。 起初这看起来像是矫枉过正,但它使得它更容易,因为你不必格式化邮件正文To:,From:,Subject:,Date:,Message-Id:Headers。

# usage:                                                
# include Email                                              
#                                                            
# TEXT EMAIL : 
# send_text_email('[email protected]', '[email protected],[email protected]', 'test subject', 'some body text')                
# HTML EMAIL : 
# send_html_email('[email protected]', '[email protected],[email protected]', '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   => "[email protected]" 
    :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服务器设置

+0

嘿,感谢所有的信息!我尝试了你所建议的一切:切换端口,创建一个真正的电子邮件字符串而不是“嗨!”,并尝试在start()调用中抛出所有auth信息。我仍然得到与之前报道的完全相同的错误: -/ –

+0

我看到了宝石如何使它变得更简单,但请原谅我,这个例外是纠缠在我身后,我想解决它! –

+0

见上面的编辑版本..这绝对适合我 – Tilo

0

试试这个代码

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

我在尝试发布的代码之前尝试了此操作,并且导致了相同的结果。我把start()和auth()分开了,因为它给了我同样的关于auth的错误,我想把它隔离开来。 –