Help with SMTP mailer

Posted on 2008-06-18
Last Modified: 2011-03-10

I have working the following smtp script, in my C# console application.

At the bottom of the script where the message is sent I've included, Thread.Sleep(10000).  If this isnt there the message is disposed, and it doesnt send.  

My question is - How do I remove the Thread.Sleep(10000), and get the code to end (message.dispose) after the message has been sent?
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());




                Console.WriteLine("Message sent.");


            mailSent = true;


        public static void SendMail(string args, string emailto, string theServer)


            // Command line argument must the the SMTP host.

            SmtpClient client = new SmtpClient(args.ToString());

            // Specify the e-mail sender.

            // Create a mailing address that includes a UTF8 character

            // in the display name.

            MailAddress from = new MailAddress("",



            // Set destinations for the e-mail message.

            MailAddress to = new MailAddress(emailto);

            // Specify the message content.

            MailMessage message = new MailMessage(from, to);

            message.Body = "alert: Server, " + theServer + " (IS DOWN)";

            // Include some non-ASCII characters in body and subject.

            string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });

            message.Body += Environment.NewLine + someArrows;

            message.BodyEncoding = System.Text.Encoding.UTF8;

            message.Subject = "alert";

            message.SubjectEncoding = System.Text.Encoding.UTF8;

            // Set the method that is called back when the send operation ends.

            client.SendCompleted += new


            // The userState can be any object that allows your callback 

            // method to identify this send operation.

            // For this example, the userToken is a string constant.

            string userState = "test message";

            client.SendAsync(message, userState);








Open in new window

Question by:mhouldridge

Accepted Solution

l-cm earned 500 total points
ID: 21816301
You shouldn't need to dispose of your message garbage collection should handle it for you, but if you want you can set an event for when the message has been sent.

client.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);

See this article about sending email asynchronously

If you do need to dispose of the message then just send the message and then dispose it without using SendAsync

client.SendAsync(message, userState);

Author Closing Comment

ID: 31468531
Absolutely fantastic!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

867 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

18 Experts available now in Live!

Get 1:1 Help Now