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(); }
public static void Run() { try { string senderEmailAddress = "*****@*****.**"; // 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("*****@*****.**"); // 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."); }
private static async Task SendMailExImplAsync( System.Net.Mail.SmtpClient client, System.Net.Mail.MailMessage message, CancellationToken token) { token.ThrowIfCancellationRequested(); var tcs = new TaskCompletionSource <bool>(); System.Net.Mail.SendCompletedEventHandler handler = null; Action unsubscribe = () => client.SendCompleted -= handler; handler = async(s, e) => { unsubscribe(); // a hack to complete the handler asynchronously await Task.Yield(); if (e.UserState != tcs) { tcs.TrySetException(new InvalidOperationException("Unexpected UserState")); } else if (e.Cancelled) { tcs.TrySetCanceled(); } else if (e.Error != null) { tcs.TrySetException(e.Error); } else { tcs.TrySetResult(true); } }; client.SendCompleted += handler; try { client.SendAsync(message, tcs); using (token.Register(() => client.SendAsyncCancel(), useSynchronizationContext: false)) { await tcs.Task; } } finally { unsubscribe(); } }
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("*****@*****.**", "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("*****@*****.**"); // 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."); }
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"); }; }
public void Cancel() { mClient.SendAsyncCancel(); }
public void SendAsyncCancel() { _smtpClient.SendAsyncCancel(); }