Can anyone see whats wrong with my code?

Can someone please tell me how to close the socket so that I can send consecutive emails.

Thanks,
Kim

#class auto

#define TCPCONFIG 1

#define FROM “myaddress@mydomain.com
#define TO “myaddress@mydomain.com
#define SUBJECT “You’ve got mail!”
#define BODY "Visit the Rabbit Semiconductor web site.
"
“There you’ll find the latest news about Dynamic C.”

#define SMTP_SERVER “mymailserver.mydomain.com

#define SMTP_VERBOSE

#memmap xmem
#use dcrtcp.lib
#use smtp.lib

// global variables
int testVal;
tcp_Socket s;

// function declaration
void SendM();

void main()
{
testVal = 0;
// set up port E as input
WrPortI(PEDDR, &PEDDRSHADOW, 0xFF);

while (1)
{
    testVal = RdPortI(PEDR);
    testVal = (testVal & 0x30);
     if (testVal == 0x10)
     {
SendM();
     }
}

}

void SendM()
{
sock_init();
// Wait for the interface to come up
while (ifpending(IF_DEFAULT) == IF_COMING_UP)
{
tcp_tick(NULL);
}

smtp_sendmail(TO, FROM, SUBJECT, BODY);
while(smtp_mailtick()==SMTP_PENDING) 
     continue;

if(smtp_status()==SMTP_SUCCESS)
    printf("Message sent

");
else
printf("Error sending message
");

sock_close( &s);

// if the following line is commented out it says Error Opening socket 
//  when I attempt to send a second email.  When the line is left in, 
// the program hangs in the while loop.
while (tcp_tick(&s) != NULL);

}

I think sock_init() gets called once to initialize the settings in (#define TCPCONFIG 1).

Then it’s tcp_tick() to re-send or tcp_open() to use after a tcp_close().

You might try putting sock_init() in main before the loop and get rid of close. Something like that…

Thank you very much Zeeb, that did it.