diff options
author | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-09 08:58:30 +0000 |
---|---|---|
committer | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-09 08:58:30 +0000 |
commit | fa6dc283cd81445d4220018f49f5c82a572101fe (patch) | |
tree | fbb338267eb6b1d0fc9f88483542d19053b8ef38 | |
parent | a20863ff41b0493da0d16597b693e88b54731c03 (diff) | |
download | ruby-fa6dc283cd81445d4220018f49f5c82a572101fe.tar.gz |
aamine
* lib/net/protocol.rb: calls on_connect before conn_command
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1891 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/net/http.rd.ja | 60 | ||||
-rw-r--r-- | doc/net/pop.rd.ja | 142 | ||||
-rw-r--r-- | doc/net/smtp.rd.ja | 49 | ||||
-rw-r--r-- | lib/net/http.rb | 60 | ||||
-rw-r--r-- | lib/net/pop.rb | 130 | ||||
-rw-r--r-- | lib/net/protocol.rb | 2 | ||||
-rw-r--r-- | lib/net/smtp.rb | 45 |
8 files changed, 301 insertions, 191 deletions
@@ -1,3 +1,7 @@ +Sun Dec 9 18:06:26 2001 Minero Aoki <aamine@loveruby.net> + + * lib/net/protocol.rb: calls on_connect before conn_command + Fri Dec 7 19:12:14 2001 Minero Aoki <aamine@loveruby.net> * lib/net/smtp.rb: SMTP.new requires at least one arg. diff --git a/doc/net/http.rd.ja b/doc/net/http.rd.ja index d1c098e17f..69cdfb17c7 100644 --- a/doc/net/http.rd.ja +++ b/doc/net/http.rd.ja @@ -27,8 +27,8 @@ require 'net/http' Net::HTTP.start( 'some.www.server', 80 ) {|http| - response , = http.post( '/cgi-bin/any.rhtml', - 'querytype=subject&target=ruby' ) + response , = http.post( '/cgi-bin/any.rhtml', + 'querytype=subject&target=ruby' ) } === プロクシ経由のアクセス @@ -78,9 +78,9 @@ Net::HTTP を継承しているので Net::HTTP と全く同じように使えます。 require 'net/http' Net::HTTP.start( 'auth.some.domain' ) {|http| - response , = http.get( '/need-auth.cgi', - 'Authentication' => ["#{account}:#{password}"].pack('m').strip ) - print response.body + response , = http.get( '/need-auth.cgi', + 'Authentication' => ["#{account}:#{password}"].pack('m').strip ) + print response.body } バージョン 1.2 (Ruby 1.7 以降に添付) では次のように書けます。 @@ -90,8 +90,8 @@ Net::HTTP を継承しているので Net::HTTP と全く同じように使えます。 req = Net::HTTP::Get.new('/need-auth.cgi') req.basic_auth 'account', 'password' Net::HTTP.start( 'auth.some.domain' ) {|http| - response = http.request( req ) - print response.body + response = http.request(req) + print response.body } == 新しい仕様への変更と移行措置について @@ -148,11 +148,11 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 # example proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 ) - : - proxy_class.start( 'www.ruby-lang.org' ) do |http| - # connecting proxy.foo.org:8080 - : - end + : + proxy_class.start( 'www.ruby-lang.org' ) {|http| + # connecting proxy.foo.org:8080 + : + } : proxy_class? 自身が (Proxy メソッドによって作成された) プロクシ用のクラスならば真。 @@ -233,13 +233,13 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 # using block File.open( 'save.txt', 'w' ) {|f| - http.get( '/~foo/', nil ) do |str| - f.write str - end + http.get( '/~foo/', nil ) do |str| + f.write str + end } # same effect File.open( 'save.txt', 'w' ) {|f| - http.get '/~foo/', nil, f + http.get '/~foo/', nil, f } : head( path, header = nil ) @@ -256,7 +256,7 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 response = nil Net::HTTP.start( 'some.www.server', 80 ) {|http| - response = http.head( '/index.html' ) + response = http.head( '/index.html' ) } p response['content-type'] @@ -283,13 +283,13 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 # using block File.open( 'save.html', 'w' ) {|f| - http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| - f.write str - end + http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| + f.write str + end } # same effect 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 ) @@ -309,10 +309,10 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 # using block http.get2( '/index.html' ) {|response| - p response['content-type'] - response.read_body do |str| # read body now - print str - end + p response['content-type'] + response.read_body do |str| # read body now + print str + end } : post2( path, header = nil ) @@ -332,11 +332,11 @@ Ruby 1.6 に入っているのが http.rb 1.1 で 1.7 以降が 1.2 ですが、 # using block http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response| - p response.status - p response['content-type'] - response.read_body do |str| # read body now - print str - end + p response.status + p response['content-type'] + response.read_body do |str| # read body now + print str + end } : request( request [, data] ) diff --git a/doc/net/pop.rd.ja b/doc/net/pop.rd.ja index e1d1bb577d..e81d6371ae 100644 --- a/doc/net/pop.rd.ja +++ b/doc/net/pop.rd.ja @@ -12,57 +12,93 @@ === メールの受信 -メールを受信してファイル 'inbox/1' 'inbox/2'... に書きこみ、 -サーバ上からメールを消します。 -pop3.server.address は適宜読みかえてください。 +以下のコードは、メールを受信してファイル 'inbox/1' 'inbox/2'... に +書きこみ、サーバ上からメールを消します。pop3.server.address は適宜 +読みかえてください。 require 'net/pop' - Net::POP3.start( 'pop3.server.address', 110, - 'YourAccount', 'YourPassword' ) {|pop| - 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| + pop = Net::POP3.new( 'pop3.server.address', 110 ) + pop.start( '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 + } + m.delete + i += 1 end 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' + + 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, 'YourAccount', 'YourPassword' ) {|pop| - if pop.mails.empty? then - puts 'no mail.' - else - i = 0 - pop.delete_all do |m| - File.open( 'inbox/' + i.to_s, 'w' ) {|f| - f.write m.pop - } - i += 1 - end - end + if pop.mails.empty? then + puts 'no mail.' + else + i = 0 + pop.delete_all do |m| + File.open( 'inbox/' + i.to_s, 'w' ) {|f| + f.write m.pop + } + i += 1 + end + end } クラスメソッドの POP3.delete_all を使うとさらに短くなります。 require 'net/pop' + i = 0 Net::POP3.delete_all( 'pop3.server.address', 110, 'YourAccount', 'YourPassword' ) do |m| File.open( 'inbox/' + i.to_s, 'w' ) {|f| - f.write m.pop + f.write m.pop } i += 1 end @@ -78,7 +114,7 @@ pop3.server.address は適宜読みかえてください。 Net::POP3.delete_all( 'pop3.server.address', 110, 'YourAccount', 'YourPassword' ) do |m| File.open( 'inbox', 'w' ) {|f| - m.pop f #### + m.pop f #### } end @@ -93,7 +129,7 @@ APOP 認証を使うには require 'net/pop' Net::APOP.start( 'apop.server.address', 110, 'YourAccount', 'YourPassword' ) {|pop| - # Rest code is same. + # Rest code is same. } # (2) @@ -102,7 +138,7 @@ APOP 認証を使うには 'YourAccount', 'YourPassword', true #### ) {|pop| - # Rest code is same. + # Rest code is same. } == Net::POP3 class @@ -119,21 +155,21 @@ APOP 認証を使うには password で POP ログインします。第二引数 port に nil を渡すと POP3 のデフォルトポート(110)を使います。 - Net::POP3.start( addr, port, account, password ) do |pop| - pop.each_mail do |m| - file.write m.pop - m.delete - end - end + Net::POP3.start( addr, port, account, password ) {|pop| + pop.each_mail do |m| + file.write m.pop + m.delete + end + } : foreach( address, port = 110, account, password ) {|mail| .... } POP セッションを開き、サーバ上のすべてのメールに対して繰り返します。 以下と同じです。 Net::POP3.start( address, port, account, password ) {|pop| - pop.each_mail do |m| - yield m - end + pop.each_mail do |m| + yield m + end } # example @@ -159,10 +195,10 @@ APOP 認証を使うには POP before SMTP 専用です。 # example - pop = Net::POP3.auth_only( 'your.pop3.server', - nil, # using default (110) - 'YourAccount', - 'YourPassword' ) + Net::POP3.auth_only( 'your.pop3.server', + nil, # using default (110) + 'YourAccount', + 'YourPassword' ) === メソッド @@ -249,20 +285,20 @@ POP サーバー上のメール一通を抽象的に表現するクラス。 # example allmails = nil POP3.start( 'your.pop3.server', 110, - 'YourAccount, 'YourPassword' ) do |pop| - allmails = pop.mails.collect {|popmail| popmail.pop } - end + 'YourAccount, 'YourPassword' ) {|pop| + allmails = pop.mails.collect {|popmail| popmail.pop } + } : pop {|str| .... } メールの文字列を少しづつ読みこみ、順次ブロックに与えます。 # example POP3.start( 'localhost', 110 ) {|pop3| - pop3.each_mail do |m| - m.pop do |str| - # do anything - end - end + pop3.each_mail do |m| + m.pop do |str| + # do anything + end + end } : header diff --git a/doc/net/smtp.rd.ja b/doc/net/smtp.rd.ja index 3484e61ccb..60aa9ef47c 100644 --- a/doc/net/smtp.rd.ja +++ b/doc/net/smtp.rd.ja @@ -20,7 +20,7 @@ SMTP を使ってメールを送るにはまず SMTP.start でセッションを開きます。 require 'net/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 サーバのアドレスに読みかえてください。 @@ -31,7 +31,7 @@ your.smtp.server は適切な SMTP サーバのアドレスに読みかえてください。 require 'net/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> To: Dest Address <to@some.domain> Subject: test mail @@ -42,6 +42,27 @@ your.smtp.server は適切な SMTP サーバのアドレスに読みかえてください。 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' Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| - File.open( 'Mail/draft/1' ) {|f| - smtp.send_mail f, 'your@mail.address', 'to@some.domain' - } + File.open( 'Mail/draft/1' ) {|f| + smtp.send_mail f, 'your@mail.address', 'to@some.domain' + } } === Hello ドメイン @@ -82,7 +103,7 @@ SMTP ではメールを送る側のホストの名前を要求されるのですが、 # example 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 Net::SMTP.start( 'your.smtp.server' ) {|smtp| - smtp.send_mail mail_string, - 'from@mail.address', - 'dest@mail.address' 'dest2@mail.address' + smtp.send_mail mail_string, + 'from@mail.address', + 'dest@mail.address' 'dest2@mail.address' } : ready( from_addr, *to_addrs ) {|adapter| .... } @@ -140,11 +161,11 @@ SMTP ではメールを送る側のホストの名前を要求されるのですが、 # example Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| - smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| - adapter.write str1 - adapter.write str2 - adapter.write str3 - end + smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| + adapter.write str1 + adapter.write str2 + adapter.write str3 + end } == 発生する例外 diff --git a/lib/net/http.rb b/lib/net/http.rb index 730ea8eafc..5fb2e299a0 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -43,8 +43,8 @@ This is required for compatibility. require 'net/http' Net::HTTP.start( 'some.www.server', 80 ) {|http| - response , = http.post( '/cgi-bin/any.rhtml', - 'querytype=subject&target=ruby' ) + response , = http.post( '/cgi-bin/any.rhtml', + 'querytype=subject&target=ruby' ) } === Accessing via Proxy @@ -94,9 +94,9 @@ URI class will be included in ruby standard library. require 'net/http' Net::HTTP.start( 'auth.some.domain' ) {|http| - response , = http.get( '/need-auth.cgi', - 'Authentication' => ["#{account}:#{password}"].pack('m').strip ) - print response.body + response , = http.get( '/need-auth.cgi', + 'Authentication' => ["#{account}:#{password}"].pack('m').strip ) + print response.body } 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.basic_auth 'account', 'password' Net::HTTP.start( 'auth.some.domain' ) {|http| - response = http.request( req ) - print response.body + response = http.request(req) + print response.body } == Switching Net::HTTP versions @@ -157,11 +157,11 @@ Yes, this is not thread-safe. # example proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 ) - : - proxy_class.start( 'www.ruby-lang.org' ) do |http| - # connecting proxy.foo.org:8080 - : - end + : + proxy_class.start( 'www.ruby-lang.org' ) {|http| + # connecting proxy.foo.org:8080 + : + } : proxy_class? If self is HTTP, false. @@ -242,13 +242,13 @@ Yes, this is not thread-safe. # using block File.open( 'save.txt', 'w' ) {|f| - http.get( '/~foo/', nil ) do |str| - f.write str - end + http.get( '/~foo/', nil ) do |str| + f.write str + end } # same effect File.open( 'save.txt', 'w' ) {|f| - http.get '/~foo/', nil, f + http.get '/~foo/', nil, f } : head( path, header = nil ) @@ -263,7 +263,7 @@ Yes, this is not thread-safe. response = nil Net::HTTP.start( 'some.www.server', 80 ) {|http| - response = http.head( '/index.html' ) + response = http.head( '/index.html' ) } p response['content-type'] @@ -290,13 +290,13 @@ Yes, this is not thread-safe. # using block File.open( 'save.html', 'w' ) {|f| - http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| - f.write str - end + http.post( '/cgi-bin/search.rb', 'querytype=subject&target=ruby' ) do |str| + f.write str + end } # same effect 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 ) @@ -315,10 +315,10 @@ Yes, this is not thread-safe. # using block http.get2( '/index.html' ) {|response| - p response['content-type'] - response.read_body do |str| # read body now - print str - end + p response['content-type'] + response.read_body do |str| # read body now + print str + end } : post2( path, header = nil ) @@ -335,11 +335,11 @@ Yes, this is not thread-safe. # using block http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response| - p response.status - p response['content-type'] - response.read_body do |str| # read body now - print str - end + p response.status + p response['content-type'] + response.read_body do |str| # read body now + print str + end } : request( request [, data] ) diff --git a/lib/net/pop.rb b/lib/net/pop.rb index 925dfce147..eca7c60949 100644 --- a/lib/net/pop.rb +++ b/lib/net/pop.rb @@ -29,49 +29,81 @@ Replace 'pop3.server.address' your POP3 server address. require 'net/pop' - Net::POP3.start( 'pop3.server.address', 110, - 'YourAccount', 'YourPassword' ) {|pop| - 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| + pop = Net::POP3.new( 'pop3.server.address', 110 ) + pop.start( '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 + } + m.delete + i += 1 end puts "#{pop.mails.size} mails popped." + end + pop.finish ### + +(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' + + 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 } -=== Shorter Version +POP3#delete_all alternates #each_mail and m.delete. require 'net/pop' + Net::POP3.start( 'pop3.server.address', 110, 'YourAccount', 'YourPassword' ) {|pop| - if pop.mails.empty? then - puts 'no mail.' - else - i = 0 - pop.delete_all do |m| - File.open( 'inbox/' + i.to_s, 'w' ) {|f| - f.write m.pop - } - i += 1 - end - end + if pop.mails.empty? then + puts 'no mail.' + else + i = 0 + pop.delete_all do |m| + File.open( 'inbox/' + i.to_s, 'w' ) {|f| + f.write m.pop + } + i += 1 + end + end } And here is more shorter example. require 'net/pop' + i = 0 Net::POP3.delete_all( 'pop3.server.address', 110, 'YourAccount', 'YourPassword' ) do |m| File.open( 'inbox/' + i.to_s, 'w' ) {|f| - f.write m.pop + f.write m.pop } i += 1 end @@ -85,7 +117,7 @@ This example does not create such one. Net::POP3.delete_all( 'pop3.server.address', 110, 'YourAccount', 'YourPassword' ) do |m| File.open( 'inbox', 'w' ) {|f| - m.pop f #### + m.pop f #### } end @@ -99,7 +131,7 @@ net/pop also supports APOP authentication. There's two way to use APOP: require 'net/pop' Net::APOP.start( 'apop.server.address', 110, 'YourAccount', 'YourPassword' ) {|pop| - # Rest code is same. + # Rest code is same. } # (2) @@ -108,7 +140,7 @@ net/pop also supports APOP authentication. There's two way to use APOP: 'YourAccount', 'YourPassword', true #### ) {|pop| - # Rest code is same. + # Rest code is same. } == 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| .... } equals to Net::POP3.new( address, port ).start( account, password ) - Net::POP3.start( addr, port, account, password ) do |pop| - pop.each_mail do |m| - file.write m.pop - m.delete - end - end + Net::POP3.start( addr, port, account, password ) {|pop| + pop.each_mail do |m| + file.write m.pop + m.delete + end + } : foreach( address, port = 110, account, password ) {|mail| .... } starts POP3 protocol and iterates for each POPMail object. This method equals to Net::POP3.start( address, port, account, password ) {|pop| - pop.each_mail do |m| - yield m - end + pop.each_mail do |m| + yield m + end } # 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. # example - pop = Net::POP3.auth_only( 'your.pop3.server', - nil, # using default (110) - 'YourAccount', - 'YourPassword' ) + Net::POP3.auth_only( 'your.pop3.server', + nil, # using default (110) + 'YourAccount', + 'YourPassword' ) === Instance Methods @@ -253,20 +285,20 @@ A class of mail which exists on POP server. # example allmails = nil POP3.start( 'your.pop3.server', 110, - 'YourAccount, 'YourPassword' ) do |pop| - allmails = pop.mails.collect {|popmail| popmail.pop } - end + 'YourAccount, 'YourPassword' ) {|pop| + allmails = pop.mails.collect {|popmail| popmail.pop } + } : pop {|str| .... } gives the block part strings of a mail. # example POP3.start( 'localhost', 110 ) {|pop3| - pop3.each_mail do |m| - m.pop do |str| - # do anything - end - end + pop3.each_mail do |m| + m.pop do |str| + # do anything + end + end } : header diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 600dc04b58..c67be1863f 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -146,8 +146,8 @@ module Net def connect conn_socket @address, @port - conn_command @socket on_connect + conn_command @socket end def re_connect diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index 5331ee17e7..dfb27660fd 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -37,7 +37,7 @@ executed. require 'net/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 @@ -49,7 +49,7 @@ Then you can send mail. require 'net/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> To: Dest Address <to@some.domain> Subject: test mail @@ -60,6 +60,23 @@ Then you can send mail. 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 In an example above I sent mail from String (here document literal). @@ -68,9 +85,9 @@ like File and Array. require 'net/smtp' Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| - File.open( 'Mail/draft/1' ) {|f| - smtp.send_mail f, 'your@mail.address', 'to@some.domain' - } + File.open( 'Mail/draft/1' ) {|f| + smtp.send_mail f, 'your@mail.address', 'to@some.domain' + } } === Giving "Hello" Domain @@ -99,7 +116,7 @@ send or reject SMTP session by this data. # example 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 @@ -153,9 +170,9 @@ send or reject SMTP session by this data. # example Net::SMTP.start( 'your.smtp.server' ) {|smtp| - smtp.send_mail mail_string, - 'from@mail.address', - 'dest@mail.address' 'dest2@mail.address' + smtp.send_mail mail_string, + 'from@mail.address', + 'dest@mail.address' 'dest2@mail.address' } : ready( from_addr, *to_addrs ) {|adapter| .... } @@ -169,11 +186,11 @@ send or reject SMTP session by this data. # example Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| - smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| - adapter.write str1 - adapter.write str2 - adapter.write str3 - end + smtp.ready( 'from@mail.addr', 'dest@mail.addr' ) do |adapter| + adapter.write str1 + adapter.write str2 + adapter.write str3 + end } == Exceptions |