0

I hope you can help me with this problem. When I click a Email button, it takes few seconds to process before sending successfully. I need to put process image on but how to implement with "while" loop for processing.
Here is code: i think i should implement with client.Send(msg); for processing. How does it work? I will appreciate your example code. Thanks! (I am using C# and WPF)

private void BtnSendEmailClick(object sender, RoutedEventArgs e)
{
        SmtpClient client = null;
        MailMessage msg = null;

        try
        {

            msg = new MailMessage
            {
                From = new MailAddress("me@hotmail.com", "Me Hotmail")
            };

            msg.To.Add(txtEmailAddress.Text);


            msg.Priority = MailPriority.High;
            msg.Subject = "Blah blah";


            msg.Body =

                "<!DOCTYPE html><html lang='en' xmlns='http://www.w3.org/1999/xhtml'>" +
                "<head> </head>" +
                "<body>" +
                "<h3>Message</h3>" +
                "<p>" + lblEmailMessage.Content + "</p>" +
                "</body>" +
                "</html>";

            msg.IsBodyHtml = true;

            client = new SmtpClient
            {
                Host = "smtp.live.com",
                Port = 25,
                EnableSsl = true,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("me@hotmail.com", "password"),
                DeliveryMethod = SmtpDeliveryMethod.Network
            };

              //how to implement while loop for processing
              client.Send(msg);

              lblMessage.Content = "Successfully sent to your Mail!";

        }
        catch (Exception ex)
        {
            lblMessage.Content = ex.Message;

        }

    }
Jeremy Wiebe
  • 3,894
  • 22
  • 31
user1358072
  • 447
  • 2
  • 12
  • 26
  • See this [post](http://stackoverflow.com/questions/1964839/jquery-please-wait-loading-animation) Maybe could help you. – Wilfredo P Jun 20 '13 at 15:57
  • @Freak_Droid That question is related to jQuery and the web and is not applicable to .NET and WPF. – Jeremy Wiebe Jun 21 '13 at 21:23
  • @JeremyWiebe Ok thanks for the correction, But at the moment that the question was posted I did not see nothing about here WPF. Cheers – Wilfredo P Jun 22 '13 at 10:36

2 Answers2

1

You will need to return instantly, and use a BackgroundWorker to send the email. Once it is finished, you can use the Dispatcher to notify the GUI. See http://msdn.microsoft.com/en-us/magazine/cc163328.aspx for more details.

See also How to use WPF Background Worker.

Community
  • 1
  • 1
Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
  • 2
    If you use .net 4+ I would prefer the Task API to the BackgroundWorker, but the idea is the same. – jods Jun 20 '13 at 16:06
0

Are you wanting to display an image to indicate that the program is doing something (progress indicator)?

In that case you need to do the work of sending the email on a separate thread. You can do this in several ways. The easiest (if you are using .NET 4) is to use the await and async keywords. Simply display the image before starting your work. You'll need to extract the code that actually sends the email into a new method also. Like this:

private void BtnSendEmailClick(object sender, RoutedEventArgs e)
{
    // Display progress image
    progressImage.Visibility = System.Windows.Visibility.Visible;

    string errorMessage = await SendEmail();

    if (errorMessage == null) 
    {
        lblMessage.Content = "Successfully sent to your Mail!";
    }
    else
    {
        lblMessage.Content = errorMessage;
    }

    progressImage.Visibility = System.Windows.Visibility.Hidden;
}

private async Task<string> SendEmail()
{
    try
    {
        var msg = new MailMessage
        {
            From = new MailAddress("me@hotmail.com", "Me Hotmail")
        };

        msg.To.Add(txtEmailAddress.Text);
        msg.Priority = MailPriority.High;
        msg.Subject = "Blah blah";
        msg.Body =
            "<!DOCTYPE html><html lang='en' xmlns='http://www.w3.org/1999/xhtml'>" +
            "<head> </head>" +
            "<body>" +
            "<h3>Message</h3>" +
            "<p>" + lblEmailMessage.Content + "</p>" +
            "</body>" +
            "</html>";
        msg.IsBodyHtml = true;

        var client = new SmtpClient
        {
            Host = "smtp.live.com",
            Port = 25,
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential("me@hotmail.com", "password"),
            DeliveryMethod = SmtpDeliveryMethod.Network
        };

        //how to implement while loop for processing
        client.Send(msg);

        return "Successfully sent to your Mail!";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

One final note: Be sure that you don't touch any UI components in your 'async' method as there is no guarantee that the code will be running on the UI thread. If you need more information returned from the async method you'll have to create a custom structure to contain the result and return it from that SendEmail() method.

Jeremy Wiebe
  • 3,894
  • 22
  • 31
  • hey thanks for your code but it is not working. before i run it, i set the process image's visibility hidden on UI. Then i clicked the button, the process image doesnt display. i dont understand why it is not working because you already set process image visible in code. it should be supposed to display. – user1358072 Jun 21 '13 at 10:27
  • Oops, my code was wrong. I was using a WinForms property (.Visibile) instead of the WPF version (.Visibility). That should work now. – Jeremy Wiebe Jun 21 '13 at 17:10