Newbie question re: 'net'

Hi,

I have the Nutshell book, and I’ve been playing with the Net classes
examples.

The FTP example works like a champ. (A dozen lines to manage a simple FTP
session…who knew :slight_smile:

However, I’m running into a little snag with the POP3 (or, more
specifically, the PopMail) class.

I just want to print the number of unread mails (which works), and the
header for each mail. But when I do this:

require 'net/pop’
mailbox = Net::POP3::new( “pop.west.cox.net” )
mails = []
mailbox.start( “dhtapp”, ) { mails = mailbox.mails }
print "# of mails: ", mails.size, "\n"
mails.each { |mail| p mail.header, “\n” }

I get:

of mails: 1

c:/ruby/lib/ruby/1.8/net/protocol.rb:259:in do_write': undefined methodwrite’
for nil (NoMethodError)
from c:/ruby/lib/ruby/1.8/net/protocol.rb:242:in writeline' from c:/ruby/lib/ruby/1.8/net/protocol.rb:241:inwriting’
from c:/ruby/lib/ruby/1.8/net/protocol.rb:243:in writeline' from c:/ruby/lib/ruby/1.8/net/pop.rb:785:ingetok’
from c:/ruby/lib/ruby/1.8/net/pop.rb:745:in top' from c:/ruby/lib/ruby/1.8/net/pop.rb:744:incritical’
from c:/ruby/lib/ruby/1.8/net/pop.rb:747:in top' from c:/ruby/lib/ruby/1.8/net/pop.rb:657:intop’
from c:/ruby/lib/ruby/1.8/net/pop.rb:661:in header' from trypop.rb:10 from trypop.rb:10:ineach’
from trypop.rb:10

I’ve tried moving the “p mail.header” up as a block to the mailbox.start
call, thinking that maybe the mailbox was closed by the time I tried to
print the headers out, but it made no difference. Can anyone point me to
something obvious?

Thanks,

  • dan

Hi,

In mail “Newbie question re: ‘net’”

require ‘net/pop’
mailbox = Net::POP3::new( “pop.west.cox.net” )
mails =
mailbox.start( “dhtapp”, ) { mails = mailbox.mails }
print "# of mails: ", mails.size, “\n”
mails.each { |mail| p mail.header, “\n” }

This script works for me:

require ‘net/pop’

host = ‘pop.west.cox.net
port = 110
user = ‘dhtapp’
pass = ‘Your Password’

pop = Net::POP3.new(host, port)
pop.start(user, pass) {
puts “# of mails: #{pop.mails.size}”
pop.each_mail do |mail|
puts mail.header
end
}

I’ve tried moving the “p mail.header” up as a block to the mailbox.start
call, thinking that maybe the mailbox was closed by the time I tried to
print the headers out, but it made no difference. Can anyone point me to

Yes, your first error is caused by calling I/O method after closing
POP3 connection. I can’t detect the reason of your second error.

– Minero Aoki

···

“dhtapp” dhtapp@cox.net wrote:

[snip]

require ‘net/pop’
mailbox = Net::POP3::new( “pop.west.cox.net” )
mails =
mailbox.start( “dhtapp”, ) { mails = mailbox.mails }
print "# of mails: ", mails.size, “\n”
mails.each { |mail| p mail.header, “\n” }

[snip]

I’ve tried moving the “p mail.header” up as a block to the mailbox.start
call, thinking that maybe the mailbox was closed by the time I tried to
print the headers out,

Certaily, this is correct. Net::POPMail objects download
the contents of the mail item on demand, using a reference
to the Net::POP3 object that they’re drawn from, so fetching
the headers or body will only work while the POP3 object is
still open (in your example, inside the mailbox.start
block). However…

                   but it made no difference.  Can anyone point me to

something obvious?

I can’t see anything else obviously wrong with your code.
It should look something like this:

require 'net/pop'
mailbox = Net::POP3::new("pop.west.cox.net")
mailbox.start("dhtapp", "mypassword") {
    print "# of mails: ", mailbox.mails.size, "\n"
    mailbox.mails.each { |mail|
        p mail.header, "\n"
    }
}

Does this also not work?

William

···

On Mon, Sep 01, 2003 at 06:27:47PM +0900, dhtapp wrote:

“Minero Aoki” aamine@loveruby.net wrote in message

This script works for me:

require ‘net/pop’

host = ‘pop.west.cox.net
port = 110
user = ‘dhtapp’
pass = ‘Your Password’

pop = Net::POP3.new(host, port)
pop.start(user, pass) {
puts “# of mails: #{pop.mails.size}”
pop.each_mail do |mail|
puts mail.header
end
}

Pardon me for piggy-backing on this thread. I have been
wanting to ask this for a while but never really got around to it:

How do you get (and save) attachments using this library?

Thanks,
– shanko

Thanks for the help; that confirms my suspicions.

As to why I got an error even when I moved the print into the block, I’ll
chalk that up to a typo on my part, plus hastily mis-reporting to the group.
(It must’ve been a different error, which I neglected in my fatigue to
notice. I should’ve double-checked it before posting on it.)

– dan

How do you get (and save) attachments using this library?

You read the message, and then you use a module which can extract
attachments (raa : in the category Library/Mail)

Guy Decoux

“ts” decoux@moulon.inra.fr wrote in message

How do you get (and save) attachments using this library?

You read the message, and then you use a module which can extract
attachments (raa : in the category Library/Mail)

Guy Decoux

I will give it a whirl.
Thanks Guy !