(Ruby)让 Net::SMTP 与 Gmail 一起工作......? [英] (Ruby) Getting Net::SMTP working with Gmail...?
问题描述
有没有人有关于使用 Ruby 的 Net::SMTP 通过 Gmail 发送邮件的任何质量(和最新)信息?我见过几个例子——大多数可以追溯到 2007 年到 2008 年年中,但没有一个对我有用.我需要更多使用最新 1.8.7 版本的当前示例.如果文档不仅涵盖没有人真正使用过的简单示例,我也将不胜感激.
Does anyone have any quality (and up-to-date) information regarding sending mail via Gmail using Ruby's Net::SMTP? I've seen several examples -- most dating from 2007 to mid-2008 and none of them work for me. I need more current examples that use the most recent 1.8.7 release. I'd also appreciate if the documentation didn't only cover simple examples that no one ever really uses.
目前我收到一个错误:
SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol
关于 SMTP 协议,我对 SSL 不是很熟悉,所以我完全不知道这些.不幸的是,Net::SMTP 文档只涵盖了基础,也没有提供各种潜在 OpenSSL::SSL 上下文的完整列表,因此我无法尝试各种不同的上下文.
I'm not terribly familiar with SSL as regards the SMTP protocol, so this is all lost on me. Unfortunately the Net::SMTP documentation only covers the bases and doesn't provide a full list of the various potential OpenSSL::SSL contexts either so I can't try various ones.
无论如何,如果有人有任何关于让它与 Gmail 一起使用的质量信息,我们将不胜感激.
Anyway, if anyone has any quality info on getting this to work with Gmail it would be most appreciated.
最好的.
推荐答案
我实际上刚刚开始工作.写了一个快速脚本来测试它.
I actually just got this working. Wrote a quick script to test it.
我遇到了与您不同的错误(需要 STARTTLS),我还发现我必须使用端口 587 而不是 465.
I was getting a different error than you were (requiring STARTTLS), I also found I had to use port 587 instead of 465.
我找到了让它在我找到的 Rails 插件中工作的技巧.(agilewebdevelopment.com/plugins/net_smtp_tls_support)
I found the trick to get it working in a Rails plugin I found. (agilewebdevelopment.com/plugins/net_smtp_tls_support)
如果你'评估'这个文件(它向标准 Net::SMTP 库添加了 tls 支持):
if you 'eval' this file (it adds tls support to the standard Net::SMTP library):
然后运行'Net::SMTP.enable_tls()'
then run 'Net::SMTP.enable_tls()'
似乎一切正常.
这是我的代码:
require 'rubygems'
require 'net/smtp'
eval File.read("smtp_tls.rb")
Net::SMTP.enable_tls()
FROM_EMAIL = "REMOVED"
PASSWORD = "REMOVED"
TO_EMAIL = "REMOVED"
msgstr = <<END_OF_MESSAGE
From: Your Name <#{FROM_EMAIL}>
To: my phone <#{TO_EMAIL}>
Subject: text message
Date: Sat, 23 Jun 2001 16:26:43 +0900
Message-Id: <unique.message.id.string@example.com>
This is a test message.
END_OF_MESSAGE
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
FROM_EMAIL, PASSWORD, :plain) do |smtp|
smtp.send_message msgstr, FROM_EMAIL, TO_EMAIL
end
显然,我将上述文件下载到同一目录并将其命名为smtp_tls.rb"
obviously, i downloaded the above mentioned file to the same directory and named it 'smtp_tls.rb'
希望这有帮助!
这篇关于(Ruby)让 Net::SMTP 与 Gmail 一起工作......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!