public int PostAWBs() > { > string smtpClient = mmAppBase.AppSettingsMgr.GetSetting("SmtpClient"); > string smtpPort = mmAppBase.AppSettingsMgr.GetSetting("SmtpPort"); > > int smtpPortInt = 25; > bool resultport = Int32.TryParse(smtpPort, out smtpPortInt); > > string smtpFrom = mmAppBase.AppSettingsMgr.GetSetting("SmtpFrom"); > string smtpUser = mmAppBase.AppSettingsMgr.GetSetting("SmtpUser"); > string smtpPassword = mmAppBase.AppSettingsMgr.GetSetting("SmtpPassword"); > string smtpBCC = mmAppBase.AppSettingsMgr.GetSetting("SmtpBCC"); > > string emailTest = mmAppBase.AppSettingsMgr.GetSetting("EmailTest"); > > NetworkCredential cred = new NetworkCredential(smtpUser, smtpPassword); > > // get the Merge Template to use for Notification of Arrival > MergeTemplateEntity mergeTemplate = MergeTemplate.GetMergeTemplateByCode("Arrival"); > > > // get all records from ManifestImport > mmBindingList<ManifestImportEntity> ManifestImportList = this.ManifestImport.GetAllEntities(); > > // for each record > // Insert into AirWayBills > // send email > object numRemainingLock = new object(); > int numRemaining = ManifestImportList.Count; > using (ManualResetEvent waitHandle = new ManualResetEvent(numRemaining == 0)) > { > foreach (ManifestImportEntity ManifestImport in ManifestImportList) > { > AirWayBillEntity awb = this.AirWayBill.NewEntity(); > CustomerEntity customer = this.Customer.GetCustomerByCustomerNumber(ManifestImport.dls_acct.Substring(3)); // we do not store the country code as part of the customer code > > // get the customer primary key > awb.awb_cusfk = customer.cus_PK; > > int profileKey = 0; > bool result = Int32.TryParse(ManifestImport.profile_key, out profileKey); > > awb.awb_keycode = profileKey; > awb.awb_manifestdate = ManifestHeaderImportList[0].man_date; > awb.awb_manifestnumber = ManifestHeaderImportList[0].man_number; > awb.awb_number = ManifestImport.awb; > awb.awb_pieces = ManifestImport.pieces; > awb.awb_value = ManifestImport.value; // need to calculate value > decimal weight = 0; > if (ManifestImport.weight != null) > { > weight = ManifestImport.weight.Value; > } > awb.awb_weight = Math.Ceiling(weight); > awb.awb_freight = ManifestImport.weight; // need to calculate frieght > awb.awb_fuel = 0; // need to calculate fuel > awb.awb_insurance = 0; // need to calculate insurance > awb.awb_shipper = ManifestImport.shipper; > awb.awb_description = ManifestImport.description; > > > using (SmtpClient client = new SmtpClient(smtpClient, smtpPortInt)) > { > client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); > > MailMessage msg = new MailMessage(); > > // Send to all email addresses for this customer > if (EmailAddress.EntityList.Count > 0) > { > foreach (EmailAddressEntity email in EmailAddress.EntityList) > { > if (emailTest == String.Empty) > { > msg.To.Add(email.eml_email); > } > else > msg.To.Add(emailTest); > > } > > if (smtpBCC != String.Empty) > { > msg.Bcc.Add(smtpBCC); > } > > msg.From = new MailAddress(smtpFrom); > msg.Subject = mergeTemplate.mrg_subject; > string msgBody = mergeTemplate.mrg_template; > > msg.Body = String.Format(msgBody, awb.awb_number, awb.awb_shipper, awb.awb_pieces, awb.awb_weight, awb.awb_value, awb.awb_description); > msg.IsBodyHtml = true; > > > client.Credentials = cred; // Send our account login details to the client. > client.EnableSsl = true; > try > { > string userState = String.Format("Sending to {0}", customer.cus_firstname + customer.cus_lastname); > client.SendCompleted += delegate > { > lock (numRemainingLock) > { > if (--numRemaining == 0) > { > waitHandle.Set(); > } > } > }; > client.SendAsync(msg, userState); // Send our email. > //client.Send(msg); > } > catch (Exception) > { > msg.Dispose(); > // throw; > } > } > > } > > } > waitHandle.WaitOne(); > } >// some more code to save things goes here > } > > static bool mailSent = false; > private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) > { > // Get the unique identifier for this asynchronous operation. > String token = (string)e.UserState; > > if (e.Cancelled) > { > Console.WriteLine("[{0}] Send canceled.", token); > } > if (e.Error != null) > { > Console.WriteLine("[{0}] {1}", token, e.Error.ToString()); > } > else > { > Console.WriteLine("Message sent."); > } > mailSent = true; > }Not sure I read the code correctly (I'll take a closer look) but won't the SmtpClient instance be disposed before the callback occurs ?