SmtpBeginSend Method |
Note: This API is now obsolete.
Namespace: MailBee.SmtpMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use SendAsync instead.")] public IAsyncResult BeginSend( string senderEmail, EmailAddressCollection recipients, AsyncCallback callback, Object state )
Exception | Condition |
---|---|
MailBeeInvalidStateException | There is already an operation in progress. |
// To use the code below, import MailBee namespaces at the top of your code. using MailBee; using MailBee.SmtpMail; using MailBee.DnsMX; using MailBee.Mime; // Put the code below inside your class. // MessageSubmittedToServer event handler. private void OnMessageSubmittedToServer(object sender, SmtpMessageSubmittedToServerEventArgs e) { // If no recipients received the message, this event won't be raised. // It's safe to assume AcceptedRecipients collection is not empty. string domain = e.AcceptedRecipients[0].GetDomain(); MessageBox.Show("Message was sent to recipients at " + domain + " domain"); } // MessageRecipientSubmitted event handler. private void OnMessageRecipientSubmitted(object sender, SmtpMessageRecipientSubmittedEventArgs e) { // If a particular recipient was refused, let the user know. if (!e.Result) { MessageBox.Show("For e-mail address " + e.RecipientEmail + ", server responded: " + e.ServerStatusMessage); } } // Send callback function. private void SendCallback(IAsyncResult result) { Smtp mailer = (Smtp)result.AsyncState; mailer.EndSend(); MessageBox.Show("The e-mail was sent to: " + mailer.GetAcceptedRecipients().ToString()); } // The actual code. private void Form1_Load(object sender, System.EventArgs e) { Smtp mailer = new Smtp(); // Enable logging SMTP session into a file. mailer.Log.Enabled = true; mailer.Log.Filename = @"C:\Temp\log.txt"; mailer.Log.Clear(); mailer.Log.Format = LogFormatOptions.AddContextInfo; // Get the list of DNS servers for MX lookup from OS settings // (app.config/web.config/machine.config files are ignored). mailer.DnsServers.Autodetect( DnsAutodetectOptions.Registry | DnsAutodetectOptions.Wmi); // Subscribe to events. mailer.MessageSubmittedToServer += new SmtpMessageSubmittedToServerEventHandler(OnMessageSubmittedToServer); mailer.MessageRecipientSubmitted += new SmtpMessageRecipientSubmittedEventHandler(OnMessageRecipientSubmitted); mailer.From.Email = "jdoe@domain.com"; mailer.Subject = "Important details"; // Demonstrate different methods of adding To, CC, and BCC recipients. mailer.To.AsString = "kathy@domain.com, \"Bill Smith, Jr.\" <b.smith@company.com>"; mailer.Cc.Add("Peter", "peter@domain.com"); mailer.Cc.Add("john@domain.com"); mailer.Bcc.AsString = "echelon@secret.com"; // Specify HTML body. mailer.BodyHtmlText = "<html>Here are the details.</html>"; // Create plain-text version. mailer.Message.MakePlainBodyFromHtmlBody(); // Initiate an asynchronous send mail attempt. mailer.BeginSend(null, null, new AsyncCallback(SendCallback), mailer); }