System.Net.Mail.SmtpClient.SendAsyncCancel C# (CSharp) Method

SendAsyncCancel() public method

public SendAsyncCancel ( ) : void
return void
        public void SendAsyncCancel()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);

            try
            {
                if (!InCall || _cancelled)
                {
                    return;
                }

                _cancelled = true;
                Abort();
            }
            finally
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
            }
        }

Usage Example

Esempio n. 1
0
        public static void Main(string[] args)
        {
            string xmstpapiJson = XsmtpapiHeaderAsJson();

            var client = new SmtpClient
            {
                Port = 587,
                Host = "smtp.sendgrid.net",
                Timeout = 10000,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false
            };

            Console.WriteLine("Enter your SendGrid username:"******"Enter your SendGrid password (warning: password will be visible):");
            string password = Console.ReadLine();

            client.Credentials = new NetworkCredential(username, password);

            var mail = new MailMessage
            {
                From = new MailAddress("*****@*****.**"),
                Subject = "Good Choice Signing Up for Our Service!.",
                Body = "Hi there. Thanks for signing up for Appsterify.ly. It's disruptive! %tag%"
            };

            // add the custom header that we built above
            mail.Headers.Add("X-SMTPAPI", xmstpapiJson);
            mail.BodyEncoding = Encoding.UTF8;

            //async event handler
            client.SendCompleted += SendCompletedCallback;
            const string state = "test1";

            Console.WriteLine("Enter an email address to which a test email will be sent:");
            string email = Console.ReadLine();

            if (email != null)
            {
                // Remember that MIME To's are different than SMTPAPI Header To's!
                mail.To.Add(new MailAddress(email));
                client.SendAsync(mail, state);

                Console.WriteLine(
                    "Sending message... press c to cancel, or wait for completion. Press any other key to exit.");
                string answer = Console.ReadLine();

                if (answer != null && answer.StartsWith("c") && _mailSent == false)
                {
                    client.SendAsyncCancel();
                }
            }

            mail.Dispose();
        }
All Usage Examples Of System.Net.Mail.SmtpClient::SendAsyncCancel