void FlushCommandQueue (MimeMessage message, MailboxAddress sender, IList<MailboxAddress> recipients, CancellationToken cancellationToken)
{
if (queued.Count == 0)
return;
try {
var responses = new List<SmtpResponse> ();
Exception rex = null;
int count = 0;
int rcpt = 0;
// Note: queued commands are buffered by the stream
Stream.Flush (cancellationToken);
// Note: we need to read all responses from the server before we can process
// them in case any of them have any errors so that we can RSET the state.
try {
for (int i = 0; i < queued.Count; i++)
responses.Add (Stream.ReadResponse (cancellationToken));
} catch (Exception ex) {
// Note: save this exception for later (it may be related to
// an error response for a MAIL FROM or RCPT TO command).
rex = ex;
}
for (int i = 0; i < responses.Count; i++) {
switch (queued[i]) {
case SmtpCommand.MailFrom:
ProcessMailFromResponse (message, sender, responses[i]);
break;
case SmtpCommand.RcptTo:
if (ProcessRcptToResponse (message, recipients[rcpt++], responses[i]))
count++;
break;
}
}
if (count == 0)
OnNoRecipientsAccepted (message);
if (rex != null)
throw new SmtpProtocolException ("Error reading a response from the SMTP server.", rex);
} finally {
queued.Clear ();
}
}