1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
* lib/net/protocol.rb: calls on_connect before conn_command


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1891 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2001-12-09 08:58:30 +00:00
parent 772678cb7a
commit 9fff32fd7b
8 changed files with 302 additions and 192 deletions

View file

@ -1,3 +1,7 @@
Sun Dec 9 18:06:05 2001 Minero Aoki <aamine@loveruby.net>
* lib/net/protocol.rb: calls on_connect before conn_command
Fri Dec 7 19:20:44 2001 Minero Aoki <aamine@loveruby.net> Fri Dec 7 19:20:44 2001 Minero Aoki <aamine@loveruby.net>
* lib/net/smtp.rb: uses Digest::MD5 instead of MD5 (again). * lib/net/smtp.rb: uses Digest::MD5 instead of MD5 (again).

View file

@ -27,8 +27,8 @@
require 'net/http' require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http| Net::HTTP.start( 'some.www.server', 80 ) {|http|
response , = http.post( '/cgi-bin/any.rhtml', response , = http.post( '/cgi-bin/any.rhtml',
'querytype=subject&target=ruby' ) 'querytype=subject&target=ruby' )
} }
=== プロクシ経由のアクセス === プロクシ経由のアクセス
@ -78,9 +78,9 @@ Net::HTTP
require 'net/http' require 'net/http'
Net::HTTP.start( 'auth.some.domain' ) {|http| Net::HTTP.start( 'auth.some.domain' ) {|http|
response , = http.get( '/need-auth.cgi', response , = http.get( '/need-auth.cgi',
'Authentication' => ["#{account}:#{password}"].pack('m').strip ) 'Authentication' => ["#{account}:#{password}"].pack('m').strip )
print response.body print response.body
} }
バージョン 1.2 (Ruby 1.7 以降に添付) では次のように書けます。 バージョン 1.2 (Ruby 1.7 以降に添付) では次のように書けます。
@ -90,8 +90,8 @@ Net::HTTP
req = Net::HTTP::Get.new('/need-auth.cgi') req = Net::HTTP::Get.new('/need-auth.cgi')
req.basic_auth 'account', 'password' req.basic_auth 'account', 'password'
Net::HTTP.start( 'auth.some.domain' ) {|http| Net::HTTP.start( 'auth.some.domain' ) {|http|
response = http.request( req ) response = http.request(req)
print response.body print response.body
} }
== 新しい仕様への変更と移行措置について == 新しい仕様への変更と移行措置について
@ -148,11 +148,11 @@ Ruby 1.6
# example # example
proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 ) proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
: :
proxy_class.start( 'www.ruby-lang.org' ) do |http| proxy_class.start( 'www.ruby-lang.org' ) {|http|
# connecting proxy.foo.org:8080 # connecting proxy.foo.org:8080
: :
end }
: proxy_class? : proxy_class?
自身が (Proxy メソッドによって作成された) プロクシ用のクラスならば真。 自身が (Proxy メソッドによって作成された) プロクシ用のクラスならば真。
@ -233,13 +233,13 @@ Ruby 1.6
# using block # using block
File.open( 'save.txt', 'w' ) {|f| File.open( 'save.txt', 'w' ) {|f|
http.get( '/~foo/', nil ) do |str| http.get( '/~foo/', nil ) do |str|
f.write str f.write str
end end
} }
# same effect # same effect
File.open( 'save.txt', 'w' ) {|f| File.open( 'save.txt', 'w' ) {|f|
http.get '/~foo/', nil, f http.get '/~foo/', nil, f
} }
: head( path, header = nil ) : head( path, header = nil )
@ -256,7 +256,7 @@ Ruby 1.6
response = nil response = nil
Net::HTTP.start( 'some.www.server', 80 ) {|http| Net::HTTP.start( 'some.www.server', 80 ) {|http|
response = http.head( '/index.html' ) response = http.head( '/index.html' )
} }
p response['content-type'] p response['content-type']
@ -283,13 +283,13 @@ Ruby 1.6
# using block # using block
File.open( 'save.html', 'w' ) {|f| File.open( 'save.html', 'w' ) {|f|
http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str|
f.write str f.write str
end end
} }
# same effect # same effect
File.open( 'save.html', 'w' ) {|f| File.open( 'save.html', 'w' ) {|f|
http.post '/cgi-bin/search.rb', 'querytype=subject&target=ruby', nil, f http.post '/cgi-bin/search.rb', 'querytype=subject&target=ruby', nil, f
} }
: get2( path, header = nil ) : get2( path, header = nil )
@ -309,10 +309,10 @@ Ruby 1.6
# using block # using block
http.get2( '/index.html' ) {|response| http.get2( '/index.html' ) {|response|
p response['content-type'] p response['content-type']
response.read_body do |str| # read body now response.read_body do |str| # read body now
print str print str
end end
} }
: post2( path, header = nil ) : post2( path, header = nil )
@ -332,11 +332,11 @@ Ruby 1.6
# using block # using block
http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response| http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response|
p response.status p response.status
p response['content-type'] p response['content-type']
response.read_body do |str| # read body now response.read_body do |str| # read body now
print str print str
end end
} }

View file

@ -12,57 +12,93 @@
=== メールの受信 === メールの受信
メールを受信してファイル 'inbox/1' 'inbox/2'... に書きこみ、 以下のコードは、メールを受信してファイル 'inbox/1' 'inbox/2'... に
サーバ上からメールを消します。 書きこみ、サーバ上からメールを消します。pop3.server.address は適宜
pop3.server.address は適宜読みかえてください。 読みかえてください。
require 'net/pop' require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110, pop = Net::POP3.new( 'pop3.server.address', 110 )
'YourAccount', 'YourPassword' ) {|pop| pop.start( 'YourAccount', 'YourPassword' ) ###
if pop.mails.empty? then if pop.mails.empty? then
puts 'no mail.' puts 'no mail.'
else else
i = 0 i = 0
pop.each_mail do |m| # or "pop.mails.each ..." pop.each_mail do |m| # or "pop.mails.each ..."
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
m.delete m.delete
i += 1 i += 1
end
end end
puts "#{pop.mails.size} mails popped." puts "#{pop.mails.size} mails popped."
} end
pop.finish ###
POP サーバはネットワークのむこうに存在するので、なにか仕事をさせる
にはその前に開始手続きを、終わったら終了手続きを、行わなければいけ
ません。それを行うのが Net::POP3#start と #finish で、POP3 オブジェクト
はその二つのメソッドの間でだけ有効になります。
サーバ上のメールは POPMail オブジェクトとして表現されており、この
オブジェクトのメソッドを呼ぶことでメールを取ってきたり消したりする
ことができます。POP3#mails はこの POPMail オブジェクトの配列であり、
POP3#each_mail はさらに mails.each のショートカットです。
=== 短くする === 短くする
以下は動作は同じでコードを短くしたバージョンです。 上の例はあえて省略や短縮用メソッドを避けたためにかなり冗長です。
まず、ブロック付きの Net::POP3.start を使うことで POP3.new #start
#finish を併合できます。
require 'net/pop' require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110 )
'YourAccount', 'YourPassword' )
if pop.mails.empty? then
puts 'no mail.'
else
i = 0
pop.each_mail do |m| # or "pop.mails.each ..."
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop
}
m.delete
i += 1
end
puts "#{pop.mails.size} mails popped."
end
}
POP3#delete_all を使うとさらに #each_mail と m.delete を
併合できます。
require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110, Net::POP3.start( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) {|pop| 'YourAccount', 'YourPassword' ) {|pop|
if pop.mails.empty? then if pop.mails.empty? then
puts 'no mail.' puts 'no mail.'
else else
i = 0 i = 0
pop.delete_all do |m| pop.delete_all do |m|
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
i += 1 i += 1
end end
end end
} }
クラスメソッドの POP3.delete_all を使うとさらに短くなります。 クラスメソッドの POP3.delete_all を使うとさらに短くなります。
require 'net/pop' require 'net/pop'
i = 0 i = 0
Net::POP3.delete_all( 'pop3.server.address', 110, Net::POP3.delete_all( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) do |m| 'YourAccount', 'YourPassword' ) do |m|
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
i += 1 i += 1
end end
@ -78,7 +114,7 @@ pop3.server.address
Net::POP3.delete_all( 'pop3.server.address', 110, Net::POP3.delete_all( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) do |m| 'YourAccount', 'YourPassword' ) do |m|
File.open( 'inbox', 'w' ) {|f| File.open( 'inbox', 'w' ) {|f|
m.pop f #### m.pop f ####
} }
end end
@ -93,7 +129,7 @@ APOP ǧ
require 'net/pop' require 'net/pop'
Net::APOP.start( 'apop.server.address', 110, Net::APOP.start( 'apop.server.address', 110,
'YourAccount', 'YourPassword' ) {|pop| 'YourAccount', 'YourPassword' ) {|pop|
# Rest code is same. # Rest code is same.
} }
# (2) # (2)
@ -102,7 +138,7 @@ APOP ǧ
'YourAccount', 'YourPassword', 'YourAccount', 'YourPassword',
true #### true ####
) {|pop| ) {|pop|
# Rest code is same. # Rest code is same.
} }
== Net::POP3 class == Net::POP3 class
@ -119,21 +155,21 @@ APOP ǧ
password で POP ログインします。第二引数 port に nil を渡すと password で POP ログインします。第二引数 port に nil を渡すと
POP3 のデフォルトポート(110)を使います。 POP3 のデフォルトポート(110)を使います。
Net::POP3.start( addr, port, account, password ) do |pop| Net::POP3.start( addr, port, account, password ) {|pop|
pop.each_mail do |m| pop.each_mail do |m|
file.write m.pop file.write m.pop
m.delete m.delete
end end
end }
: foreach( address, port = 110, account, password ) {|mail| .... } : foreach( address, port = 110, account, password ) {|mail| .... }
POP セッションを開き、サーバ上のすべてのメールに対して繰り返します。 POP セッションを開き、サーバ上のすべてのメールに対して繰り返します。
以下と同じです。 以下と同じです。
Net::POP3.start( address, port, account, password ) {|pop| Net::POP3.start( address, port, account, password ) {|pop|
pop.each_mail do |m| pop.each_mail do |m|
yield m yield m
end end
} }
# example # example
@ -159,10 +195,10 @@ APOP ǧ
POP before SMTP 専用です。 POP before SMTP 専用です。
# example # example
pop = Net::POP3.auth_only( 'your.pop3.server', Net::POP3.auth_only( 'your.pop3.server',
nil, # using default (110) nil, # using default (110)
'YourAccount', 'YourAccount',
'YourPassword' ) 'YourPassword' )
=== メソッド === メソッド
@ -249,20 +285,20 @@ POP
# example # example
allmails = nil allmails = nil
POP3.start( 'your.pop3.server', 110, POP3.start( 'your.pop3.server', 110,
'YourAccount, 'YourPassword' ) do |pop| 'YourAccount, 'YourPassword' ) {|pop|
allmails = pop.mails.collect {|popmail| popmail.pop } allmails = pop.mails.collect {|popmail| popmail.pop }
end }
: pop {|str| .... } : pop {|str| .... }
メールの文字列を少しづつ読みこみ、順次ブロックに与えます。 メールの文字列を少しづつ読みこみ、順次ブロックに与えます。
# example # example
POP3.start( 'localhost', 110 ) {|pop3| POP3.start( 'localhost', 110 ) {|pop3|
pop3.each_mail do |m| pop3.each_mail do |m|
m.pop do |str| m.pop do |str|
# do anything # do anything
end end
end end
} }
: header : header

View file

@ -20,7 +20,7 @@ SMTP
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
# use smtp object only in this block # use smtp object only in this block
} }
your.smtp.server は適切な SMTP サーバのアドレスに読みかえてください。 your.smtp.server は適切な SMTP サーバのアドレスに読みかえてください。
@ -31,7 +31,7 @@ your.smtp.server
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.send_mail <<EndOfMail, 'your@mail.address', 'to@some.domain' smtp.send_mail <<EndOfMail, 'your@mail.address', 'to@some.domain'
From: Your Name <your@mail.address> From: Your Name <your@mail.address>
To: Dest Address <to@some.domain> To: Dest Address <to@some.domain>
Subject: test mail Subject: test mail
@ -42,6 +42,27 @@ your.smtp.server
EndOfMail EndOfMail
} }
=== セッションを終了する
メールを送ったら SMTP#finish を呼んでセッションを終了しなければいけ
ません。File のように GC 時に勝手に close されることもありません。
いろいろなところで finish がないソースコードの例を見掛けますが、
すべて誤りです。finish は必ず呼んでください。
またブロック付きの SMTP.start/SMTP#start を使うと勝手に finish を
呼んでくれるので便利です。可能な限りブロック付きの start を使うのが
よいでしょう。
# using SMTP#finish
smtp = Net::SMTP.start( 'your.smtp.server', 25 )
smtp.send_mail mail_string, 'from@address', 'to@address'
smtp.finish
# using block form of SMTP.start
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.send_mail mail_string, 'from@address', 'to@address'
}
=== 文字列以外からの送信 === 文字列以外からの送信
ひとつ上の例では文字列リテラル(ヒアドキュメント)を使って送信しましたが、 ひとつ上の例では文字列リテラル(ヒアドキュメント)を使って送信しましたが、
@ -50,9 +71,9 @@ each
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
File.open( 'Mail/draft/1' ) {|f| File.open( 'Mail/draft/1' ) {|f|
smtp.send_mail f, 'your@mail.address', 'to@some.domain' smtp.send_mail f, 'your@mail.address', 'to@some.domain'
} }
} }
=== Hello ドメイン === Hello ドメイン
@ -82,7 +103,7 @@ SMTP
# example # example
Net::SMTP.start( 'your.smtp.server' ) { Net::SMTP.start( 'your.smtp.server' ) {
smtp.send_mail mail_string, 'from@mail.address', 'dest@mail.address' smtp.send_mail mail_string, 'from@mail.address', 'dest@mail.address'
} }
=== メソッド === メソッド
@ -128,9 +149,9 @@ SMTP
# example # example
Net::SMTP.start( 'your.smtp.server' ) {|smtp| Net::SMTP.start( 'your.smtp.server' ) {|smtp|
smtp.send_mail mail_string, smtp.send_mail mail_string,
'from@mail.address', 'from@mail.address',
'dest@mail.address' 'dest2@mail.address' 'dest@mail.address' 'dest2@mail.address'
} }
: ready( from_addr, *to_addrs ) {|adapter| .... } : ready( from_addr, *to_addrs ) {|adapter| .... }
@ -140,11 +161,11 @@ SMTP
# example # example
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter|
adapter.write str1 adapter.write str1
adapter.write str2 adapter.write str2
adapter.write str3 adapter.write str3
end end
} }
== 発生する例外 == 発生する例外

View file

@ -43,8 +43,8 @@ This is required for compatibility.
require 'net/http' require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http| Net::HTTP.start( 'some.www.server', 80 ) {|http|
response , = http.post( '/cgi-bin/any.rhtml', response , = http.post( '/cgi-bin/any.rhtml',
'querytype=subject&target=ruby' ) 'querytype=subject&target=ruby' )
} }
=== Accessing via Proxy === Accessing via Proxy
@ -94,9 +94,9 @@ URI class will be included in ruby standard library.
require 'net/http' require 'net/http'
Net::HTTP.start( 'auth.some.domain' ) {|http| Net::HTTP.start( 'auth.some.domain' ) {|http|
response , = http.get( '/need-auth.cgi', response , = http.get( '/need-auth.cgi',
'Authentication' => ["#{account}:#{password}"].pack('m').strip ) 'Authentication' => ["#{account}:#{password}"].pack('m').strip )
print response.body print response.body
} }
In version 1.2 (Ruby 1.7 or later), you can write like this: In version 1.2 (Ruby 1.7 or later), you can write like this:
@ -106,8 +106,8 @@ In version 1.2 (Ruby 1.7 or later), you can write like this:
req = Net::HTTP::Get.new('/need-auth.cgi') req = Net::HTTP::Get.new('/need-auth.cgi')
req.basic_auth 'account', 'password' req.basic_auth 'account', 'password'
Net::HTTP.start( 'auth.some.domain' ) {|http| Net::HTTP.start( 'auth.some.domain' ) {|http|
response = http.request( req ) response = http.request(req)
print response.body print response.body
} }
== Switching Net::HTTP versions == Switching Net::HTTP versions
@ -157,11 +157,11 @@ Yes, this is not thread-safe.
# example # example
proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 ) proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 )
: :
proxy_class.start( 'www.ruby-lang.org' ) do |http| proxy_class.start( 'www.ruby-lang.org' ) {|http|
# connecting proxy.foo.org:8080 # connecting proxy.foo.org:8080
: :
end }
: proxy_class? : proxy_class?
If self is HTTP, false. If self is HTTP, false.
@ -242,13 +242,13 @@ Yes, this is not thread-safe.
# using block # using block
File.open( 'save.txt', 'w' ) {|f| File.open( 'save.txt', 'w' ) {|f|
http.get( '/~foo/', nil ) do |str| http.get( '/~foo/', nil ) do |str|
f.write str f.write str
end end
} }
# same effect # same effect
File.open( 'save.txt', 'w' ) {|f| File.open( 'save.txt', 'w' ) {|f|
http.get '/~foo/', nil, f http.get '/~foo/', nil, f
} }
: head( path, header = nil ) : head( path, header = nil )
@ -263,7 +263,7 @@ Yes, this is not thread-safe.
response = nil response = nil
Net::HTTP.start( 'some.www.server', 80 ) {|http| Net::HTTP.start( 'some.www.server', 80 ) {|http|
response = http.head( '/index.html' ) response = http.head( '/index.html' )
} }
p response['content-type'] p response['content-type']
@ -290,13 +290,13 @@ Yes, this is not thread-safe.
# using block # using block
File.open( 'save.html', 'w' ) {|f| File.open( 'save.html', 'w' ) {|f|
http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str|
f.write str f.write str
end end
} }
# same effect # same effect
File.open( 'save.html', 'w' ) {|f| File.open( 'save.html', 'w' ) {|f|
http.post '/cgi-bin/search.rb', 'querytype=subject&target=ruby', nil, f http.post '/cgi-bin/search.rb', 'querytype=subject&target=ruby', nil, f
} }
: get2( path, header = nil ) : get2( path, header = nil )
@ -315,10 +315,10 @@ Yes, this is not thread-safe.
# using block # using block
http.get2( '/index.html' ) {|response| http.get2( '/index.html' ) {|response|
p response['content-type'] p response['content-type']
response.read_body do |str| # read body now response.read_body do |str| # read body now
print str print str
end end
} }
: post2( path, header = nil ) : post2( path, header = nil )
@ -335,11 +335,11 @@ Yes, this is not thread-safe.
# using block # using block
http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response| http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response|
p response.status p response.status
p response['content-type'] p response['content-type']
response.read_body do |str| # read body now response.read_body do |str| # read body now
print str print str
end end
} }

View file

@ -29,49 +29,81 @@ Replace 'pop3.server.address' your POP3 server address.
require 'net/pop' require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110, pop = Net::POP3.new( 'pop3.server.address', 110 )
'YourAccount', 'YourPassword' ) {|pop| pop.start( 'YourAccount', 'YourPassword' ) ###
if pop.mails.empty? then if pop.mails.empty? then
puts 'no mail.' puts 'no mail.'
else else
i = 0 i = 0
pop.each_mail do |m| # or "pop.mails.each ..." pop.each_mail do |m| # or "pop.mails.each ..."
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
m.delete m.delete
i += 1 i += 1
end
end end
puts "#{pop.mails.size} mails popped." puts "#{pop.mails.size} mails popped."
} end
pop.finish ###
=== Shorter Version (1) call Net::POP3#start and start POP session
(2) access mails by using POP3#each_mail and/or POP3#mails
(3) close POP session by calling POP3#finish or use block form #start.
This example is using block form #start to close the session.
=== Enshort Code
The example above is very verbose. You can enshort code by using
some utility methods. At first, block form of Net::POP3.start can
alternates POP3.new, POP3#start and POP3#finish.
require 'net/pop' require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110 )
'YourAccount', 'YourPassword' )
if pop.mails.empty? then
puts 'no mail.'
else
i = 0
pop.each_mail do |m| # or "pop.mails.each ..."
File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop
}
m.delete
i += 1
end
puts "#{pop.mails.size} mails popped."
end
}
POP3#delete_all alternates #each_mail and m.delete.
require 'net/pop'
Net::POP3.start( 'pop3.server.address', 110, Net::POP3.start( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) {|pop| 'YourAccount', 'YourPassword' ) {|pop|
if pop.mails.empty? then if pop.mails.empty? then
puts 'no mail.' puts 'no mail.'
else else
i = 0 i = 0
pop.delete_all do |m| pop.delete_all do |m|
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
i += 1 i += 1
end end
end end
} }
And here is more shorter example. And here is more shorter example.
require 'net/pop' require 'net/pop'
i = 0 i = 0
Net::POP3.delete_all( 'pop3.server.address', 110, Net::POP3.delete_all( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) do |m| 'YourAccount', 'YourPassword' ) do |m|
File.open( 'inbox/' + i.to_s, 'w' ) {|f| File.open( 'inbox/' + i.to_s, 'w' ) {|f|
f.write m.pop f.write m.pop
} }
i += 1 i += 1
end end
@ -85,7 +117,7 @@ This example does not create such one.
Net::POP3.delete_all( 'pop3.server.address', 110, Net::POP3.delete_all( 'pop3.server.address', 110,
'YourAccount', 'YourPassword' ) do |m| 'YourAccount', 'YourPassword' ) do |m|
File.open( 'inbox', 'w' ) {|f| File.open( 'inbox', 'w' ) {|f|
m.pop f #### m.pop f ####
} }
end end
@ -99,7 +131,7 @@ net/pop also supports APOP authentication. There's two way to use APOP:
require 'net/pop' require 'net/pop'
Net::APOP.start( 'apop.server.address', 110, Net::APOP.start( 'apop.server.address', 110,
'YourAccount', 'YourPassword' ) {|pop| 'YourAccount', 'YourPassword' ) {|pop|
# Rest code is same. # Rest code is same.
} }
# (2) # (2)
@ -108,7 +140,7 @@ net/pop also supports APOP authentication. There's two way to use APOP:
'YourAccount', 'YourPassword', 'YourAccount', 'YourPassword',
true #### true ####
) {|pop| ) {|pop|
# Rest code is same. # Rest code is same.
} }
== Net::POP3 class == Net::POP3 class
@ -123,21 +155,21 @@ net/pop also supports APOP authentication. There's two way to use APOP:
: start( address, port = 110, account, password ) {|pop| .... } : start( address, port = 110, account, password ) {|pop| .... }
equals to Net::POP3.new( address, port ).start( account, password ) equals to Net::POP3.new( address, port ).start( account, password )
Net::POP3.start( addr, port, account, password ) do |pop| Net::POP3.start( addr, port, account, password ) {|pop|
pop.each_mail do |m| pop.each_mail do |m|
file.write m.pop file.write m.pop
m.delete m.delete
end end
end }
: foreach( address, port = 110, account, password ) {|mail| .... } : foreach( address, port = 110, account, password ) {|mail| .... }
starts POP3 protocol and iterates for each POPMail object. starts POP3 protocol and iterates for each POPMail object.
This method equals to This method equals to
Net::POP3.start( address, port, account, password ) {|pop| Net::POP3.start( address, port, account, password ) {|pop|
pop.each_mail do |m| pop.each_mail do |m|
yield m yield m
end end
} }
# example # example
@ -163,10 +195,10 @@ net/pop also supports APOP authentication. There's two way to use APOP:
This method must not be called while POP3 session is opened. This method must not be called while POP3 session is opened.
# example # example
pop = Net::POP3.auth_only( 'your.pop3.server', Net::POP3.auth_only( 'your.pop3.server',
nil, # using default (110) nil, # using default (110)
'YourAccount', 'YourAccount',
'YourPassword' ) 'YourPassword' )
=== Instance Methods === Instance Methods
@ -253,20 +285,20 @@ A class of mail which exists on POP server.
# example # example
allmails = nil allmails = nil
POP3.start( 'your.pop3.server', 110, POP3.start( 'your.pop3.server', 110,
'YourAccount, 'YourPassword' ) do |pop| 'YourAccount, 'YourPassword' ) {|pop|
allmails = pop.mails.collect {|popmail| popmail.pop } allmails = pop.mails.collect {|popmail| popmail.pop }
end }
: pop {|str| .... } : pop {|str| .... }
gives the block part strings of a mail. gives the block part strings of a mail.
# example # example
POP3.start( 'localhost', 110 ) {|pop3| POP3.start( 'localhost', 110 ) {|pop3|
pop3.each_mail do |m| pop3.each_mail do |m|
m.pop do |str| m.pop do |str|
# do anything # do anything
end end
end end
} }
: header : header

View file

@ -146,8 +146,8 @@ module Net
def connect def connect
conn_socket @address, @port conn_socket @address, @port
conn_command @socket
on_connect on_connect
conn_command @socket
end end
def re_connect def re_connect

View file

@ -37,7 +37,7 @@ executed.
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
# use smtp object only in this block # use smtp object only in this block
} }
Replace 'your.smtp.server' by your SMTP server. Normally Replace 'your.smtp.server' by your SMTP server. Normally
@ -49,7 +49,7 @@ Then you can send mail.
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.send_mail <<EndOfMail, 'your@mail.address', 'to@some.domain' smtp.send_mail <<EndOfMail, 'your@mail.address', 'to@some.domain'
From: Your Name <your@mail.address> From: Your Name <your@mail.address>
To: Dest Address <to@some.domain> To: Dest Address <to@some.domain>
Subject: test mail Subject: test mail
@ -60,6 +60,23 @@ Then you can send mail.
EndOfMail EndOfMail
} }
=== Closing Session
You MUST close SMTP session after sending mails, by calling #finish
method. You can also use block form of SMTP.start/SMTP#start, which
closes session automatically. I strongly recommend later one. It is
more beautiful and simple.
# using SMTP#finish
smtp = Net::SMTP.start( 'your.smtp.server', 25 )
smtp.send_mail mail_string, 'from@address', 'to@address'
smtp.finish
# using block form of SMTP.start
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.send_mail mail_string, 'from@address', 'to@address'
}
=== Sending Mails from Any Sources === Sending Mails from Any Sources
In an example above I sent mail from String (here document literal). In an example above I sent mail from String (here document literal).
@ -68,9 +85,9 @@ like File and Array.
require 'net/smtp' require 'net/smtp'
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
File.open( 'Mail/draft/1' ) {|f| File.open( 'Mail/draft/1' ) {|f|
smtp.send_mail f, 'your@mail.address', 'to@some.domain' smtp.send_mail f, 'your@mail.address', 'to@some.domain'
} }
} }
=== Giving "Hello" Domain === Giving "Hello" Domain
@ -99,7 +116,7 @@ send or reject SMTP session by this data.
# example # example
Net::SMTP.start( 'your.smtp.server' ) { Net::SMTP.start( 'your.smtp.server' ) {
smtp.send_mail mail_string, 'from@mail.address', 'dest@mail.address' smtp.send_mail mail_string, 'from@mail.address', 'dest@mail.address'
} }
=== Instance Methods === Instance Methods
@ -153,9 +170,9 @@ send or reject SMTP session by this data.
# example # example
Net::SMTP.start( 'your.smtp.server' ) {|smtp| Net::SMTP.start( 'your.smtp.server' ) {|smtp|
smtp.send_mail mail_string, smtp.send_mail mail_string,
'from@mail.address', 'from@mail.address',
'dest@mail.address' 'dest2@mail.address' 'dest@mail.address' 'dest2@mail.address'
} }
: ready( from_addr, *to_addrs ) {|adapter| .... } : ready( from_addr, *to_addrs ) {|adapter| .... }
@ -169,11 +186,11 @@ send or reject SMTP session by this data.
# example # example
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter|
adapter.write str1 adapter.write str1
adapter.write str2 adapter.write str2
adapter.write str3 adapter.write str3
end end
} }
== Exceptions == Exceptions