Type:
Class
Constants:
Revision : %q$Revision$.split[1]
DEFAULT_AUTH_TYPE : :plain

Authentication

IMASK : 0x36
OMASK : 0x5c
CRAM_BUFSIZE : 64

What is This Library?

This library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. For details of SMTP itself, see [RFC2821] (www.ietf.org/rfc/rfc2821.txt).

What is This Library NOT?

This library does NOT provide functions to compose internet mails. You must create them by yourself. If you want better mail support, try RubyMail or TMail or search for alternatives in RubyGems.org or The Ruby Toolbox.

FYI: the official documentation on internet mail is: [RFC2822] (www.ietf.org/rfc/rfc2822.txt).

Examples

Sending Messages

You must open a connection to an SMTP server before sending messages. The first argument is the address of your SMTP server, and the second argument is the port number. Using ::start with a block is the simplest way to do this. This way, the SMTP connection is closed automatically after the block is executed.

require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  # Use the SMTP object smtp only in this block.
end

Replace 'your.smtp.server' with your SMTP server. Normally your system manager or internet provider supplies a server for you.

Then you can send messages.

msgstr = <<END_OF_MESSAGE
From: Your Name <your@mail.address>
To: Destination Address <someone@example.com>
Subject: test 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

require 'net/smtp'
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message msgstr,
                    'your@mail.address',
                    'his_address@example.com'
end

Closing the Session

You MUST close the SMTP session after sending messages, by calling the finish method:

# using SMTP#finish
smtp = Net::SMTP.start('your.smtp.server', 25)
smtp.send_message msgstr, 'from@address', 'to@address'
smtp.finish

You can also use the block form of ::start/SMTP#start. This closes the SMTP session automatically:

# using block form of SMTP.start
Net::SMTP.start('your.smtp.server', 25) do |smtp|
  smtp.send_message msgstr, 'from@address', 'to@address'
end

I strongly recommend this scheme. This form is simpler and more robust.

HELO domain

In almost all situations, you must provide a third argument to ::start/SMTP#start. This is the domain name which you are on (the host to send mail from). It is called the “HELO domain”. The SMTP server will judge whether it should send or reject the SMTP session by inspecting the HELO domain.

Net::SMTP.start('your.smtp.server', 25,
                'mail.from.domain') { |smtp| ... }

SMTP Authentication

The Net::SMTP class supports three authentication schemes; PLAIN, LOGIN and CRAM MD5. (SMTP Authentication: [RFC2554]) To use SMTP authentication, pass extra arguments to ::start/SMTP#start.

# PLAIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :plain)
# LOGIN
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :login)

# CRAM MD5
Net::SMTP.start('your.smtp.server', 25, 'mail.from.domain',
                'Your Account', 'Your Password', :cram_md5)
default_port
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

default_port() Class Public methods The default

2025-01-10 15:47:30
capable_starttls?
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

capable_starttls?() Instance Public methods true if server advertises STARTTLS

2025-01-10 15:47:30
ready
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

ready(from_addr, *to_addrs) Instance Public methods Alias for:

2025-01-10 15:47:30
enable_ssl
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

enable_ssl(context = SMTP.default_ssl_context) Instance Public methods Alias

2025-01-10 15:47:30
quit
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

quit() Instance Public methods

2025-01-10 15:47:30
rcptto_list
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

rcptto_list(to_addrs) Instance Public methods

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP/Net::SMTP::Response

new(status, string) Class Public methods Creates a new instance of the

2025-01-10 15:47:30
capable_cram_md5_auth?
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

capable_cram_md5_auth?() Instance Public methods true if server advertises AUTH

2025-01-10 15:47:30
message
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP/Net::SMTP::Response

message() Instance Public methods The first line of the human readable reply

2025-01-10 15:47:30
default_ssl_port
  • References/Ruby on Rails/Ruby/Classes/Net/Net::SMTP

default_ssl_port() Class Public methods Alias for:

2025-01-10 15:47:30