代码之家  ›  专栏  ›  技术社区  ›  markratledge

ruby用smtp发送邮件

  •  1
  • markratledge  · 技术社区  · 15 年前

    我试着用xcode(安装了ruby)通过os x上的ruby(没有rails)发送简单的电子邮件,但是我的smtp服务器遇到了一个问题,它要求电子邮件客户端在以身份验证的形式发送邮件之前检查邮件。

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

    编辑4/05/10:

    好吧,那太尴尬了。结果变得更简单了;我试图让事情变得比需要的更复杂。即使我的邮件服务器在SMTP之前需要POP,这也会发送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 回复  |  直到 15 年前
        1
  •  2
  •   mikej heading_to_tahiti    15 年前

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

        2
  •  1
  •   Azeem.Butt    15 年前

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