Solved

Help with SMTP mailer

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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