Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sending Email Asynchronously
Message
De
19/01/2013 06:59:42
 
 
À
19/01/2013 06:15:21
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01563134
Message ID:
01563346
Vues:
39
>>Hope you enjoyed your beer.
>>
>>OK. Not sure where you are getting the ManifestImportList from but assuming something like this:
public class EMailer
>>  {
>>    public List<string> ManifestImportList { get; set; }
>>
>>    public int PostAWBs()
>>    {
>>      object numRemainingLock = new object();
>>      int numRemaining = ManifestImportList.Count;
>>      using (ManualResetEvent waitHandle = new ManualResetEvent(numRemaining == 0))
>>      {
>>        foreach (string ManifestImport in ManifestImportList)
>>        {
>>          SmtpClient client = new SmtpClient();
>>          MailMessage msg = new MailMessage();
>>
>>          // Build the message
>>          if (true)
>>          {
>>            try
>>            {
>>              string userState = string.Empty;
>>              client.SendCompleted += delegate
>>              {
>>                client.Dispose();
>>                lock (numRemainingLock)
>>                {
>>                  if (--numRemaining == 0)
>>                  {
>>                    waitHandle.Set();
>>                  }
>>                }
>>              };
>>              client.SendAsync(msg, userState); // Send our email.   
>>            }
>>            catch (Exception)
>>            {
>>              msg.Dispose();
>>            }
>>          }
>>        }
>>        waitHandle.WaitOne();
>>      }
>>      return 6; //Use whatever meaningful value
>>    }
>>  }
(I've just made ManifestImport a string for simplicity) then you can call it like this:
  internal class Program
>>  {
>>    public delegate int AsyncMethodCaller();
>>
>>    private static void Main(string[] args)
>>    {
>>      EMailer p = new EMailer();
>>      p.ManifestImportList = new List<string> { "One", "Two" };
>>
>>      AsyncMethodCaller caller = new AsyncMethodCaller(p.PostAWBs);
>>      IAsyncResult result = caller.BeginInvoke(new AsyncCallback(CallbackMethod), "Test");
>>      Console.ReadLine();
>>    }
>>
>>    public static void CallbackMethod(IAsyncResult ar)
>>    {
>>      AsyncResult result = (AsyncResult)ar;
>>      AsyncMethodCaller caller = (AsyncMethodCaller)result.AsyncDelegate;
>>      int i = caller.EndInvoke(ar);
>>    }
>>  }
Disclaimer: haven't tested because I didn't have time to actually configure the SmtpClient :-{
>
>
>Viv,
>
>I really appreciate all the time you have spent helping me out over the past weeks. It has been fantastic and over and above the call of duty.

No problem. Maybe you could send me a bit of your weather in exchange :-}

>I believe that it is working properly now (just in the middle of testing it).
>
>One question. If the user starts this process then exits my application, will the emailing process continue until it is finished sending all the emails?

No.(at least I'm pretty sure that's the case). You could set a global 'emails pending' flag when you start the send, reset it on the callback and then check the value to prevent the app closing if still true ?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform