Arduino Mega, freezezs when connecting to server

I am using an Arduino Mega with a Ethernet Shield.

I have a program that datalogs and sends emails out every minute.
I can collect data and send out email once then on second loop through it freezes at connecting and just hangs there.

Any ideas?

Any ideas?

I have one.

Post your code.

sorry for the delay,

Here is how i am sending the emails with attachments. The code it actually in is alot bigger but the email sending is the same principal.

#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>

char Sfile[]= {"S00DATA.csv"};

char filetype[] = {"csv"};
byte slave_address = 2;
byte SLAVE_END = 3;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x6D, 0xD6 }; // Arduino's artificial mac address
byte ip[] = { 0, 0, 0, 0 }; // my ip
byte server[] = { 0, 0, 0, 0 }; // my smtp server ip
int time = 5000;
int wait = 2000;

File datatxt;
Client client(server, 25);

static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const int chipSelect = 4;

char Sender_email[] ={"blah@gmx.com"};

void setup()
{
 delay(time);
 Ethernet.begin(mac, ip);
 Serial.begin(9600);
 delay(1000);
 pinMode(10,OUTPUT); 

 Serial.print("Initializing SD card...");
 
 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

 

}

void loop()
{
   Serial.println("connecting...");
 
 if (client.connect()) {
                        
                           
                        Serial.println("connected");
   
                        client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/
                          delay(wait); /* wait for a response */

                        client.println("MAIL From: blah@gmx.com"); /* identify sender, this should be the same as the smtp server you are using */
                          delay(wait); /* wait for a response */

                        client.println("RCPT To:blahe@virelec.com"); /* identify recipient */
                          delay(wait); /* wait for a response */

                        client.println("DATA");
                          delay(wait); /* wait for a response */
                        
                        client.println("Subject: text file attached");
                        
                        client.print("Content-type: multipart/mixed; boundary=\"$boundary\" \r\n");
                        
                        while(slave_address <= SLAVE_END){
                                add_attachment(slave_address);
                                slave_address++;
                        }
                                               
                        client.println("."); /* end email */
                        client.println("QUIT"); /* terminate connection */
                        delay(wait); /* wait for a response */
                        
 
  
   
 } else {
   Serial.println("connection failed");
 }
  
  
  
 while (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   slave_address = 0; 
   delay(300000);
   
 }
}




void encodeblock(unsigned char in[3],unsigned char out[4],int len) {
  out[0]=cb64[in[0]>>2]; 
  out[1]=cb64[((in[0]&0x03)<<4)|((in[1]&0xF0)>>4)];
  out[2]=(unsigned char) (len>1 ? cb64[((in[1]&0x0F)<<2)|((in[2]&0xC0)>>6)] : '=');
  out[3]=(unsigned char) (len>2 ? cb64[in[2]&0x3F] : '=');
}

void encode() {
  unsigned char in[3],out[4]; 
  int i,len,blocksout=0;
  
  while (datatxt.available()!=0) {
    len=0; 
      for (i=0;i<3;i++){ 
            in[i]=(unsigned char) datatxt.read(); 
                if (datatxt.available()!=0) len++; 
                      else in[i]=0;
      }
      if (len){
          encodeblock(in,out,len); 
           for(i=0;i<4;i++) client.write(out[i]); 
                blocksout++; }
      if (blocksout>=19||datatxt.available()==0){ 
          if (blocksout) client.print("\r\n");  blocksout=0; 
      }
   }
}



void add_attachment(byte slave){
                                              
            client.print("--$boundary\r\n");                     
            Sfile[1] =(slave/10 + 48);
            Sfile[2] =(slave%10 + 48);
            datatxt =SD.open(Sfile,FILE_READ); 
            client.print("Content-Type: ");
            client.print(filetype);
            client.print("; ");
            client.print("name=");
            client.println(Sfile);
            client.print("Content-Disposition: attachment; ");
            client.print("filename=");
            client.println(Sfile);
            client.println("Content-Transfer-Encoding: base64");  
            client.println("");        
             
            encode();
            datatxt.close();
            delay(wait);                        
}

it sends an email a few times then halfway through it seems to be cutting out and starting again.
SERIAL MONITOR:

disconnecting.
connecting...
connected
220 to5email2.gprs.rogers.com ESMTP server (InterMail vM.7.09.00.04 201-2219-102-106-20090410) ready Wed, 24 Aug 2011 14:33:47 -0400
250 to5email2.gprs.rogers.com
250 Sender <blah@gmx.com> Ok
250 Recipient <bla@virelec.com> Ok
354 Ok Send data ending with <CRLF>.<CRLF>
250 Message received: 20110824183353.IFDQ13635.to5email2.gprs.rogers.com@itismeletschat
221 to5email2.gprs.rogers.com ESMTP server closing connection

disconnecting.
connecting...
connected
220 to5email2.gprs.rogers.com ESMTP server (InterMail vM.7.09.00.04 201-2219-102-106-20090410) ready Wed, 24 Aug 2011 14:39:00 -0400
250 to5email2.gprs.rogers.com
250 Sender <blao@gmx.com> Ok
250 Recipient <bla@virelec.com> Ok
354 Ok Send data ending with <CRLF>.<CRLF>
connecting...
connection failed
connecting...
connection failed
connecting...
connection failed