6
votes

I've recently designed a program in C# that will pull information from SQL databases, write an HTML page with the results, and auto-email it out. I've got everything working [sporadically], the problem I'm having is that I seem to be crashing our company's exchange server. After the first few successful runs of the program, I'll start getting this exception:

Base exception: System.Net.Mail.SmtpException: Insufficient system storage. The server response was: 4.3.1 Insufficient system resources

I'm wondering if I should be calling some sort of Dispose()-like method in my mailing? Or if there is any other apparent reason that I would be causing the mail system to stop responding? This affects all clients in our company, not just my code.

This is Exchange 2010, and my code is compiled against .NET 3.5. My attachments are typically 27kb. If I log into the exchange server, it seems that messages just stick in a queue indefinitely. Clearing out the queue (remove without sending NDR) and rebooting the server will get it going again.

The mailing portions look like this (username, password, and address changed):

public void doFinalEmail()
{
     List<string> distList = new List<string>();
     string distListPath = Environment.CurrentDirectory + "\\DistList.txt";
     string aLine;

     logThat("Attempting email distribution of the generated report.");

     if (File.Exists(distListPath))
     {
         FileInfo distFile = new FileInfo(distListPath);
         StreamReader distReader = distFile.OpenText();

         while (!String.IsNullOrEmpty(aLine = distReader.ReadLine()))
         {
             distList.Add(aLine);
         }
     }
     else
     {
         logThat("[[ERROR]]: Distribution List DOES NOT EXIST! Path: " + distListPath);
     }

     MailMessage msg = new MailMessage();
     MailAddress fromAddress = new MailAddress("emailaddresshere");
     msg.From = fromAddress;

     logThat("Recipients: ");
     foreach (string anAddr in distList)
     {
         msg.To.Add(anAddr);
         logThat("\t" + anAddr);
     }
     if (File.Exists(Program.fullExportPath))
     {
         logThat("Attachment: " + Program.fullExportPath);
         Attachment mailAttachment = new Attachment(Program.fullExportPath);
         msg.Attachments.Add(mailAttachment);
         string subj = "Company: " + Program.yestFileName;
         msg.Subject = subj;
         msg.IsBodyHtml = true;
         msg.BodyEncoding = System.Text.Encoding.UTF8;
         sendMail(msg);
     }
     else
     {
         logThat("[[ERROR]]: ATTACHMENT DOES NOT EXIST! Path: " + Program.fullExportPath);
     }
 }

 public void sendMail(MailMessage msg)
 {
     try
     {
         string username = "user"; //domain user
         string password = "pass"; // password
         SmtpClient mClient = new SmtpClient();
         mClient.Host = "192.168.254.11";
         mClient.Credentials = new NetworkCredential(username, password);
         mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
         mClient.Send(msg);
     }
     catch (Exception oops)
     {
         string whatHappened = String.Format("Company: \r\nFailure in {0}! \r\n\r\nError message: {1} \r\nError data: {2} \r\n\r\nStack trace: {3} \r\n\r\nBase exception: {4} \r\nOccuring in method: {5} with a type of {6}\r\n", oops.Source, oops.Message, oops.Data, oops.StackTrace, oops.GetBaseException(), oops.TargetSite, oops.GetType());
         logThat(whatHappened);
         Environment.Exit(1);
     }
 }
3
You might be right -- SmtpClient is IDisposable. So are MailMessage & Attachment.Austin Salonen
Check the amount of space available on your hard drivePhoenixReborn
I will add that I think we've seen that same error message when not properly disposing SmtpClient.Austin Salonen
Hard drive space looks good on the sending server, the exchange server, and the client (myself), I'm also well short of my mailbox size limit. Austin, I don't see any way to call a dispose on my SmtpClient, BUT I notice that I don't have a timeout defined. I wonder if it auto disposes after the timeout period?raney
You can't put your SmtpClient instance in a using block? Or just call mClient.Dispose() ?Chris Dunaway

3 Answers

9
votes

This error can happen when:

  1. The exchange server is out of disk space.
  2. The recipient mailbox is out of disk space.

It is more common to run into issue #2 than issue #1.

Here is a list of Exchange Status Codes and their meanings.

2
votes

To narrow down the issue definitively, you could swap in a different mail client like aspNetEmail (you can get an eval version to test), it wouldn't take but a few lines of code. If the problem persists, it is on the server; if not, it is on the client. I would strongly suspect this is a problem on the server, however, as when the client closes the connection and the message is sent, the server should really not be holding any resources as a result of that connection. You could verify this by looking at your SMTP logs and making sure there was no SMTP error when the connection was closed.

If this is indeed a problem on the server (the SMTP log shows clean disconnection and the problem is replicable with an alternate client), then I would log onto the server (if you can get access), and watch the disk space as jeuton suggests.

1
votes

In exchange 2007 the c: (system drive) needs about 4GB of free disk space. This was our problem. Increment the drive and the mails will flow again.