Solved

Sending Emails Asynchronously

Posted on 2009-04-09
2
308 Views
Last Modified: 2013-12-17
Hello,

I am trying to implement some sample code provided at http://www.eggheadcafe.com/articles/20030720.asp .  However, I cannot get the actually threading to execute.

If I simply call it synchronously, the code works fine.  

Please advise.

Trumpman
namespace addmik.massmail.send

{

    

    

    public class sendEmail

    {

        private string htmlFilePath;

        public string HtmlFilePath

        {

            get

            {

                return htmlFilePath;

            }

            set

            {

                htmlFilePath = value;

            }

        }

        private string subject;

        

        public string Subject

        {

            get

            {

                return subject;

            }

            set

            {

                subject = value;

            }

        }
 

        private string smtpServer;

        public string SmtpServer

        {

            get

            {

                return smtpServer;

            }

            set

            {

                smtpServer = value;

            }

        }
 

        private string fromEmail;

        public string FromEmail

        {

            get

            {

                return fromEmail;

            }
 

            set

            {

                fromEmail = value;

            }

        }
 

        private string emailBody;

        public string EmailBody

        {

            get

            {

                return emailBody;

            }
 

            set

            {

                emailBody = value;

            }

        }
 
 

        public string strBody = String.Empty;
 

        public string SendEmail(string name, string emailAddress)

        {

           /* 

            if (strBody == String.Empty)

            {

                try

                {

                    using (StreamReader reader = new StreamReader(this.HtmlFilePath))

                    {

                        strBody = reader.ReadToEnd();

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception("error reading HTML File" + ex.Message);

                }

            }*/

            try

            {

                MailMessage Message = new MailMessage();

                Message.BodyFormat = MailFormat.Html;

                Message.To = emailAddress;

                Message.From = this.FromEmail;

                Message.Subject = this.Subject;

                Message.Body = this.EmailBody;

                SmtpMail.SmtpServer = this.SmtpServer;

                SmtpMail.Send(Message);

            }

            catch (System.Web.HttpException ehttp)

            {

                throw new Exception("Send error" + ehttp.Message);

            }

            return "sent" + name;

        }

        public delegate string SendEmailDelegate(string name, string emailAddress);
 

        public void GetResultsOnCallback(IAsyncResult ar)

        {
 

            SendEmailDelegate del = (SendEmailDelegate)

             ((AsyncResult)ar).AsyncDelegate;

            try

            {

                string result;

                result = del.EndInvoke(ar);

                Debug.WriteLine("\nOn CallBack: result is " + result);

            }

            catch (Exception ex)

            {

                

                Debug.WriteLine("\nOn CallBack, problem occurred: " + ex.Message);

            }

        }
 

        public string SendEmailAsync(string name, string emailAddress)

        {
 

           //this function executes

             this.SendEmail(name, emailAddress);

            

           // the following lines do not execute

            SendEmailDelegate dc = new SendEmailDelegate(this.SendEmail);

            

            AsyncCallback cb = new AsyncCallback(this.GetResultsOnCallback);

            IAsyncResult ar = dc.BeginInvoke(name, emailAddress, cb, null);

            

            return "ok";

        }

    } // end class sendEmail

}

Open in new window

0
Comment
Question by:trumpman
2 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 24113989
0
 

Author Comment

by:trumpman
ID: 24207752
Thank you.  Sorry for the delay in response.  Am I correct to assume from this link that if decided to use this class to send an email blast to several hundred addresses at once..it would queue appropriately..keep the system from bogging down while all the emails are sent?  I believe that is what the code is doing.  Can you confirm?

- trumpman
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now