Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sending Email Asynchronously
Message
De
18/01/2013 09:15:36
 
 
À
18/01/2013 08:21:52
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01563134
Message ID:
01563249
Vues:
41
>>>Hi,
>>>
>>>I've got my code to send email successfully using client.Send(msg), however when I try to do it using client.SendAsync(msg, userState) nothing happens.
>>>
>>>I have been following all the examples on the internet like this:
>>>
>>>http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx
>>>
>>>My code runs through fine, no errors, just no email gets sent.
>>
>>Does the callback fire ? If so what's in the AsyncCompletedEventArgs ?
>
>Here's my code, it appears to run through all the data that I expect and then sits at the waitHandle.WaitOne(); line and never goes into the SendCompletedCallback :
>
>
        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 ?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform