Solved

Help with SMTP mailer

Posted on 2008-06-18
2
933 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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