internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, bool allowUnicode, out SmtpFailedRecipientException exception) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (recipients == null) { throw new ArgumentNullException(nameof(recipients)); } MailCommand.Send(_connection, SmtpCommands.Mail, sender, allowUnicode); _failedRecipientExceptions.Clear(); exception = null; string response; foreach (MailAddress address in recipients) { string smtpAddress = address.GetSmtpAddress(allowUnicode); string to = smtpAddress + (_connection.DSNEnabled ? deliveryNotify : string.Empty); if (!RecipientCommand.Send(_connection, to, out response)) { _failedRecipientExceptions.Add( new SmtpFailedRecipientException(_connection.Reader.StatusCode, smtpAddress, response)); } } if (_failedRecipientExceptions.Count > 0) { if (_failedRecipientExceptions.Count == 1) { exception = _failedRecipientExceptions[0]; } else { exception = new SmtpFailedRecipientsException(_failedRecipientExceptions, _failedRecipientExceptions.Count == recipients.Count); } if (_failedRecipientExceptions.Count == recipients.Count) { exception.fatal = true; throw exception; } } DataCommand.Send(_connection); return(new MailWriter(_connection.GetClosableStream())); }
private void SendToCollection() { while (_toIndex < _toCollection.Count) { MultiAsyncResult result = (MultiAsyncResult)RecipientCommand.BeginSend(_connection, _toCollection[_toIndex++].GetSmtpAddress(_allowUnicode) + _deliveryNotify, s_sendToCollectionCompleted, this); if (!result.CompletedSynchronously) { return; } string response; if (!RecipientCommand.EndSend(result, out response)) { _failedRecipientExceptions.Add(new SmtpFailedRecipientException(_connection.Reader.StatusCode, _toCollection[_toIndex - 1].GetSmtpAddress(_allowUnicode), response)); } } SendData(); }
private static void SendToCollectionCompleted(IAsyncResult result) { if (!result.CompletedSynchronously) { SendMailAsyncResult thisPtr = (SendMailAsyncResult)result.AsyncState; try { string response; if (!RecipientCommand.EndSend(result, out response)) { thisPtr._failedRecipientExceptions.Add( new SmtpFailedRecipientException(thisPtr._connection.Reader.StatusCode, thisPtr._toCollection[thisPtr._toIndex - 1].GetSmtpAddress(thisPtr._allowUnicode), response)); if (thisPtr._failedRecipientExceptions.Count == thisPtr._toCollection.Count) { SmtpFailedRecipientException exception = null; if (thisPtr._toCollection.Count == 1) { exception = (SmtpFailedRecipientException)thisPtr._failedRecipientExceptions[0]; } else { exception = new SmtpFailedRecipientsException(thisPtr._failedRecipientExceptions, true); } exception.fatal = true; thisPtr.InvokeCallback(exception); return; } } thisPtr.SendToCollection(); } catch (Exception e) { thisPtr.InvokeCallback(e); } } }