开发者

Ruby send mail with smtp

开发者 https://www.devze.com 2022-12-25 06:42 出处:网络
I\'m trying to send simple email via Ruby (no rails) on OS X, with XCode (which installs Ruby.) But I\'m running into a problem with my smtp server which requires the email client to check mail before

I'm trying to send simple email via Ruby (no rails) on OS X, with XCode (which installs Ruby.) But I'm running into a problem with my smtp server which requires the email client to check mail before sending as a form of authentication.

How can I get Ruby to authenticate with the smtp server in a "POP" fashion before I can send mail? Not download mail; I only want to send html formatted email (eventually via Applescript calling Ruby, because Applescript doesn't support smtp), but the server requires that I check mail before I send.

Edit 4/05/10:

Well, that's embarrasing. Turned out to be simpler; I was trying to make it more complex than it needed to be. Even though my mail server requires pop before smtp, this sends 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

Edit 4/04/10:

With this I get a 500 unrecognized command error; the pop server is responding, though.

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@m开发者_如何学Pythonydomain.com'
end
end


POP before SMTP isn't one of the authentication types supported by Net::SMTP so I think you're going to have to use Net::POP3 to do your POP3 login e.g.

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

Net::POP3 is in the Standard Library so should be available anywhere that Net::SMTP is.


If that doesn't make your server happy then Net::Telnet will let you send the raw commands yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消