SMTPClient Message.Send Error - please help!!!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the following code on a company that hosts our web site. I have
noticed lately that their mail server/smtp is unavailable several times a
minute for a second or two. This is causing my script to fail when it calls
the client.Send(msg); - script is below the error message below... Any
ideas????

ERROR MESSAGE
------------------------------------------
Service not available, closing transmission channel. The server response
was: Command timeout, closing transmission channel
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: Service not available,
closing transmission channel. The server response was: Command timeout,
closing transmission channel

Source Error:

Line 51: client.Send(msg);
---------------------------------------------

Code in Page
---------------------------------------------

// create email
MailMessage msg = new MailMessage();
msg.From = new MailAddress("(e-mail address removed));
msg.To.Add(new MailAddress("(e-mail address removed)"));
msg.Subject = "Test Message";
string bod = "<html><head><body><b>Message:</b><br>blah test</body></html>";
msg.Body = bod;
msg.IsBodyHtml = true;

// send email
SmtpClient client = new SmtpClient();
client.Host =
System.Configuration.ConfigurationManager.AppSettings["smtpaddress"];
client.Timeout = 1000000;
client.Send(msg);
---------------------------------------------
 
Yes... That is the problem. My question is when this happens, how can I
prevent my script from failing... Example set a retry attempt or delay and
retry, etc. I can't control their server being too busy - but I don't want my
script to fail.
 
Thanks for the response. Can you provide me with a basic example?


Vadym Stetsyak said:
Hello, Sammy!

you can develop retry logic.
This algorithm can be like this:
-first try, if failure wait for timeout
-second try, if failure wait for incremeneted timeout
-third try....
and so on. number of retries and timeout increment value you can
make configurable.


S> Yes... That is the problem. My question is when this happens, how can
S> I
S> prevent my script from failing... Example set a retry attempt or
S> delay and
S> retry, etc. I can't control their server being too busy - but I don't
S> want my
S> script to fail.

S> "Vadym Stetsyak said:
Hello, Sammy!
Maybe their smtp server is experiencing considerable load and that is
why you receive "service not available" message?
Another way you can follow is ivestigation. Use a sniffer ( e.g.
Ethereal )
to watch network communication, when mail is being sent.
S> I am using the following code on a company that hosts our web
site. I
S> have
S> noticed lately that their mail server/smtp is unavailable several
S> times a
S> minute for a second or two. This is causing my script to fail when
it
S> calls
S> the client.Send(msg); - script is below the error message below...
S> Any
S> ideas????
S> ERROR MESSAGE
S> ------------------------------------------
S> Service not available, closing transmission channel. The server
S> response
was:: Command timeout, closing transmission channel
S> Description: An unhandled exception occurred during the execution
of
S> the
S> current web request. Please review the stack trace for more
S> information about
S> the error and where it originated in the code.
S> Exception Details: System.Net.Mail.SmtpException: Service not
S> available,
S> closing transmission channel. The server response was: Command
S> timeout,
S> closing transmission channel
S> Source Error:
S> Line 51: client.Send(msg);
S> ---------------------------------------------
S> Code in Page
S> ---------------------------------------------
S> // create email
S> MailMessage msg = new MailMessage();
S> msg.From = new MailAddress("(e-mail address removed));
S> msg.To.Add(new MailAddress("(e-mail address removed)"));
S> msg.Subject = "Test Message";
S> string bod = "<html><head><body><b>Message:</b><br>blah
S> test</body></html>";
S> msg.Body = bod;
S> msg.IsBodyHtml = true;
S> // send email
S> SmtpClient client = new SmtpClient();
S> client.Host =
S>
System.Configuration.ConfigurationManager.AppSettings["smtpaddress"];
S> client.Timeout = 1000000;
S> client.Send(msg);
S> ---------------------------------------------
 
Hello, Sammy!

The code can look like this:

int retryCount = 5;
int timeout = 1000*60; //ms
int timeoutInc = 1000*60; //ms
bool sendResult = false;

for(int i = 0; i < retryCount; i++)
{
//this method tries to send an email
if ( SendEmailMessage() )
{ //message successfully sent
sendResult = true;
break;
}
else
{
WaitOnTimeout(timeout); //Sleep(...) ?
timeout += timeoutInc * i;
}
}

if ( sendResult )
{
//report that message successfully sent
}
else
{
//report error while sending message
}


You wrote on Thu, 2 Nov 2006 07:48:01 -0800:

S> Thanks for the response. Can you provide me with a basic example?


S> "Vadym Stetsyak said:
Hello, Sammy!
you can develop retry logic.
This algorithm can be like this:
-first try, if failure wait for timeout
-second try, if failure wait for incremeneted timeout
-third try....
and so on. number of retries and timeout increment value you can
make configurable.
S> Yes... That is the problem. My question is when this happens, how
can
S> I
S> prevent my script from failing... Example set a retry attempt or
S> delay and
S> retry, etc. I can't control their server being too busy - but I
don't
S> want my
S> script to fail.
S> "Vadym Stetsyak" wrote:
S> I am using the following code on a company that hosts our web
site. I
S> have
S> noticed lately that their mail server/smtp is unavailable several
S> times a
S> minute for a second or two. This is causing my script to fail when
it
S> calls
S> the client.Send(msg); - script is below the error message below...
S> Any
S> ideas????
S> ERROR MESSAGE
S> ------------------------------------------
S> Service not available, closing transmission channel. The server
S> response
was:: Command timeout, closing transmission channel
S> Description: An unhandled exception occurred during the execution
of
S> the
S> current web request. Please review the stack trace for more
S> information about
S> the error and where it originated in the code.
S> Exception Details: System.Net.Mail.SmtpException: Service not
S> available,
S> closing transmission channel. The server response was: Command
S> timeout,
S> closing transmission channel
S> Source Error:
S> Line 51: client.Send(msg);
S> ---------------------------------------------
S> Code in Page
S> ---------------------------------------------
S> // create email
S> MailMessage msg = new MailMessage();
S> msg.From = new MailAddress("(e-mail address removed));
S> msg.To.Add(new MailAddress("(e-mail address removed)"));
S> msg.Subject = "Test Message";
S> string bod = "<html><head><body><b>Message:</b><br>blah
S> test</body></html>";
S> msg.Body = bod;
S> msg.IsBodyHtml = true;
S> // send email
S> SmtpClient client = new SmtpClient();
S> client.Host =
S>
System.Configuration.ConfigurationManager.AppSettings["smtpaddress"];
S> client.Timeout = 1000000;
S> client.Send(msg);
S> ---------------------------------------------
 
Back
Top