Solved

Help with SMTP mailer

Posted on 2008-06-18
2
928 Views
Last Modified: 2011-03-10
Experts,

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());

            }

            else

            {

                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("alerts@mydomain.com",

               "alerts@activesos.com",

            System.Text.Encoding.UTF8);

            // 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

            SendCompletedEventHandler(SendCompletedCallback);

            // 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);

            Thread.Sleep(10000);

            message.Dispose();

            Console.WriteLine("Goodbye.");

        }
 
 
 
 
 
 

        #endregion
 
 

    }

}

Open in new window

0
Comment
Question by:mhouldridge
2 Comments
 
LVL 8

Accepted Solution

by:
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

http://www.systemnetmail.com/faq/4.6.aspx

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

use
client.Send(message);
message.Dispose();
not
client.SendAsync(message, userState);
0
 

Author Closing Comment

by:mhouldridge
ID: 31468531
Absolutely fantastic!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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: …

757 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