当前位置: 首页>>代码示例>>C#>>正文


C# SmtpClient.SendAsyncCancel方法代码示例

本文整理汇总了C#中System.Net.Mail.SmtpClient.SendAsyncCancel方法的典型用法代码示例。如果您正苦于以下问题:C# SmtpClient.SendAsyncCancel方法的具体用法?C# SmtpClient.SendAsyncCancel怎么用?C# SmtpClient.SendAsyncCancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Mail.SmtpClient的用法示例。


在下文中一共展示了SmtpClient.SendAsyncCancel方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        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:");
            string username = Console.ReadLine();

            Console.WriteLine("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("[email protected]"),
                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();
        }
开发者ID:guao2012,项目名称:smtpapi-csharp,代码行数:57,代码来源:Program.cs

示例2: Run

        public static void Run()
        {
            try
            {

                string senderEmailAddress = "[email protected]";
                // Command line argument must the the SMTP host.
                using (SmtpClient client = new SmtpClient("smtp.aliyun.com", 25))
                {
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential(senderEmailAddress, "exception_monitor");
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

                    MailAddress from = new MailAddress(senderEmailAddress, "yqz",
                    System.Text.Encoding.UTF8);

                    MailAddress to = new MailAddress("[email protected]");
                    // Specify the message content.
                    MailMessage message = new MailMessage(from, to);
                    message.Body = "This is a test e-mail message sent by an application. ";
                    // 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 = "test message 1" + someArrows;
                    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 message1";
                    client.SendAsync(message, userState);
                    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,
                    // then cancel the pending operation.
                    if (answer.StartsWith("c") && mailSent == false)
                    {
                        client.SendAsyncCancel();
                    }
                    // Clean up.
                    message.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Goodbye.");
        }
开发者ID:yinqingzhun,项目名称:consoleApp,代码行数:52,代码来源:SimpleAsynchronousExample.cs

示例3: Add

        public void Add()
        {
            SmtpClient client = new SmtpClient("");
            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]",
               "Jane " + (char)0xD8+ " Clayton",
            System.Text.Encoding.UTF8);
            // Set destinations for the e-mail message.
            System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("[email protected]");
            // Specify the message content.
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
            message.Body = "This is a test e-mail message sent by an application. ";
            // 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 = "test message 1" + someArrows;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            // Set the method that is called back when the send operation ends.
            client.SendCompleted += new
            SendCompletedEventHandler(sendCompleted);
            // 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 message1";
            client.SendAsync(message, userState);
            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,
            // then cancel the pending operation.

            if (answer.StartsWith("c") && mailSent == false)
            {
                client.SendAsyncCancel();
            }
            // Clean up.
            message.Dispose();
            Console.WriteLine("Goodbye.");
        }
开发者ID:netngn,项目名称:MailDeluxe,代码行数:41,代码来源:SendMail.cs

示例4: SendEmail

 private static void SendEmail(string username)
 {
     SmtpClient client = new SmtpClient(outEmailServer, outEmailPort);
     client.EnableSsl = true;
     client.UseDefaultCredentials = false;
     client.Credentials = new NetworkCredential(emailUsername, emailPwd);
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.Timeout = 60 * 1000 * 5;
     client.SendAsyncCancel();
     client.SendAsync(emailUsername, username + "@icsd.aegean.gr", "Result announced - Icarus Result Checker", "Hello " + username + ",\nGood/Bad news! I just wanted you to know that an exam result was announced just moments ago! Good luck!\n\nThis is an automated email. Do not reply.\nIcarus Result Checker",tok);
     client.SendCompleted += (sender, args) => {
         Console.WriteLine("Email sent to " + username + "@icsd.aegean.gr");
     };
 }
开发者ID:georgechond94,项目名称:Icarus-ResultChecker,代码行数:14,代码来源:Program.cs


注:本文中的System.Net.Mail.SmtpClient.SendAsyncCancel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。