internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, out SmtpFailedRecipientException exception)
 {
     if (sender == null)
     {
         throw new ArgumentNullException("sender");
     }
     if (recipients == null)
     {
         throw new ArgumentNullException("recipients");
     }
     MailCommand.Send(this.connection, SmtpCommands.Mail, sender.SmtpAddress);
     this.failedRecipientExceptions.Clear();
     exception = null;
     foreach (MailAddress address in recipients)
     {
         string str;
         if (!RecipientCommand.Send(this.connection, this.connection.DSNEnabled ? (address.SmtpAddress + deliveryNotify) : address.SmtpAddress, out str))
         {
             this.failedRecipientExceptions.Add(new SmtpFailedRecipientException(this.connection.Reader.StatusCode, address.SmtpAddress, str));
         }
     }
     if (this.failedRecipientExceptions.Count > 0)
     {
         if (this.failedRecipientExceptions.Count == 1)
         {
             exception = (SmtpFailedRecipientException) this.failedRecipientExceptions[0];
         }
         else
         {
             exception = new SmtpFailedRecipientsException(this.failedRecipientExceptions, this.failedRecipientExceptions.Count == recipients.Count);
         }
         if (this.failedRecipientExceptions.Count == recipients.Count)
         {
             exception.fatal = true;
             throw exception;
         }
     }
     DataCommand.Send(this.connection);
     return new MailWriter(this.connection.GetClosableStream());
 }
Exemplo n.º 2
0
        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));
            }

            if (GlobalLog.IsEnabled && recipients.Count > 0)
            {
                GlobalLog.Assert("SmtpTransport::SendMail()|recepients.Count <= 0");
            }
            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()));
        }
Exemplo n.º 3
0
        internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify,
                                     bool allowUnicode, out SmtpFailedRecipientException?exception)
        {
            ArgumentNullException.ThrowIfNull(sender);
            ArgumentNullException.ThrowIfNull(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(), encodeForTransport: true));
        }
Exemplo n.º 4
0
 internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, out SmtpFailedRecipientException exception)
 {
     if (sender == null)
     {
         throw new ArgumentNullException("sender");
     }
     if (recipients == null)
     {
         throw new ArgumentNullException("recipients");
     }
     MailCommand.Send(this.connection, SmtpCommands.Mail, sender.SmtpAddress);
     this.failedRecipientExceptions.Clear();
     exception = null;
     foreach (MailAddress address in recipients)
     {
         string str;
         if (!RecipientCommand.Send(this.connection, this.connection.DSNEnabled ? (address.SmtpAddress + deliveryNotify) : address.SmtpAddress, out str))
         {
             this.failedRecipientExceptions.Add(new SmtpFailedRecipientException(this.connection.Reader.StatusCode, address.SmtpAddress, str));
         }
     }
     if (this.failedRecipientExceptions.Count > 0)
     {
         if (this.failedRecipientExceptions.Count == 1)
         {
             exception = (SmtpFailedRecipientException)this.failedRecipientExceptions[0];
         }
         else
         {
             exception = new SmtpFailedRecipientsException(this.failedRecipientExceptions, this.failedRecipientExceptions.Count == recipients.Count);
         }
         if (this.failedRecipientExceptions.Count == recipients.Count)
         {
             exception.fatal = true;
             throw exception;
         }
     }
     DataCommand.Send(this.connection);
     return(new MailWriter(this.connection.GetClosableStream()));
 }
Exemplo n.º 5
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);
                }
            }
        }
 private static void SendToCollectionCompleted(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SendMailAsyncResult asyncState = (SendMailAsyncResult)result.AsyncState;
         try
         {
             string str;
             if (!RecipientCommand.EndSend(result, out str))
             {
                 asyncState.failedRecipientExceptions.Add(new SmtpFailedRecipientException(asyncState.connection.Reader.StatusCode, asyncState.toCollection[asyncState.toIndex - 1].SmtpAddress, str));
                 if (asyncState.failedRecipientExceptions.Count == asyncState.toCollection.Count)
                 {
                     SmtpFailedRecipientException exception = null;
                     if (asyncState.toCollection.Count == 1)
                     {
                         exception = (SmtpFailedRecipientException)asyncState.failedRecipientExceptions[0];
                     }
                     else
                     {
                         exception = new SmtpFailedRecipientsException(asyncState.failedRecipientExceptions, true);
                     }
                     exception.fatal = true;
                     asyncState.InvokeCallback(exception);
                     return;
                 }
             }
             if (asyncState.SendToCollection())
             {
                 asyncState.SendData();
             }
         }
         catch (Exception exception2)
         {
             asyncState.InvokeCallback(exception2);
         }
     }
 }
Exemplo n.º 7
0
        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);
                }
            }
        }
Exemplo n.º 8
0
        internal MailWriter SendMail(MailAddress sender, MailAddressCollection recipients, string deliveryNotify, 
            bool allowUnicode, out SmtpFailedRecipientException exception)
        {
            if (sender == null)
            {
                throw new ArgumentNullException("sender");
            }

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

            GlobalLog.Assert(recipients.Count > 0, "SmtpTransport::SendMail()|recepients.Count <= 0");

            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 = (SmtpFailedRecipientException) 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());
        }
Exemplo n.º 9
0
        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 static void SendToCollectionCompleted(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SendMailAsyncResult asyncState = (SendMailAsyncResult) result.AsyncState;
         try
         {
             string str;
             if (!RecipientCommand.EndSend(result, out str))
             {
                 asyncState.failedRecipientExceptions.Add(new SmtpFailedRecipientException(asyncState.connection.Reader.StatusCode, asyncState.toCollection[asyncState.toIndex - 1].SmtpAddress, str));
                 if (asyncState.failedRecipientExceptions.Count == asyncState.toCollection.Count)
                 {
                     SmtpFailedRecipientException exception = null;
                     if (asyncState.toCollection.Count == 1)
                     {
                         exception = (SmtpFailedRecipientException) asyncState.failedRecipientExceptions[0];
                     }
                     else
                     {
                         exception = new SmtpFailedRecipientsException(asyncState.failedRecipientExceptions, true);
                     }
                     exception.fatal = true;
                     asyncState.InvokeCallback(exception);
                     return;
                 }
             }
             if (asyncState.SendToCollection())
             {
                 asyncState.SendData();
             }
         }
         catch (Exception exception2)
         {
             asyncState.InvokeCallback(exception2);
         }
     }
 }