尝试在Ruby中验证SMTP
我查看了所有SMTP ruby文档,不知道哪里出了问题:尝试在Ruby中验证SMTP,ruby,Ruby,我查看了所有SMTP ruby文档,不知道哪里出了问题: def send(username, password, data, toAddress, fromAddress) smtp = Net::SMTP.new('my.smtp.host', 25) smtp.start('thisisunimportant', username, password, "plain") do |sender|
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:inauthenticate”:参数数量错误(3代表4)(ArgumentError)
from/usr/lib/ruby/1.9.1/net/smtp.rb:566:in
do_start'
从/usr/lib/ruby/1.9.1/net/smtp.rb:531:instart'
从gmx_pop.rb:24:in
send'
来自gmx_pop.rb:30:in`'
我试过几次踢我的电脑,但问题仍然存在。试试这段代码
Net::SMTP.smtp.start('my.smtp.host', 25, 'mail.from.domain', username, password, :plain) do |smtp|
smtp.send_message data, fromAddress, toAddress
end
下面是对Net::SMTP#启动调用的描述: 该页面提到您可以只执行SMTP。立即开始执行所有操作 看起来您缺少端口参数。尝试使用端口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!
e、 g.如果您想使用Gmail的SMTP服务器通过您的Gmail帐户发送电子邮件,则上述代码有效。。其他SMTP服务器可能需要其他值:port、:authentication和:enable_starttls_auto,具体取决于SMTP服务器设置踢计算机始终是一个好主意!继续做下去!检查修改后的答案-谷歌邮箱对我有用嘿,谢谢你提供的所有信息!我尝试了你建议的一切:切换端口,创建一个真正的电子邮件字符串而不是“嘿!”,并尝试在start()调用中抛出所有身份验证信息。我仍然得到与我之前报告的完全相同的错误:-/我知道gem如何使它更简单,但是请原谅,这个异常正在窃听我的积垢,我想解决它!在我尝试发布代码之前,我尝试了这个,结果也是一样的。我将start()和auth()分开,因为它给了我关于auth的相同错误,我想将其隔离。
# 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