Solved

Help with SMTP mailer

Posted on 2008-06-18
2
929 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

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 (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. 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