SMTP Class

E-mail is still one of the most important and popular technologies used online.

Let's build a small SMTP client using the Net::SMTP class in Ruby. The program p070rubysmtp.rb is the SMTP client and the explanation follows.

Explanation of the p070rubysmtp.rb code:

  • The Net::SMTP library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. For SMTP, I am referring to Request for Comments (RFC's) 1869, 2554, 2821, 2822 and 3207. The Net::SMTP library does NOT provide functions to compose internet mails. You must create them by yourself.
  • The line Net::SMTP.start assumes that you have an email server that you can send messages through. It could be the email server at your company or your internet service provider. You will need the host name and port number which you can usually find in your email client by looking at your account settings for the values stored under outgoing SMTP server. On Windows, I prefer to download and use the Free SMTP Server for testing. Connect your PC to Internet, then start the Free SMTP Server program. The program will try to detect your provider's DNS server. If a DNS server is available, and the standard SMTP port #25 is free the program will become ready to process your mail. If it is not possible to detect provider's DNS server, the program will prompt you to enter any DNS server you know, after that if the server exists the program will become ready to process mail. If the port #25 is busy by another local SMTP server/service, the program will show you an error message. You can specify any DNS server or SMTP port by using the button Options, which is situated on the Toolbar. You should use the word "localhost" as the SMTP server host in your mail program.
  • The Net::SMTP.start method creates a new Net::SMTP object, opens a TCP connection and connects to the server. 'localhost' is the IP address of your SMTP server and the port used is 25.
  • Since we have called with a block, the newly-opened Net::SMTP object is yielded to the block, and automatically closed when the block finishes.
  • In the line smtpclient.send_message(the_email, user_from, user_to) the_email is the message to be sent. Single CR and LF found in the string, are converted to the CRLF pair. You cannot send a binary message with this method. the_email should include both the message headers and body. user_from is a String representing the source mail address. user_to is a String or Array of Strings, representing the destination mail address or addresses.
  • If you have more than one recipient you would like the message to go to, just add them to the list as shown here: user_to = "'batman@world.com', 'robin@world.com'"
  • The way we have used Net::SMTP.start it finishes the SMTP session and closes TCP connection.
  • We have also used begin and rescue for Exception handling.

The above program can be misused and I would strongly advise you not to do the same. Also, I want to make the point that I am writing this article for the purpose of education and awareness. The purpose is NOT to provide someone the information needed to abuse SMTP services.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on rubylearning.com and in the course at rubylearning.org is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.