# Recovering from errors in net:smtp

**URL:** <https://www.ruby-forum.com/t/recovering-from-errors-in-net-smtp/71724>\
**Category:** Ruby\
**Created:** [October 11, 2006, 2:56am UTC](https://www.ruby-forum.com/t/recovering-from-errors-in-net-smtp/71724 "2006-10-11T02:56:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 11, 2006, 2:56am UTC](https://www.ruby-forum.com/t/recovering-from-errors-in-net-smtp/71724/1 "2006-10-11T02:56:36Z")

</div>

Hi,  
I’ve got a script that sends reports by email using Net::SMTP.  
Occassionally I get errors from the mail server (normally rejected  
addresses) which causes an exception which I catch with rescue. This is  
in a class derived from Net::SMTP:

def send (to, subject, data)  
retries = 0  
@count += 1  
to\_array = to.split(/\s\*,\s\*/)

```
hdrs = <<HDRS

```

To: #{to}  
Subject: #{subject}  
Date: #{Time.now.strftime("%a %b %e %T %Y %z")}  
Message-Id: \<selms-#{@time}-#{@count}@selms\>  
From: #{@from}

HDRS

```
  send_message( hdrs + data.join("\n") + ".\n", @from, *to_array)

```

rescue Net::SMTPFatalError, Net::SMTPSyntaxError  
if $! =~ /virtual alias table/ then  
retries += 1  
STDERR.puts “mail failed #{retries} for #{to}:#{$!}”  
spleep(10)  
retry if retries \<= 2  
end  
STDERR.puts “mail failed for #{to}:#{$!}”  
false  
end

I wish to ‘reset’ the smtp session so that I can continue sending email  
without doing a finish and another start (i.e. before I retry). Is this  
possible? I’ve looked through the docs for net::smtp and not found  
anything obvious. I have not looked at the source yet.

Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 12, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/recovering-from-errors-in-net-smtp/71724/2 "2006-10-12T23:20:56Z")

</div>

Eric H. wrote:

Thanks very much Eric, I’m sure I could have figured this out for myself  
but it would have taken much time and delving into RFCs and source code.

> I haven’t submitted a patch for it yet, but this will do it:

Are you going to or should I submit it on your behalf (with appropriate  
attribution of course 🙂

> ## 
> 
> # Hack in RSET
> 
> class Net::SMTP # :nodoc:
> 
> unless instance\_methods.include? ‘reset’ then  
> ##  
> # Resets the SMTP connection.
> 
> ```
> def reset
> getok 'RSET'
> end
> 
> ```
> 
> end
> 
> end
> 
> –  
> Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [October 11, 2006, 6:49am UTC](https://www.ruby-forum.com/t/recovering-from-errors-in-net-smtp/71724/3 "2006-10-11T06:49:45Z")

</div>

On Oct 10, 2006, at 5:56 PM, Russell F. wrote:

> I wish to ‘reset’ the smtp session so that I can continue sending  
> email  
> without doing a finish and another start (i.e. before I retry). Is  
> this  
> possible? I’ve looked through the docs for net::smtp and not found  
> anything obvious. I have not looked at the source yet.

I haven’t submitted a patch for it yet, but this will do it:

## 

# Hack in RSET

class Net::SMTP # :nodoc:

unless instance\_methods.include? ‘reset’ then  
##  
# Resets the SMTP connection.

```
 def reset
   getok 'RSET'
 end

```

end

end

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
