# Net::POP3 and Firewall

**URL:** <https://www.ruby-forum.com/t/net-pop3-and-firewall/116116>\
**Category:** Ruby\
**Created:** [October 2, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116 "2007-10-02T16:04:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [October 2, 2007, 4:04pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116/1 "2007-10-02T16:04:23Z")

</div>

Hi,  
I’m trying to use Net::POP3 from behind a firewall and I can’t get past  
this error  
C:/ruby/lib/ruby/1.8/net/protocol.rb:206:in `initialize’: Bad file  
descriptor - connect(2) (Errno::EBADF)

I had a similar error when trying to access a url via http but I got  
around it by using a Net::HTTP::Proxy i.e.

host = 'whatever ’  
path = ‘/…/ etc’

proxy\_addr = ‘my.proxy.addr’  
proxy\_port = myProxyPort

response = Net::HTTP::Proxy(proxy\_addr, proxy\_port).get\_response(host,  
path)  
puts response

I’ve been unable to find anything similar for Net::POP3 ie code like  
this

pop = Net::POP3.new(…)

just dies and gives the error at the start of this post.

What do I need to do? Are there some ENV vars that need setting? I’ve  
spent a day Googling etc. but go nowhere!

Thanks

Mike

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [October 2, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116/2 "2007-10-02T19:50:15Z")

</div>

This code woks on my home PC - i.e. no firewall. Does anyone know what’s  
needed for it to run from behind a firewall?  
###############  
require ‘net/pop’

pop = Net::POP3.new(‘[pop.mail.yahoo.co.uk](http://pop.mail.yahoo.co.uk)’, 110, false)  
pop.start(‘myUserName’, ‘myPwd’)  
if pop.mails.empty?  
puts ‘No mail.’  
else  
i = 0  
pop.each\_mail do |m|  
File.open(“inbox/#{i}”, ‘w’) do |f|

```
      f.write m.pop
    end

    i += 1
  end
  puts "#{pop.mails.size} mails popped."
end
pop.finish

```

#################

I’d really appreciate any thoughts or comments.

Thanks

Mike

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [October 2, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116/3 "2007-10-02T20:18:36Z")

</div>

On Wed, Oct 03, 2007 at 02:50:19AM +0900, Mike Ho wrote:

> ```
> else
> puts "#{pop.mails.size} mails popped."
> end
> pop.finish
> 
> ```
> 
> #################
> 
> I’d really appreciate any thoughts or comments.

I don’t know of any programmatic way to do this - have you spoken to  
your  
firewall administrator? He will need to allow POP3 traffic out to the  
Internet (at least from the box hosting your app to the POP3 server it  
is  
trying to contact) and back again.

You could also check out SOCKS, which may enable you to proxy through  
the firewall.

Dan

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [February 20, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116/4 "2009-02-20T14:07:23Z")

</div>

Hello,

I am troubling wit fetching my .xls attachments from my mail.i did  
this with the help if IMAP.but i wan to do the same in using POP3.i got  
the text files with the contents and some of bar codes.but i couldn’t  
get the original attachments from my mail.Plz help me.Thanks in  
advance.my code is.

```
require 'rubygems'
require 'net/pop'

Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
pop = Net::POP3.new('pop.gmail.com', Net::POP3.default_pop3s_port, 

```

false)  
pop.start(‘removed\_email\_address@domain.invalid’, ‘password’)  
if pop.mails.empty?  
puts ‘No mail.’  
else  
i = 0  
pop.each\_mail do |m|  
File.open(“inbox/#{i}”, ‘w’) do |f|  
f.write m.pop  
end  
i += 1  
end  
puts “#{pop.mails.size} mails popped.”  
end  
pop.finish

---

<div class="post-metadata">

**Author:** ![ghoti143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@ghoti143](https://www.ruby-forum.com/u/ghoti143)\
**Post date:** [October 2, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/net-pop3-and-firewall/116116/5 "2007-10-02T21:58:58Z")

</div>

> > ###############  
> > File.open(“inbox/#{i}”, ‘w’) do |f|
> 
> ```
> - against HTML, vCards and X
> - proprietary attachments in 
> 
> ```
> 
> e-mail / \

One faily cheap and easy trick - if the requirements are met - is  
tunneling  
over SSH. If the firewall allows SSH traffic to the internet, and if you  
have a machine outside the firewall that you can SSH to, you can ‘proxy’  
your POP3 like so:

ssh -L5000:ip.of.mail.server:110 removed\_email\_address@domain.invalid

And then connect to localhost:5000 to retrieve mail.

You could probably also utilize net/ssh as a ruby library to build the  
tunnel within the script, though I’ve never done that.

If this is in a corporate (or other network policy) situation, note that  
_any_ circumvention of the firewall will be very, very frowned upon and  
may  
lead to termination - always check with the policy makers first.

HTH,

Felix
