Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sending Email Asynchronously
Message
From
19/01/2013 06:15:21
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01563134
Message ID:
01563342
Views:
44
>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.

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?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform