gpt4 book ai didi

ruby - 尝试在 Ruby 中使用经过身份验证的 SMTP

转载 作者:数据小太阳 更新时间:2023-10-29 08:28:50 25 4
gpt4 key购买 nike

我查看了所有 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': 参数数量错误(3 对 4)(ArgumentError)
来自/usr/lib/ruby/1.9.1/net/smtp.rb:566:in
do_start' 来自/usr/lib/ruby/1.9.1/net/smtp.rb:531:in start'
从 gmx_pop.rb:24:in
发送' 来自 gmx_pop.rb:30:in `'

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

最佳答案

下面是 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!

重要:

  • 请注意,您需要将 To: 、 From: 、 Subject: header 添加到您的邮件正文中!
  • Message-Id: 和 Date: header 将由您的 SMTP 服务器添加

同时检查:


另一种从 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 服务器设置

关于ruby - 尝试在 Ruby 中使用经过身份验证的 SMTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com