private async Task SubmitMail( SmtpServer server, SmtpMail mail, string[] attachments, string bodyText, bool htmlBody, int index) { SmtpClient smtp = null; try { smtp = new SmtpClient(); // add event handler smtp.Authorized += OnAuthorized; smtp.Connected += OnConnected; smtp.Securing += OnSecuring; smtp.SendingDataStream += OnSendingDataStream; UpdateRecipientItem(index, "Preparing ..."); if (!htmlBody) { mail.TextBody = bodyText; } else { string html = bodyText; html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>"; await mail.ImportHtmlAsync(html, Windows.ApplicationModel.Package.Current.InstalledLocation.Path, ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss); } int count = attachments.Length; for (int i = 0; i < count; i++) { await mail.AddAttachmentAsync(attachments[i]); } UpdateRecipientItem(index, string.Format("Connecting {0} ...", server.Server)); smtp.Tag = index; // You can genereate a log file by the following code. // smtp.LogFileName = "ms-appdata:///local/smtp.txt"; IAsyncAction asynCancelSend = smtp.SendMailAsync(server, mail); _cancellationToken.Token.Register(() => asynCancelSend.Cancel()); await asynCancelSend; Interlocked.Increment(ref _success); UpdateRecipientItem(index, "Completed"); } catch (Exception ep) { smtp.Close(); string errDescription = ep.Message; UpdateRecipientItem(index, errDescription); Interlocked.Increment(ref _failed); } }
private async Task SubmitMail( SmtpServer oServer, SmtpMail oMail, string[] atts, string bodyText, bool htmlBody, int index ) { SmtpClient oSmtp = null; try { oSmtp = new SmtpClient(); // oSmtp.TaskCancellationToken = m_cts.Token; // oSmtp.Authorized += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized); oSmtp.Connected += OnConnected; // oSmtp.Securing += new SmtpClient.OnSecuringEventHandler(OnSecuring); //oSmtp.SendingDataStream += // new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream); UpdateRecipientItem(index, "Preparing ..."); if ( !htmlBody ) { oMail.TextBody = bodyText; } else { string html = bodyText; html = "<html><head><meta charset=\"utf-8\" /></head><body style=\"font-family:Calibri;font-size: 15px;\">" + html + "<body></html>"; await oMail.ImportHtmlAsync(html, Windows.ApplicationModel.Package.Current.InstalledLocation.Path, ImportHtmlBodyOptions.ErrorThrowException | ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportHttpPictures | ImportHtmlBodyOptions.ImportCss); } int count = atts.Length; for (int i = 0; i < count; i++) { await oMail.AddAttachmentAsync(atts[i]); } UpdateRecipientItem(index, String.Format("Connecting {0} ...", oServer.Server)); oSmtp.Tag = index; // You can genereate a log file by the following code. // oSmtp.LogFileName = "ms-appdata:///local/smtp.txt"; IAsyncAction asynCancelSend = oSmtp.SendMailAsync(oServer, oMail); m_cts.Token.Register(() => asynCancelSend.Cancel()); await asynCancelSend; Interlocked.Increment(ref m_success); UpdateRecipientItem(index, "Completed"); } catch (Exception ep) { oSmtp.Close(); string errDescription = ep.Message; UpdateRecipientItem(index, errDescription); Interlocked.Increment(ref m_failed); } }