Service crashes all the time

Sep 27, 2011 at 10:29 PM

Nice application in here.

Just installed the last version and I mange to send one email. And then the service crashes when I try to send the second email.

In event viewer I have this:

Application: SES_Server.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException
Stack:
   at System.Net.Sockets.TcpClient.GetStream()
   at SES_Server.SMTPSession.ReadComplete(System.IAsyncResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr)
   at System.Net.ContextAwareResult.CompleteCallback(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Net.ContextAwareResult.Complete(IntPtr)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(System.Object, IntPtr)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)

Here is my scenario:

#1. Start the service

#2. Send one email with

         string server = SMTPServer.Text;
         string port = SMTPPort.Text;

         string username = SMTPUserName.Text;
         
         if(!String.IsNullOrEmpty(SMTPLoginDomain.Text))
         {
            username = String.Join(@"\", new string[] { SMTPLoginDomain.Text, SMTPUserName.Text });
         }

         string password = SMTPPassword.Text;
         string from = From.Text;
         string to = To.Text;

         if (!String.IsNullOrEmpty(from) &&
            !String.IsNullOrEmpty(to) &&
            !String.IsNullOrEmpty(server) &&
            !String.IsNullOrEmpty(port))
         {
            SmtpClient client = new SmtpClient(server, int.Parse(port));
            //client.Credentials = new System.Net.NetworkCredential(username, password);
            MailMessage mailMessage = new MailMessage(from, to);
            mailMessage.Subject = "Test Email";
            mailMessage.Body = "Hello, this is a test email from amazon. Please ignore.";

            try
            {
               client.Send(mailMessage);

               Response.Write("Success!!!");
            }
            catch (SmtpException exception)
            {
               Response.Write(String.Format("Cannot send mail. Status Code {0}. Details:{1}", exception.StatusCode, exception.Message));
            }
         }

#3. When I run the above code the email is delivered, but SES_Server.exe is using a lot of CPU resources.

#4. Email is delivered with success.

#5. I run the same code, with the same parameters like the first time and I get an exception with GeneralFailure and Failure sending mail and the service crashes with the above error message from event viewer.

Looks to me that there is some bug somewhere. Can you fix it ?

/sebastian

Oct 5, 2011 at 11:41 AM
Edited Oct 5, 2011 at 11:41 AM

Hi Sebastian,

I'm having the same problem and looking at the code it appears it's guaranteed to fail after eight seconds of inactivity because of this:

while (!session.transmissionComplete)
{
  System.Threading.Thread.Sleep(100);
  i++;
  if (i >= 80)
  {
    session.recievedData = "SENDNOQUIT";
    session.transmissionComplete = true;
  }
}

"SENDNOQUIT" is picked up later on and triggers an attempt at sending which fails when there's no data to process.

Good luck, T.

 

Coordinator
Oct 15, 2011 at 4:35 AM

Hi Sebastian and thoqbk,

Sorry it took so long to reply.

When testing the service with the .NET SMTPClient class, you should keep in mind that it doesn't send a QUIT command (http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/1c3d182f-6ec5-4a6d-a0b7-cf93f48e7a40), which leaves most SMTP services 'hanging' waiting for the client to QUIT. This is the main reason that I added a "SENDNOQUIT" command which automatically closes the connect after a certain amount of time. 

After the first email is sent to the service, the service sends the email and waits for a quit command. When it doesn't receive the QUIT command and you send a second email it can crash the service. 

As thoqbk mentioned, the "SENDNOQUIT" command used to crash, as there was no data. The latest version has an increased timeout of 1200 and no longer crashes after the timeout period has been reached, it just closes the connection.

Lately I haven't had much time to work on the service, but in the next short while I plan to work more on this project and make a number of improvements.

Once I have done this I will upload the new source code and an improved version of the application to the CodePlex.

Best Regards,

ComputerOne

Nov 19, 2011 at 2:09 AM

I am getting the same error as the first poster.

Nov 24, 2011 at 5:31 PM

Hey Sebastian,

 I just posted a solution to this in the other thread. Hope it helps.

Best,

Robert