class Net::POP3


String. Normally the unique-id is a hash of the message.
The POPMail#unique_id() method returns the unique-id of the message as a
end
end
do_something(m.pop)
pop.mails.select { |m| need_pop?(m.unique_id) }.each do |m|
‘Your account’, ‘Your password’) do |pop|
Net::POP3.start(‘pop.example.com’, 110,
end
# determine if we need pop this mail…
def need_pop?( id )
e.g.
you can grab only selected mails from the POP server.
If your POP server provides UIDL functionality,
=== Fetch Only Selected Mail Using ‘UIDL’ POP Command
end
# Rest of the code is the same.
pop.start(‘YourAccount’, ‘YourPassword’) do |pop|
pop = Net::POP3.APOP($isapop).new(‘apop.example.com’, 110)
# Use APOP authentication if $isapop == true
require ‘net/pop’
You can use the utility method, Net::POP3.APOP(). For example:
To use APOP, use the Net::APOP class instead of the Net::POP3 class.
The net/pop library supports APOP authentication.
=== Using APOP
end
end
i += 1
end
f.write chunk
m.pop do |chunk| # get a message little by little.
File.open(“inbox/#{i}”, ‘w’) do |f|
‘YourAccount’, ‘YourPassword’) do |m|
Net::POP3.delete_all(‘pop.example.com’, 110,
i = 1
require ‘net/pop’
This example avoids this.
All the examples above get each message as one big string.
=== Memory Space Issues
end
i += 1
end
f.write m.pop
File.open(“inbox/#{i}”, ‘w’) do |f|
‘YourAccount’, ‘YourPassword’) do |m|
Net::POP3.delete_all(‘pop.example.com’, 110,
i = 0
require ‘net/pop’
And here is an even shorter example.
end
end
end
i += 1
end
f.write m.pop
File.open(“inbox/#{i}”, ‘w’) do |f|
pop.delete_all do |m|
i = 1
else
puts ‘No mail.’
if pop.mails.empty?
‘YourAccount’, ‘YourPassword’) do |pop|
Net::POP3.start(‘pop.example.com’, 110,
require ‘net/pop’
POP3#delete_all is an alternative for #each_mail and #delete.
end
end
puts “#{pop.mails.size} mails popped.”
end
i += 1
m.delete
end
f.write m.pop
File.open(“inbox/#{i}”, ‘w’) do |f|
pop.each_mail do |m| # or “pop.mails.each …”
i = 0
else
puts ‘No mail.’
if pop.mails.empty?
‘YourAccount’, ‘YourPassword’) do |pop|
Net::POP3.start(‘pop.example.com’, 110,
require ‘net/pop’
be used instead of POP3.new, POP3#start and POP3#finish.
some utility methods. First, the block form of Net::POP3.start can
The example above is very verbose. You can shorten the code by using
=== Shortened Code
3. Close POP session by calling POP3#finish or use the block form of #start.
2. Access messages by using POP3#each_mail and/or POP3#mails.
1. Call Net::POP3#start and start POP session.
pop.finish # (3)
end
puts “#{pop.mails.size} mails popped.”
end
i += 1
m.delete
end
f.write m.pop
File.open(“inbox/#{i}”, ‘w’) do |f|
pop.each_mail do |m| # or “pop.mails.each …” # (2)
i = 0
else
puts ‘No mail.’
if pop.mails.empty?
pop.start(‘YourAccount’, ‘YourPassword’) # (1)
pop = Net::POP3.new(‘pop.example.com’)
require ‘net/pop’
details.
‘YourAccount’ and ‘YourPassword’ with the appropriate account
Replace ‘pop.example.com’ with your POP3 server address, and
Messages are written to files named ‘inbox/1’, ‘inbox/2’, .…
on the server.
This example retrieves messages from the server and deletes them
=== Retrieving Messages
== Examples
of POP3, see [RFC1939] (www.ietf.org/rfc/rfc1939.txt).
email via POP3, the Post Office Protocol version 3. For details
This library provides functionality for retrieving
== What is This Library?

def apop?

Does this instance use APOP authentication?
def apop?
  @apop
end

def auth_only(account, password)

This method raises POPAuthenticationError if authentication fails.
This method must not be called while POP3 session is opened.
Starts a pop3 session, attempts authentication, and quits.
def auth_only(account, password)
  raise IOError, 'opening previously opened POP session' if started?
  start(account, password) {
    ;
  }
end

def command # :nodoc:

:nodoc:
Raises IOError if there is no active socket

Returns the current command.
def command # :nodoc:
  raise IOError, 'POP session not opened yet' \
                                  if not @socket or @socket.closed?
  @command
end

def delete_all # :yield: message

:yield: message

This method raises a POPError if an error occurs.

end
n += 1
end
f.write m.pop
File.open("inbox/#{n}") do |f|
pop.delete_all do |m|
n = 1

=== Example

If called with a block, yields each message in turn before deleting it.

Deletes all messages on the server.
def delete_all # :yield: message
  mails().each do |m|
    yield m if block_given?
    m.delete unless m.deleted?
  end
end

def disable_ssl

Disable SSL for all new instances.
def disable_ssl
  @ssl_params = nil
end

def do_finish # :nodoc:

:nodoc:
- quits the current command, if any
- number counter for bytes
- number counter for mails
- mails
nil's out the:
def do_finish # :nodoc:
  @mails = nil
  @n_mails = nil
  @n_bytes = nil
  @command.quit if @command
ensure
  @started = false
  @command = nil
  @socket.close if @socket
  @socket = nil
end

def do_start(account, password) # :nodoc:

:nodoc:
internal method for Net::POP3.start
def do_start(account, password) # :nodoc:
  begin
    s = Socket.tcp @address, port, nil, nil, connect_timeout: @open_timeout
  rescue Errno::ETIMEDOUT #raise Net:OpenTimeout instead for compatibility with previous versions
    raise Net::OpenTimeout, "Timeout to open TCP connection to "\
        "#{@address}:#{port} (exceeds #{@open_timeout} seconds)"
  end
  if use_ssl?
    raise 'openssl library not installed' unless defined?(OpenSSL)
    context = OpenSSL::SSL::SSLContext.new
    context.set_params(@ssl_params)
    s = OpenSSL::SSL::SSLSocket.new(s, context)
    s.hostname = @address
    s.sync_close = true
    ssl_socket_connect(s, @open_timeout)
    if context.verify_mode != OpenSSL::SSL::VERIFY_NONE
      s.post_connection_check(@address)
    end
  end
  @socket = InternetMessageIO.new(s,
                                  read_timeout: @read_timeout,
                                  debug_output: @debug_output)
  logging "POP session started: #{@address}:#{@port} (#{@apop ? 'APOP' : 'POP'})"
  on_connect
  @command = POP3Command.new(@socket)
  if apop?
    @command.apop account, password
  else
    @command.auth account, password
  end
  @started = true
ensure
  # Authentication failed, clean up connection.
  unless @started
    s.close if s
    @socket = nil
    @command = nil
  end
end

def each_mail(&block) # :yield: message

:yield: message
This method raises a POPError if an error occurs.

end
....
pop3.mails.each do |popmail|

Equivalent to:
Yields each message to the passed-in block in turn.
def each_mail(&block)  # :yield: message
  mails().each(&block)
end

def enable_ssl(verify_or_params = {}, certs = nil, port = nil)

+params+ (except :port) is passed to OpenSSL::SSLContext#set_params.
+params[:port]+ is port to establish the SSL connection on; Defaults to 995.
established to have any effect.
Enables SSL for this instance. Must be called before the connection is

Net::POP#enable_ssl(params = {})
:call-seq:
def enable_ssl(verify_or_params = {}, certs = nil, port = nil)
  begin
    @ssl_params = verify_or_params.to_hash.dup
    @port = @ssl_params.delete(:port) || @port
  rescue NoMethodError
    @ssl_params = POP3.create_ssl_params(verify_or_params, certs)
    @port = port || @port
  end
end

def finish

Finishes a POP3 session and closes TCP connection.
def finish
  raise IOError, 'POP session not yet started' unless started?
  do_finish
end

def initialize(addr, port = nil, isapop = false)

This method does *not* open the TCP connection.

to use APOP authentication; it defaults to +false+.
The optional +isapop+ specifies whether this connection is going

The optional +port+ is the port to connect to.

+address+ is the hostname or ip address of your POP3 server.

Creates a new POP3 object.
def initialize(addr, port = nil, isapop = false)
  @address = addr
  @ssl_params = POP3.ssl_params
  @port = port
  @apop = isapop
  @command = nil
  @socket = nil
  @started = false
  @open_timeout = 30
  @read_timeout = 60
  @debug_output = nil
  @mails = nil
  @n_mails = nil
  @n_bytes = nil
end

def inspect

Provide human-readable stringification of class state.
def inspect
  +"#<#{self.class} #{@address}:#{@port} open=#{@started}>"
end

def logging(msg)

debugging output for +msg+
def logging(msg)
  @debug_output << msg + "\n" if @debug_output
end

def mails

This method raises a POPError if an error occurs.

this method is called (directly or indirectly) and cached.
restarts; otherwise, it is fetched from the server the first time
messages on the server. This array is renewed when the session
Returns an array of Net::POPMail objects, representing all the
def mails
  return @mails.dup if @mails
  if n_mails() == 0
    # some popd raises error for LIST on the empty mailbox.
    @mails = []
    return []
  end
  @mails = command().list.map {|num, size|
    POPMail.new(num, size, self, command())
  }
  @mails.dup
end

def n_bytes

Returns the total size in bytes of all the messages on the POP server.
def n_bytes
  return @n_bytes if @n_bytes
  @n_mails, @n_bytes = command().stat
  @n_bytes
end

def n_mails

Returns the number of messages on the POP server.
def n_mails
  return @n_mails if @n_mails
  @n_mails, @n_bytes = command().stat
  @n_mails
end

def on_connect # :nodoc:

:nodoc:
Does nothing
def on_connect # :nodoc:
end

def port

The port number to connect to.
def port
  return @port || (use_ssl? ? POP3.default_pop3s_port : POP3.default_pop3_port)
end

def read_timeout=(sec)

Set the read timeout.
def read_timeout=(sec)
  @command.socket.read_timeout = sec if @command
  @read_timeout = sec
end

def reset

This method raises a POPError if an error occurs.

Resets the session. This clears all "deleted" marks from messages.
def reset
  command().rset
  mails().each do |m|
    m.instance_eval {
      @deleted = false
    }
  end
end

def set_all_uids #:nodoc: internal use only (called from POPMail#uidl)

:nodoc: internal use only (called from POPMail#uidl)
def set_all_uids   #:nodoc: internal use only (called from POPMail#uidl)
  uidl = command().uidl
  @mails.each {|m| m.uid = uidl[m.number] }
end

def set_debug_output(arg)


end
....
pop.start(account, passwd) do |pop|
pop.set_debug_output $stderr
pop = Net::POP.new(addr, port)

=== Example

Set an output stream for debugging.

Use this method only for debugging.
*WARNING*: This method causes a serious security hole.
def set_debug_output(arg)
  @debug_output = arg
end

def start(account, password) # :yield: pop

:yield: pop
This method raises a POPAuthenticationError if authentication fails.

closes the session after block call finishes.
When called with block, gives a POP3 object to the block and

Starts a POP3 session.
def start(account, password) # :yield: pop
  raise IOError, 'POP session already started' if @started
  if block_given?
    begin
      do_start account, password
      return yield(self)
    ensure
      do_finish
    end
  else
    do_start account, password
    return self
  end
end

def started?

+true+ if the POP3 session has started.
def started?
  @started
end

def use_ssl?

does this instance use SSL?
def use_ssl?
  return !@ssl_params.nil?
end