static void SendMail()
        {
            try
            {
                // Declare msg as MailMessage instance
                MailMessage  msg    = new MailMessage("*****@*****.**", "*****@*****.**", "Test subject", "Test body");
                SmtpClient   client = GetSmtpClient2();
                object       state  = new object();
                IAsyncResult ar     = client.BeginSend(msg, Callback, state);

                Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
                string answer = Console.ReadLine();

                // If the user canceled the send, and mail hasn't been sent yet,
                if (answer != null && answer.StartsWith("c"))
                {
                    client.CancelAsyncOperation(ar);
                }

                msg.Dispose();
                Console.WriteLine("Goodbye.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
示例#2
0
        public static void Run()
        {
            //ExStart: 1
            SmtpClient smtpClient = new SmtpClient();

            smtpClient.Host                = "<HOST>";
            smtpClient.Username            = "******";
            smtpClient.Password            = "******";
            smtpClient.Port                = 587;
            smtpClient.SupportedEncryption = EncryptionProtocols.Tls;
            smtpClient.SecurityOptions     = SecurityOptions.SSLExplicit;

            int messageNumber           = 30;
            List <MailMessage> messages = new List <MailMessage>();

            for (int i = 0; i < messageNumber; i++)
            {
                MailMessage message = new MailMessage(
                    "*****@*****.**",
                    "*****@*****.**",
                    "Test Message - " + Guid.NewGuid().ToString(),
                    "Use disk cache and sending queue in group SMTP send operation");
                messages.Add(message);
            }

            smtpClient.SmtpQueueLocation = @"D:\E\AsposeTestDir\queue";
            int counter = 0;

            smtpClient.SucceededQueueSending += delegate(object sender, MailMessageEventArgs arguments)
            {
                counter++;
            };
            smtpClient.FailedQueueSending += delegate(object sender, MailMessageEventArgs arguments)
            {
                counter++;
            };
            smtpClient.SendToQueue(messages);
            IAsyncResult asyncResult = smtpClient.BeginSendQueue();

            while (counter != messageNumber)
            {
                Thread.Sleep(50);
            }
            smtpClient.CancelAsyncOperation(asyncResult);
            //ExEnd: 1

            Console.WriteLine("UseDiskCacheAndSendingQueue executed successfully.");
        }