Net::NNTP Client Library

Get Version


→ ‘nntp’


This gem allows you to to retrieve and post Usenet news articles via NNTP, the Network News Transfer Protocol. For details of NNTP itself, see RFC977.

This library does NOT provide functions to compose Usenet news articles. You must create and format them yourself per Standard for Interchange of Usenet Messages. See RFC850, RFC2047.


sudo gem install nntp

Alternatively, you may download the gem from the project info page and install manually.

The basics

Posting Messages

You must open a connection to a NNTP server before posting messages. The first argument is the address of your NNTP server, and the second argument is the port number. Using NNTP.start with a block is the simplest way to do this because the NNTP connection closes automatically after the block executes.

  require 'rubygems'
  require 'nntp'
  Net::NNTP.start('your.nntp.server', 119) do |nntp|
    # Use the NNTP object nntp only in this block.

Replace ‘your.nntp.server’ with your NNTP server. Normally your systems administrator or internet service provider supplies a server for you.

Then you can post messages.

  require 'date'
  date ='%a, %d %b %Y %T %z')

  msgstr = <<END_OF_MESSAGE
  From: Your Name <your@mail.address>
  Newsgroups:, ...
  Subject: test message
  Date: #{date}

  This is a test message.

  require 'rubygems'
  require 'nntp'
  Net::NNTP.start('your.nntp.server', 119) do |nntp| msgstr

NOTE: The NNTP message headers such as Date:, Message-ID:, and Path:, if ommited, may automatically be added by your Usenet news server; however, it is best not to rely on this behavior.

Reading Messages

This snippet will display all subject lines within a particular news group.

  require 'rubygems'
  require 'nntp'

  newsgroup = 'comp.lang.ruby'

  Net::NNTP.start('your.nntp.server', 119) do |nntp|
    message_ids = nntp.listgroup(newsgroup)
    message_ids[1].each do |id|
      nntp.head(id).each do |header|
        header.each { |line| puts(line) if line.index('Subject:') }

Closing the Session

You MUST close the NNTP session after posting messages by calling the Net::NNTP#finish method:

  # using NNTP#finish
  nntp = Net::NNTP.start('your.nntp.server', 119) msgstr

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

  # using block form of NNTP.start
  Net::NNTP.start('your.nntp.server', 119) do |nntp| msgstr

We strongly recommend the latter scheme since it is simpler and more robust.

NNTP Authentication

The Net::NNTP class may support various authentication schemes depending on your news server‘s reponse to CAPABILITIES command. To use NNTP authentication, pass extra arguments to NNTP.start/NNTP#start.

See NNTP Extension for Authentication.

  Net::NNTP.start('your.nntp.server', 119,
                  'YourAccountName', 'YourPassword', :method)

Where :method can be one of ‘gassapi’, ‘digest_md5’, ‘cram_md5’, ‘starttls’, ‘external’, ‘plain’, ‘generic’, ‘simple’ or ‘original’.

In the case of method :generic, arguments should be passed to a format string as follows:

  Net::NNTP.start('your.nntp.server', 119,
                  "format", *arguments, :generic)

NOTE: With the exception of :generic, the authentication mechanism will fallback to a less secure scheme if your Usenet server does not support the method you selected.

How to Submit Patches


This code is free to use under the terms of version 2.1 of the GNU Lesser General Public License.


Any contribution of yours, however small it may be, will highly be appreciated and used for further development of this and other F/LOSS (Free/Libre Open Source Software) Projects.


In addition to this page, you can also view the automatically generated RDocs in the “DocManager: Project Documentation” section of the project info page. If you elect to install the RI documentation when you install the gem, you can afterwards issue the following command for the API reference:

ri Net::NNTP

Similar Gems

Anton Bangratz’s ruby-net-nntp


Where to get help

If you need assistance with this gem, visit the project info page to file a bug, make a support request, or suggest a new feature.

Albert Vernon, 2nd January 2008
Theme extended from Paul Battley