2010-04-03 279 views
1

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

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

编辑10年4月5日:

嗯,这是尴尬。原来简单些,我试图让它比需要的更复杂。虽然我的邮件服务器的SMTP之前需要流行,这发出了OK:

require 'net/smtp' 

message = <<MESSAGE_END 
    From: Private Person <[email protected]> 
    To: A Test User <[email protected]> 
    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, 
      '[email protected]', 
      '[email protected]' 
end 

编辑10年4月4日:

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

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

message = <<MESSAGE_END 
From: Private Person <[email protected]> 
To: A Test User <[email protected]> 
Subject: SMTP e-mail test 

This is a test e-mail message. 
MESSAGE_END 

Net::POP3.start('mail.mydomain.com', 110, '[email protected]', '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', 
       '[email protected]', 'password', :plain) do |smtp| 
    smtp.send_message message, '[email protected]', 
          '[email protected]' 
end 
end 
+0

如果smtp服务器上没有弹出窗口,您为什么需要弹出检查? – 2010-04-03 04:22:29

+0

我的错误;我的意思是说安全的流行...... – markratledge 2010-04-03 17:48:32

+0

我怀疑当执行'pop.finish'时出现500错误的原因是,如果你在上面做的时候将一个块传递给'POP3.start',那么连接会自动在块的末尾关闭你,所以通过放入一个'pop.close'你试图关闭连接两次。 – mikej 2010-04-04 22:47:21

回答

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是。

+0

靠近。通过上面的代码,我得到了一个弹出服务器准备好的200个回复代码。 (显然我只是在学习Ruby ...) – markratledge 2010-04-03 17:52:45

+0

'start'应该连接到服务器并登录。尝试在'POP3.start'之后插入一个'puts pop.n_mails' - 这会打印出数量邮件信箱中的邮件作为您确定与POP服务器交谈的诊断信息。在进行SMTP连接之前,请尝试执行'pop.finish'。如果您真的尝试**通过SMTP发送邮件,现在会发生什么? – mikej 2010-04-03 18:32:55

+0

移动pop.finish没有运气;仍然是500错误。没有首先检查电子邮件,仍然无法通过SMTP发送。但我可以用上面的代码片断登录到pop服务器。 – markratledge 2010-04-04 22:01:56

1

如果这样不能让你的服务器开心,那么Net :: Telnet会让你自己发送原始命令。

+0

我没有telnet访问我的服务器;只有ssh。 – markratledge 2010-04-04 22:02:20

+0

如果您在适当的端口号上打开Telnet连接(POP为110,SMTP为25),则可以发送原始命令。 – mikej 2010-04-04 23:35:42

+0

我再次检查,你都是对的;我确实有远程访问。我认为从这个共享主机上禁用了过去的telnet。最后,为了发送HTML格式的电子邮件,将从Applescript调用Ruby脚本,所以telnet可以方便地测试pop服务器,但telnet可以接收html格式的电子邮件吗? – markratledge 2010-04-05 03:50:05