Exemple #1
0
 private static void SendDataCompleted(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SendMailAsyncResult thisPtr = (SendMailAsyncResult)result.AsyncState;
         try
         {
             DataCommand.EndSend(result);
             thisPtr._stream = thisPtr._connection.GetClosableStream();
             if (thisPtr._failedRecipientExceptions.Count > 1)
             {
                 thisPtr.InvokeCallback(new SmtpFailedRecipientsException(thisPtr._failedRecipientExceptions, thisPtr._failedRecipientExceptions.Count == thisPtr._toCollection.Count));
             }
             else if (thisPtr._failedRecipientExceptions.Count == 1)
             {
                 thisPtr.InvokeCallback(thisPtr._failedRecipientExceptions[0]);
             }
             else
             {
                 thisPtr.InvokeCallback();
             }
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Exemple #2
0
 internal MailWriter EndSendMail(IAsyncResult result)
 {
     try
     {
         return(SendMailAsyncResult.End(result));
     }
     finally
     {
     }
 }
Exemple #3
0
        private void SendMailCallback(IAsyncResult result)
        {
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(this);
            }
            try
            {
                _writer = _transport.EndSendMail(result);
                // If some recipients failed but not others, send the e-mail anyways, but then return the
                // "Non-fatal" exception reporting the failures.  The sync code path does it this way.
                // Fatal exceptions would have thrown above at transport.EndSendMail(...)
                SendMailAsyncResult sendResult = (SendMailAsyncResult)result;
                // Save these and throw them later in SendMessageCallback, after the message has sent.
                _failedRecipientException = sendResult.GetFailedRecipientException();
            }
            catch (Exception e)
            {
                Complete(e, result);
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Exit(this);
                }
                return;
            }

            try
            {
                if (_cancelled)
                {
                    Complete(null, result);
                }
                else
                {
                    _message.BeginSend(_writer, DeliveryMethod != SmtpDeliveryMethod.Network,
                                       ServerSupportsEai, new AsyncCallback(SendMessageCallback), result.AsyncState);
                }
            }
            catch (Exception e)
            {
                Complete(e, result);
            }
            finally
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Exit(this);
                }
            }
        }
Exemple #4
0
 private static void SendMailFromCompleted(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SendMailAsyncResult thisPtr = (SendMailAsyncResult)result.AsyncState;
         try
         {
             MailCommand.EndSend(result);
             thisPtr.SendToCollection();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Exemple #5
0
        internal static MailWriter End(IAsyncResult result)
        {
            SendMailAsyncResult thisPtr = (SendMailAsyncResult)result;
            object sendMailResult       = thisPtr.InternalWaitForCompletion();

            // Note the difference between the singular and plural FailedRecipient exceptions.
            // Only fail immediately if we couldn't send to any recipients.
            if ((sendMailResult is Exception e) &&
                (!(sendMailResult is SmtpFailedRecipientException) ||
                 ((SmtpFailedRecipientException)sendMailResult).fatal))
            {
                ExceptionDispatchInfoThrower.Throw(e);
            }

            return(new MailWriter(thisPtr._stream));
        }
Exemple #6
0
        internal IAsyncResult BeginSendMail(MailAddress sender, MailAddressCollection recipients,
                                            string deliveryNotify, bool allowUnicode, AsyncCallback callback, object state)
        {
            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            if (recipients == null)
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            SendMailAsyncResult result = new SendMailAsyncResult(_connection, sender, recipients,
                                                                 allowUnicode, _connection.DSNEnabled ? deliveryNotify : null,
                                                                 callback, state);

            result.Send();
            return(result);
        }
Exemple #7
0
        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);
                }
            }
        }