Ruby用smtp发送邮件

时间:2010-04-03 03:50:21

标签: ruby xcode macos smtp

我正在尝试通过Ruby(无轨道)在OS X上发送简单的电子邮件,使用XCode(安装Ruby)。但是我的smtp服务器遇到问题,需要电子邮件客户端检查邮件之前作为一种身份验证形式发送。

在发送邮件之前,如何让Ruby以“POP”方式使用smtp服务器进行身份验证?不下载邮件;我只想发送html格式的电子邮件(最终通过Applescript调用Ruby,因为Applescript不支持smtp),但服务器要求我在发送之前检查邮件。

编辑4/05/10:

嗯,这很尴尬。变得更简单;我试图让它变得比它需要的更复杂。即使我的邮件服务器需要在smtp之前弹出,这也会发送OK:

require 'net/smtp'

message = <<MESSAGE_END
    From: Private Person <me@fromdomain.com>
    To: A Test User <test@todomain.com>
    Subject: SMTP e-mail test

    This is a test e-mail message.
    MESSAGE_END

Net::SMTP.start('mail.mydomain.com', 25) do |smtp|
smtp.send_message message,
            'mark@mydomain.com',
            'mark@mydomain.com'
end

编辑4/04/10:

有了这个,我得到一个500无法识别的命令错误;但是,pop服务器正在响应。

require 'net/smtp'
require 'net/pop'

message = <<MESSAGE_END
From: Private Person <me@fromdomain.com>
To: A Test User <test@todomain.com>
Subject: SMTP e-mail test

This is a test e-mail message.
MESSAGE_END

Net::POP3.start('mail.mydomain.com', 110, 'mark@mydomain.com', 'password') do |pop|

// If this line is included,
// I get a printout of the number
// of emails on the server
// right before the error:
//
// puts pop.n_mails  end

Net::SMTP.start('mail.markratledge.com', 
                25, 
                'localhost', 
                'mark@mydomain.com', 'password', :plain) do |smtp|
  smtp.send_message message, 'mark@mydomain.com', 
                             'mark@mydomain.com'
end
end

2 个答案:

答案 0 :(得分:2)

POP before SMTP不是Net::SMTP支持的身份验证类型之一,因此我认为您必须使用Net::POP3进行POP3登录,例如

require 'net/pop'
pop = Net::POP3.start(addr, port, account, password)
pop.finish

Net::POP3位于标准库中,因此应该可以在Net::SMTP所在的任何位置使用。

答案 1 :(得分:1)

如果这不能使您的服务器满意,那么Net :: Telnet将允许您自己发送原始命令。