# Exception in for next item

**URL:** <https://www.ruby-forum.com/t/exception-in-for-next-item/56352>\
**Category:** Ruby\
**Created:** [January 5, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/exception-in-for-next-item/56352 "2006-01-05T22:01:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 5, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/exception-in-for-next-item/56352/1 "2006-01-05T22:01:12Z")

</div>

Hello

I retrieve a number of message 1 to n  
And try to send these by stmp that is no problem with te following code  
:

```
 messages = Spool.find_created_messages

 Net::SMTP.start('sigma.itxl.nl',25,'itxl.nl') do | smtp |
   for message in messages
     begin
       smtp.send_message message.mime_content, message.email_from 

```

,message.email\_to  
message.destroy  
rescue Net::SMTPSyntaxError  
message.state = ‘ERROR’  
message.save  
rescue Exception =\> exc  
message.state = ‘ERROR’  
message.save  
STDERR.puts “General spool error #{exc.class} #{exc.message}”  
end  
end  
end

I would like to handel smtp errors gracefully, log the message en  
continue with the next message  
in the loop.  
For example when the email adres has a syntac error danielitxl.nl ( no  
@)  
The next message should be sended, so the system doesn’t get halted

Any ideas ?

Thanks in advantage

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 6, 2006, 8:47am UTC](https://www.ruby-forum.com/t/exception-in-for-next-item/56352/2 "2006-01-06T08:47:32Z")

</div>

I have pinpointed the problem what happens is this :

daniel-wijnands-powerbook58:~/Desktop/mailing danielwijnands$ ruby  
script/runner ‘Spool.spool’  
General spool error Net::SMTPServerBusy 450 \<pietitxl.nl\>: User unknown  
in local recipient table  
General spool error Net::SMTPSyntaxError 503 Error: nested MAIL command  
General spool error Net::SMTPSyntaxError 503 Error: nested MAIL command

The problem is that i’m sending multiple messages.  
When te exception occurs ( Net::SMTPServerBusy 450 ) the server still  
has the message open.  
When i send another message it says Nested Mail command.

Is it possible to reset the connnection or the previous command ?

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [January 6, 2006, 8:53am UTC](https://www.ruby-forum.com/t/exception-in-for-next-item/56352/3 "2006-01-06T08:53:57Z")

</div>

Okay i have a solution. but i think it could be formed nicer :

Net::SMTP.start(‘mailserver’,25,‘domain’) do | smtp |  
for message in messages  
begin  
smtp.send\_message message.mime\_content, message.email\_from  
,message.email\_to  
message.destroy  
rescue Exception =\> exc  
message.state = ‘ERROR’  
message.save  
smtp.finish  
smtp = smtp.start(‘domain’)  
STDERR.puts “General spool error #{exc.class} #{exc.message}”  
next  
end  
end  
end  
end
