public void SendAsync(MailMessage message, object userToken)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, this, "SendAsync", "DeliveryMethod=" + this.DeliveryMethod.ToString());
            }
            try
            {
                if (this.InCall)
                {
                    throw new InvalidOperationException(SR.GetString("net_inasync"));
                }
                if (message == null)
                {
                    throw new ArgumentNullException("message");
                }
                if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    this.CheckHostAndPort();
                }
                this.recipients = new MailAddressCollection();
                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpFromRequired"));
                }
                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        this.recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address2 in message.Bcc)
                    {
                        this.recipients.Add(address2);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address3 in message.CC)
                    {
                        this.recipients.Add(address3);
                    }
                }
                if (this.recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired"));
                }
                try
                {
                    CredentialCache cache;
                    this.InCall = true;
                    this.cancelled = false;
                    this.message = message;
                    this.transport.IdentityRequired = ((this.Credentials != null) && ComNetOS.IsWinNt) && (((this.Credentials is SystemNetworkCredential) || ((cache = this.Credentials as CredentialCache) == null)) || cache.IsDefaultInCache);
                    this.asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (this.DeliveryMethod)
                    {
                        case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                            if (this.EnableSsl)
                            {
                                throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                            }
                            break;

                        case SmtpDeliveryMethod.PickupDirectoryFromIis:
                            if (this.EnableSsl)
                            {
                                throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                            }
                            goto Label_02B2;

                        default:
                            goto Label_0329;
                    }
                    this.writer = this.GetFileMailWriter(this.PickupDirectoryLocation);
                    message.Send(this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network);
                    if (this.writer != null)
                    {
                        this.writer.Close();
                    }
                    this.transport.ReleaseConnection();
                    AsyncCompletedEventArgs arg = new AsyncCompletedEventArgs(null, false, this.asyncOp.UserSuppliedState);
                    this.InCall = false;
                    this.asyncOp.PostOperationCompleted(this.onSendCompletedDelegate, arg);
                    return;
                Label_02B2:
                    this.writer = this.GetFileMailWriter(IisPickupDirectory.GetPickupDirectory());
                    message.Send(this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network);
                    if (this.writer != null)
                    {
                        this.writer.Close();
                    }
                    this.transport.ReleaseConnection();
                    AsyncCompletedEventArgs args2 = new AsyncCompletedEventArgs(null, false, this.asyncOp.UserSuppliedState);
                    this.InCall = false;
                    this.asyncOp.PostOperationCompleted(this.onSendCompletedDelegate, args2);
                    return;
                Label_0329:
                    this.operationCompletedResult = new ContextAwareResult(this.transport.IdentityRequired, true, null, this, _ContextSafeCompleteCallback);
                    lock (this.operationCompletedResult.StartPostingAsyncOp())
                    {
                        this.transport.BeginGetConnection(this.ServicePoint, this.operationCompletedResult, new AsyncCallback(this.ConnectCallback), this.operationCompletedResult);
                        this.operationCompletedResult.FinishPostingAsyncOp();
                    }
                }
                catch (Exception exception)
                {
                    this.InCall = false;
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.Web, this, "Send", exception);
                    }
                    if ((exception is SmtpFailedRecipientException) && !((SmtpFailedRecipientException) exception).fatal)
                    {
                        throw;
                    }
                    this.Abort();
                    if (this.timedOut)
                    {
                        throw new SmtpException(SR.GetString("net_timeout"));
                    }
                    if (((exception is SecurityException) || (exception is AuthenticationException)) || (exception is SmtpException))
                    {
                        throw;
                    }
                    throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception);
                }
            }
            finally
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, this, "SendAsync", (string) null);
                }
            }
        }
 private void SendMailCallback(IAsyncResult result)
 {
     try
     {
         this.writer = this.transport.EndSendMail(result);
     }
     catch (Exception exception)
     {
         if (!(exception is SmtpFailedRecipientException) || ((SmtpFailedRecipientException) exception).fatal)
         {
             this.Complete(exception, result);
             return;
         }
     }
     try
     {
         if (this.cancelled)
         {
             this.Complete(null, result);
         }
         else
         {
             this.message.BeginSend(this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network, new AsyncCallback(this.SendMessageCallback), result.AsyncState);
         }
     }
     catch (Exception exception2)
     {
         this.Complete(exception2, result);
     }
 }
Beispiel #3
0
        private void SendMailCallback(IAsyncResult result)
        {
            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Enter("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback");
            }
            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 (GlobalLog.IsEnabled)
                {
                    GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback");
                }
                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);
            }

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::SendMailCallback");
            }
        }
Beispiel #4
0
        public void SendAsync(MailMessage message, object userToken)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Web, this, "SendAsync", "DeliveryMethod=" + DeliveryMethod.ToString());
            }

            if (GlobalLog.IsEnabled)
            {
                GlobalLog.Enter("SmtpClient#" + LoggingHash.HashString(this) + "::SendAsync Transport#" + LoggingHash.HashString(_transport));
            }

            try
            {
                if (InCall)
                {
                    throw new InvalidOperationException(SR.net_inasync);
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                    CheckHostAndPort();

                _recipients = new MailAddressCollection();

                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.SmtpFromRequired);
                }

                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        _recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address in message.Bcc)
                    {
                        _recipients.Add(address);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address in message.CC)
                    {
                        _recipients.Add(address);
                    }
                }

                if (_recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.SmtpRecipientRequired);
                }

                try
                {
                    InCall = true;
                    _cancelled = false;
                    _message = message;
                    string pickupDirectory = PickupDirectoryLocation;

                    CredentialCache cache;
                    // Skip token capturing if no credentials are used or they don't include a default one.
                    // Also do capture the token if ICredential is not of CredentialCache type so we don't know what the exact credential response will be.
                    _transport.IdentityRequired = Credentials != null && (ReferenceEquals(Credentials, CredentialCache.DefaultNetworkCredentials) || (cache = Credentials as CredentialCache) == null);

                    _asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (DeliveryMethod)
                    {
                        case SmtpDeliveryMethod.PickupDirectoryFromIis:
                            throw new NotSupportedException(SR.SmtpGetIisPickupDirectoryNotSupported);

                        case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                            {
                                if (EnableSsl)
                                {
                                    throw new SmtpException(SR.SmtpPickupDirectoryDoesnotSupportSsl);
                                }

                                _writer = GetFileMailWriter(pickupDirectory);
                                bool allowUnicode = IsUnicodeSupported();
                                ValidateUnicodeRequirement(message, _recipients, allowUnicode);
                                message.Send(_writer, true, allowUnicode);

                                if (_writer != null)
                                    _writer.Close();

                                _transport.ReleaseConnection();
                                AsyncCompletedEventArgs eventArgs = new AsyncCompletedEventArgs(null, false, _asyncOp.UserSuppliedState);
                                InCall = false;
                                _asyncOp.PostOperationCompleted(_onSendCompletedDelegate, eventArgs);
                                break;
                            }

                        case SmtpDeliveryMethod.Network:
                        default:
                            _operationCompletedResult = new ContextAwareResult(_transport.IdentityRequired, true, null, this, s_contextSafeCompleteCallback);
                            lock (_operationCompletedResult.StartPostingAsyncOp())
                            {
                                if (GlobalLog.IsEnabled)
                                {
                                    GlobalLog.Print("SmtpClient#" + LoggingHash.HashString(this) + "::SendAsync calling BeginConnect.  Transport#" + LoggingHash.HashString(_transport));
                                }

                                _transport.BeginGetConnection(_operationCompletedResult, ConnectCallback, _operationCompletedResult, Host, Port);
                                _operationCompletedResult.FinishPostingAsyncOp();
                            }
                            break;
                    }
                }
                catch (Exception e)
                {
                    InCall = false;

                    if (NetEventSource.Log.IsEnabled()) NetEventSource.Exception(NetEventSource.ComponentType.Web, this, "Send", e);

                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal)
                    {
                        throw;
                    }

                    Abort();
                    if (_timedOut)
                    {
                        throw new SmtpException(SR.net_timeout);
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException)
                    {
                        throw;
                    }

                    throw new SmtpException(SR.SmtpSendMailFailure, e);
                }
            }
            finally
            {
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.Exit(NetEventSource.ComponentType.Web, this, "SendAsync", null);
                }

                if (GlobalLog.IsEnabled)
                {
                    GlobalLog.Leave("SmtpClient#" + LoggingHash.HashString(this) + "::SendAsync");
                }
            }
        }
Beispiel #5
0
        public void Send(MailMessage message) {
            if (Logging.On) Logging.Enter(Logging.Web, this, "Send", message);
            if (disposed) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }           
            try {
                if (Logging.On) Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + DeliveryMethod.ToString());
                if (Logging.On) Logging.Associate(Logging.Web, this, message);
                SmtpFailedRecipientException recipientException = null;

                if (InCall) {
                    throw new InvalidOperationException(SR.GetString(SR.net_inasync));
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                    CheckHostAndPort();

                MailAddressCollection recipients = new MailAddressCollection();

                if (message.From == null) {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpFromRequired));
                }

                if (message.To != null) {
                    foreach (MailAddress address in message.To) {
                        recipients.Add(address);
                    }
                }
                if (message.Bcc != null) {
                    foreach (MailAddress address in message.Bcc) {
                        recipients.Add(address);
                    }
                }
                if (message.CC != null) {
                    foreach (MailAddress address in message.CC) {
                        recipients.Add(address);
                    }
                }

                if (recipients.Count == 0) {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpRecipientRequired));
                }

                transport.IdentityRequired = false;  // everything completes on the same thread.

                try {
                    InCall = true;
                    timedOut = false;
                    timer = new Timer(new TimerCallback(this.TimeOutCallback), null, Timeout, Timeout);
                    bool allowUnicode = false;
                    string pickupDirectory = PickupDirectoryLocation;

                    MailWriter writer;
                    switch (DeliveryMethod) {
#if !FEATURE_PAL
                        case SmtpDeliveryMethod.PickupDirectoryFromIis:
                            pickupDirectory = IisPickupDirectory.GetPickupDirectory();
                            goto case SmtpDeliveryMethod.SpecifiedPickupDirectory;
#endif // !FEATURE_PAL
                        case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                            if (EnableSsl)
                                throw new SmtpException(SR.GetString(SR.SmtpPickupDirectoryDoesnotSupportSsl));
                            allowUnicode = IsUnicodeSupported(); // Determend by the DeliveryFormat paramiter
                            ValidateUnicodeRequirement(message, recipients, allowUnicode);
                            writer = GetFileMailWriter(pickupDirectory);
                            break;

                        case SmtpDeliveryMethod.Network:
                        default:
                            GetConnection();
                            // Detected durring GetConnection(), restrictable using the DeliveryFormat paramiter
                            allowUnicode = IsUnicodeSupported();
                            ValidateUnicodeRequirement(message, recipients, allowUnicode);
                            writer = transport.SendMail(message.Sender ?? message.From, recipients, 
                                message.BuildDeliveryStatusNotificationString(), allowUnicode, out recipientException);
                            break;
                    }
                    this.message = message;
                    message.Send(writer, DeliveryMethod != SmtpDeliveryMethod.Network, allowUnicode);
                    writer.Close();
                    transport.ReleaseConnection();

                    //throw if we couldn't send to any of the recipients
                    if (DeliveryMethod == SmtpDeliveryMethod.Network && recipientException != null) {
                        throw recipientException;
                    }
                }
                catch (Exception e) {

                    if (Logging.On) Logging.Exception(Logging.Web, this, "Send", e);


                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal) {
                        throw;
                    }


                    Abort();
                    if (timedOut) {
                        throw new SmtpException(SR.GetString(SR.net_timeout));
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException) 
                    {
                        throw;
                    }

                    throw new SmtpException(SR.GetString(SR.SmtpSendMailFailure), e);
                } 
                finally {
                    InCall = false;
                    if (timer != null) {
                        timer.Dispose();
                    }
                }
            } finally {
                if (Logging.On) Logging.Exit(Logging.Web, this, "Send", null);
            }
        }
Beispiel #6
0
        public void SendAsync(MailMessage message, object userToken) {
            if (disposed) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (Logging.On) Logging.Enter(Logging.Web, this, "SendAsync", "DeliveryMethod=" + DeliveryMethod.ToString());
            GlobalLog.Enter("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync Transport#" + ValidationHelper.HashString(transport));
            try {
                if (InCall) {
                    throw new InvalidOperationException(SR.GetString(SR.net_inasync));
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                    CheckHostAndPort();

                recipients = new MailAddressCollection();

                if (message.From == null) {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpFromRequired));
                }

                if (message.To != null) {
                    foreach (MailAddress address in message.To) {
                        recipients.Add(address);
                    }
                }
                if (message.Bcc != null) {
                    foreach (MailAddress address in message.Bcc) {
                        recipients.Add(address);
                    }
                }
                if (message.CC != null) {
                    foreach (MailAddress address in message.CC) {
                        recipients.Add(address);
                    }
                }

                if (recipients.Count == 0) {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpRecipientRequired));
                }

                try {
                    InCall = true;
                    cancelled = false;
                    this.message = message;
                    string pickupDirectory = PickupDirectoryLocation;

#if !FEATURE_PAL
                    CredentialCache cache;
                    // Skip token capturing if no credentials are used or they don't include a default one.
                    // Also do capture the token if ICredential is not of CredentialCache type so we don't know what the exact credential response will be.
                    transport.IdentityRequired = Credentials != null && (Credentials is SystemNetworkCredential || (cache = Credentials as CredentialCache) == null || cache.IsDefaultInCache);
#endif // !FEATURE_PAL

                    asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (DeliveryMethod) {
#if !FEATURE_PAL
                        case SmtpDeliveryMethod.PickupDirectoryFromIis:
                            pickupDirectory = IisPickupDirectory.GetPickupDirectory();
                            goto case SmtpDeliveryMethod.SpecifiedPickupDirectory;
#endif // !FEATURE_PAL
                        case SmtpDeliveryMethod.SpecifiedPickupDirectory: 
                            {
                                if (EnableSsl)
                                    throw new SmtpException(SR.GetString(SR.SmtpPickupDirectoryDoesnotSupportSsl));
                                writer = GetFileMailWriter(pickupDirectory);
                                bool allowUnicode = IsUnicodeSupported();
                                ValidateUnicodeRequirement(message, recipients, allowUnicode);
                                message.Send(writer, true, allowUnicode);

                                if (writer != null)
                                    writer.Close();

                                transport.ReleaseConnection();
                                AsyncCompletedEventArgs eventArgs = new AsyncCompletedEventArgs(null, false, asyncOp.UserSuppliedState);
                                InCall = false;
                                asyncOp.PostOperationCompleted(onSendCompletedDelegate, eventArgs);
                                break;
                            }

                        case SmtpDeliveryMethod.Network:
                        default:
                            operationCompletedResult = new ContextAwareResult(transport.IdentityRequired, true, null, this, _ContextSafeCompleteCallback);
                            lock (operationCompletedResult.StartPostingAsyncOp()) 
                            {
                                GlobalLog.Print("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync calling BeginConnect.  Transport#" + ValidationHelper.HashString(transport));
                                transport.BeginGetConnection(ServicePoint, operationCompletedResult, ConnectCallback, operationCompletedResult);
                                operationCompletedResult.FinishPostingAsyncOp();
                            }
                            break;
                    }

                }
                catch (Exception e) {
                    InCall = false;

                    if (Logging.On) Logging.Exception(Logging.Web, this, "Send", e);

                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal) {
                        throw;
                    }

                    Abort();
                    if (timedOut) {
                        throw new SmtpException(SR.GetString(SR.net_timeout));
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException) 
                    {
                        throw;
                    }

                    throw new SmtpException(SR.GetString(SR.SmtpSendMailFailure), e);
                }
            } finally {
                if (Logging.On) Logging.Exit(Logging.Web, this, "SendAsync", null);
                GlobalLog.Leave("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync");
            }
        }
Beispiel #7
0
        public void SendAsync(MailMessage message, object userToken)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, this, "SendAsync", "DeliveryMethod=" + DeliveryMethod.ToString());
            }
            GlobalLog.Enter("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync Transport#" + ValidationHelper.HashString(transport));
            try {
                if (InCall)
                {
                    throw new InvalidOperationException(SR.GetString(SR.net_inasync));
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    CheckHostAndPort();
                }

                recipients = new MailAddressCollection();

                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpFromRequired));
                }

                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address in message.Bcc)
                    {
                        recipients.Add(address);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address in message.CC)
                    {
                        recipients.Add(address);
                    }
                }

                if (recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpRecipientRequired));
                }

                try {
                    InCall       = true;
                    cancelled    = false;
                    this.message = message;
                    string pickupDirectory = PickupDirectoryLocation;

#if !FEATURE_PAL
                    CredentialCache cache;
                    // Skip token capturing if no credentials are used or they don't include a default one.
                    // Also do capture the token if ICredential is not of CredentialCache type so we don't know what the exact credential response will be.
                    transport.IdentityRequired = Credentials != null && (Credentials is SystemNetworkCredential || (cache = Credentials as CredentialCache) == null || cache.IsDefaultInCache);
#endif // !FEATURE_PAL

                    asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (DeliveryMethod)
                    {
#if !FEATURE_PAL
                    case SmtpDeliveryMethod.PickupDirectoryFromIis:
                        pickupDirectory = IisPickupDirectory.GetPickupDirectory();
                        goto case SmtpDeliveryMethod.SpecifiedPickupDirectory;
#endif // !FEATURE_PAL
                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                    {
                        if (EnableSsl)
                        {
                            throw new SmtpException(SR.GetString(SR.SmtpPickupDirectoryDoesnotSupportSsl));
                        }
                        writer = GetFileMailWriter(pickupDirectory);
                        bool allowUnicode = IsUnicodeSupported();
                        ValidateUnicodeRequirement(message, recipients, allowUnicode);
                        message.Send(writer, true, allowUnicode);

                        if (writer != null)
                        {
                            writer.Close();
                        }

                        transport.ReleaseConnection();
                        AsyncCompletedEventArgs eventArgs = new AsyncCompletedEventArgs(null, false, asyncOp.UserSuppliedState);
                        InCall = false;
                        asyncOp.PostOperationCompleted(onSendCompletedDelegate, eventArgs);
                        break;
                    }

                    case SmtpDeliveryMethod.Network:
                    default:
                        operationCompletedResult = new ContextAwareResult(transport.IdentityRequired, true, null, this, _ContextSafeCompleteCallback);
                        lock (operationCompletedResult.StartPostingAsyncOp())
                        {
                            GlobalLog.Print("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync calling BeginConnect.  Transport#" + ValidationHelper.HashString(transport));
                            transport.BeginGetConnection(ServicePoint, operationCompletedResult, ConnectCallback, operationCompletedResult);
                            operationCompletedResult.FinishPostingAsyncOp();
                        }
                        break;
                    }
                }
                catch (Exception e) {
                    InCall = false;

                    if (Logging.On)
                    {
                        Logging.Exception(Logging.Web, this, "Send", e);
                    }

                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal)
                    {
                        throw;
                    }

                    Abort();
                    if (timedOut)
                    {
                        throw new SmtpException(SR.GetString(SR.net_timeout));
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException)
                    {
                        throw;
                    }

                    throw new SmtpException(SR.GetString(SR.SmtpSendMailFailure), e);
                }
            } finally {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, this, "SendAsync", null);
                }
                GlobalLog.Leave("SmtpClient#" + ValidationHelper.HashString(this) + "::SendAsync");
            }
        }
Beispiel #8
0
        public void SendAsync(MailMessage message, object userToken)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Enter(this, message, userToken, _transport);
            }

            try
            {
                if (InCall)
                {
                    throw new InvalidOperationException(SR.net_inasync);
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    CheckHostAndPort();
                }

                _recipients = new MailAddressCollection();

                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.SmtpFromRequired);
                }

                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        _recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address in message.Bcc)
                    {
                        _recipients.Add(address);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address in message.CC)
                    {
                        _recipients.Add(address);
                    }
                }

                if (_recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.SmtpRecipientRequired);
                }

                try
                {
                    InCall     = true;
                    _cancelled = false;
                    _message   = message;
                    string pickupDirectory = PickupDirectoryLocation;

                    CredentialCache cache;
                    // Skip token capturing if no credentials are used or they don't include a default one.
                    // Also do capture the token if ICredential is not of CredentialCache type so we don't know what the exact credential response will be.
                    _transport.IdentityRequired = Credentials != null && (ReferenceEquals(Credentials, CredentialCache.DefaultNetworkCredentials) || (cache = Credentials as CredentialCache) == null || IsSystemNetworkCredentialInCache(cache));

                    _asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (DeliveryMethod)
                    {
                    case SmtpDeliveryMethod.PickupDirectoryFromIis:
                        throw new NotSupportedException(SR.SmtpGetIisPickupDirectoryNotSupported);

                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                    {
                        if (EnableSsl)
                        {
                            throw new SmtpException(SR.SmtpPickupDirectoryDoesnotSupportSsl);
                        }

                        _writer = GetFileMailWriter(pickupDirectory);
                        bool allowUnicode = IsUnicodeSupported();
                        ValidateUnicodeRequirement(message, _recipients, allowUnicode);
                        message.Send(_writer, true, allowUnicode);

                        if (_writer != null)
                        {
                            _writer.Close();
                        }

                        _transport.ReleaseConnection();
                        AsyncCompletedEventArgs eventArgs = new AsyncCompletedEventArgs(null, false, _asyncOp.UserSuppliedState);
                        InCall = false;
                        _asyncOp.PostOperationCompleted(_onSendCompletedDelegate, eventArgs);
                        break;
                    }

                    case SmtpDeliveryMethod.Network:
                    default:
                        _operationCompletedResult = new ContextAwareResult(_transport.IdentityRequired, true, null, this, s_contextSafeCompleteCallback);
                        lock (_operationCompletedResult.StartPostingAsyncOp())
                        {
                            if (NetEventSource.IsEnabled)
                            {
                                NetEventSource.Info(this, $"Calling BeginConnect. Transport: {_transport}");
                            }
                            _transport.BeginGetConnection(_operationCompletedResult, ConnectCallback, _operationCompletedResult, Host, Port);
                            _operationCompletedResult.FinishPostingAsyncOp();
                        }
                        break;
                    }
                }
                catch (Exception e)
                {
                    InCall = false;

                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Error(this, e);
                    }

                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal)
                    {
                        throw;
                    }

                    Abort();
                    if (_timedOut)
                    {
                        throw new SmtpException(SR.net_timeout);
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException)
                    {
                        throw;
                    }

                    throw new SmtpException(SR.SmtpSendMailFailure, e);
                }
            }
            finally
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Exit(this);
                }
            }
        }
Beispiel #9
0
        public void Send(MailMessage message)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, this, "Send", message);
            }
            if (disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            try {
                if (Logging.On)
                {
                    Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + DeliveryMethod.ToString());
                }
                if (Logging.On)
                {
                    Logging.Associate(Logging.Web, this, message);
                }
                SmtpFailedRecipientException recipientException = null;

                if (InCall)
                {
                    throw new InvalidOperationException(SR.GetString(SR.net_inasync));
                }

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

                if (DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    CheckHostAndPort();
                }

                MailAddressCollection recipients = new MailAddressCollection();

                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpFromRequired));
                }

                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address in message.Bcc)
                    {
                        recipients.Add(address);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address in message.CC)
                    {
                        recipients.Add(address);
                    }
                }

                if (recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.SmtpRecipientRequired));
                }

                transport.IdentityRequired = false;  // everything completes on the same thread.

                try {
                    InCall   = true;
                    timedOut = false;
                    timer    = new Timer(new TimerCallback(this.TimeOutCallback), null, Timeout, Timeout);
                    bool   allowUnicode    = false;
                    string pickupDirectory = PickupDirectoryLocation;

                    MailWriter writer;
                    switch (DeliveryMethod)
                    {
#if !FEATURE_PAL
                    case SmtpDeliveryMethod.PickupDirectoryFromIis:
                        pickupDirectory = IisPickupDirectory.GetPickupDirectory();
                        goto case SmtpDeliveryMethod.SpecifiedPickupDirectory;
#endif // !FEATURE_PAL
                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                        if (EnableSsl)
                        {
                            throw new SmtpException(SR.GetString(SR.SmtpPickupDirectoryDoesnotSupportSsl));
                        }
                        allowUnicode = IsUnicodeSupported();     // Determend by the DeliveryFormat paramiter
                        ValidateUnicodeRequirement(message, recipients, allowUnicode);
                        writer = GetFileMailWriter(pickupDirectory);
                        break;

                    case SmtpDeliveryMethod.Network:
                    default:
                        GetConnection();
                        // Detected durring GetConnection(), restrictable using the DeliveryFormat paramiter
                        allowUnicode = IsUnicodeSupported();
                        ValidateUnicodeRequirement(message, recipients, allowUnicode);
                        writer = transport.SendMail(message.Sender ?? message.From, recipients,
                                                    message.BuildDeliveryStatusNotificationString(), allowUnicode, out recipientException);
                        break;
                    }
                    this.message = message;
                    message.Send(writer, DeliveryMethod != SmtpDeliveryMethod.Network, allowUnicode);
                    writer.Close();
                    transport.ReleaseConnection();

                    //throw if we couldn't send to any of the recipients
                    if (DeliveryMethod == SmtpDeliveryMethod.Network && recipientException != null)
                    {
                        throw recipientException;
                    }
                }
                catch (Exception e) {
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.Web, this, "Send", e);
                    }


                    if (e is SmtpFailedRecipientException && !((SmtpFailedRecipientException)e).fatal)
                    {
                        throw;
                    }


                    Abort();
                    if (timedOut)
                    {
                        throw new SmtpException(SR.GetString(SR.net_timeout));
                    }

                    if (e is SecurityException ||
                        e is AuthenticationException ||
                        e is SmtpException)
                    {
                        throw;
                    }

                    throw new SmtpException(SR.GetString(SR.SmtpSendMailFailure), e);
                }
                finally {
                    InCall = false;
                    if (timer != null)
                    {
                        timer.Dispose();
                    }
                }
            } finally {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, this, "Send", null);
                }
            }
        }
Beispiel #10
0
 private void SendMailCallback(IAsyncResult result)
 {
     try
       {
     this.writer = this.transport.EndSendMail(result);
     this.failedRecipientException = ((SendMailAsyncResult) result).GetFailedRecipientException();
       }
       catch (Exception ex)
       {
     this.Complete(ex, result);
     return;
       }
       try
       {
     if (this.cancelled)
       this.Complete((Exception) null, result);
     else
       this.message.BeginSend((BaseWriter) this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network, this.ServerSupportsEai, new AsyncCallback(this.SendMessageCallback), result.AsyncState);
       }
       catch (Exception ex)
       {
     this.Complete(ex, result);
       }
 }
Beispiel #11
0
 public void SendAsync(MailMessage message, object userToken)
 {
     if (this.disposed)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (Logging.On)
     Logging.Enter(Logging.Web, (object) this, "SendAsync", "DeliveryMethod=" + ((object) this.DeliveryMethod).ToString());
       try
       {
     if (this.InCall)
       throw new InvalidOperationException(SR.GetString("net_inasync"));
     if (message == null)
       throw new ArgumentNullException("message");
     if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
       this.CheckHostAndPort();
     this.recipients = new MailAddressCollection();
     if (message.From == null)
       throw new InvalidOperationException(SR.GetString("SmtpFromRequired"));
     if (message.To != null)
     {
       foreach (MailAddress mailAddress in (Collection<MailAddress>) message.To)
     ((Collection<MailAddress>) this.recipients).Add(mailAddress);
     }
     if (message.Bcc != null)
     {
       foreach (MailAddress mailAddress in (Collection<MailAddress>) message.Bcc)
     ((Collection<MailAddress>) this.recipients).Add(mailAddress);
     }
     if (message.CC != null)
     {
       foreach (MailAddress mailAddress in (Collection<MailAddress>) message.CC)
     ((Collection<MailAddress>) this.recipients).Add(mailAddress);
     }
     if (this.recipients.Count == 0)
       throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired"));
     try
     {
       this.InCall = true;
       this.cancelled = false;
       this.message = message;
       string pickupDirectory = this.PickupDirectoryLocation;
       CredentialCache credentialCache;
       this.transport.IdentityRequired = this.Credentials != null && (this.Credentials is SystemNetworkCredential || (credentialCache = this.Credentials as CredentialCache) == null || credentialCache.IsDefaultInCache);
       this.asyncOp = AsyncOperationManager.CreateOperation(userToken);
       switch (this.DeliveryMethod)
       {
     case SmtpDeliveryMethod.SpecifiedPickupDirectory:
       if (this.EnableSsl)
         throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
       this.writer = this.GetFileMailWriter(pickupDirectory);
       bool allowUnicode = this.IsUnicodeSupported();
       this.ValidateUnicodeRequirement(message, this.recipients, allowUnicode);
       message.Send((BaseWriter) this.writer, true, allowUnicode);
       if (this.writer != null)
         this.writer.Close();
       this.transport.ReleaseConnection();
       AsyncCompletedEventArgs completedEventArgs = new AsyncCompletedEventArgs((Exception) null, false, this.asyncOp.UserSuppliedState);
       this.InCall = false;
       this.asyncOp.PostOperationCompleted(this.onSendCompletedDelegate, (object) completedEventArgs);
       break;
     case SmtpDeliveryMethod.PickupDirectoryFromIis:
       pickupDirectory = IisPickupDirectory.GetPickupDirectory();
       goto case 1;
     default:
       this.operationCompletedResult = new ContextAwareResult(this.transport.IdentityRequired, true, (object) null, (object) this, SmtpClient._ContextSafeCompleteCallback);
       lock (this.operationCompletedResult.StartPostingAsyncOp())
       {
         this.transport.BeginGetConnection(this.ServicePoint, this.operationCompletedResult, new AsyncCallback(this.ConnectCallback), (object) this.operationCompletedResult);
         this.operationCompletedResult.FinishPostingAsyncOp();
         break;
       }
       }
     }
     catch (Exception ex)
     {
       this.InCall = false;
       if (Logging.On)
     Logging.Exception(Logging.Web, (object) this, "Send", ex);
       if (ex is SmtpFailedRecipientException && !((SmtpFailedRecipientException) ex).fatal)
       {
     throw;
       }
       else
       {
     this.Abort();
     if (this.timedOut)
       throw new SmtpException(SR.GetString("net_timeout"));
     if (!(ex is SecurityException) && !(ex is AuthenticationException) && !(ex is SmtpException))
       throw new SmtpException(SR.GetString("SmtpSendMailFailure"), ex);
     throw;
       }
     }
       }
       finally
       {
     if (Logging.On)
       Logging.Exit(Logging.Web, (object) this, "SendAsync", (string) null);
       }
 }
        public void SendAsync(MailMessage message, object userToken)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, this, "SendAsync", "DeliveryMethod=" + this.DeliveryMethod.ToString());
            }
            try
            {
                if (this.InCall)
                {
                    throw new InvalidOperationException(SR.GetString("net_inasync"));
                }
                if (message == null)
                {
                    throw new ArgumentNullException("message");
                }
                if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    this.CheckHostAndPort();
                }
                this.recipients = new MailAddressCollection();
                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpFromRequired"));
                }
                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        this.recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address2 in message.Bcc)
                    {
                        this.recipients.Add(address2);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address3 in message.CC)
                    {
                        this.recipients.Add(address3);
                    }
                }
                if (this.recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired"));
                }
                try
                {
                    CredentialCache cache;
                    this.InCall    = true;
                    this.cancelled = false;
                    this.message   = message;
                    this.transport.IdentityRequired = ((this.Credentials != null) && ComNetOS.IsWinNt) && (((this.Credentials is SystemNetworkCredential) || ((cache = this.Credentials as CredentialCache) == null)) || cache.IsDefaultInCache);
                    this.asyncOp = AsyncOperationManager.CreateOperation(userToken);
                    switch (this.DeliveryMethod)
                    {
                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                        if (this.EnableSsl)
                        {
                            throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                        }
                        break;

                    case SmtpDeliveryMethod.PickupDirectoryFromIis:
                        if (this.EnableSsl)
                        {
                            throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                        }
                        goto Label_02B2;

                    default:
                        goto Label_0329;
                    }
                    this.writer = this.GetFileMailWriter(this.PickupDirectoryLocation);
                    message.Send(this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network);
                    if (this.writer != null)
                    {
                        this.writer.Close();
                    }
                    this.transport.ReleaseConnection();
                    AsyncCompletedEventArgs arg = new AsyncCompletedEventArgs(null, false, this.asyncOp.UserSuppliedState);
                    this.InCall = false;
                    this.asyncOp.PostOperationCompleted(this.onSendCompletedDelegate, arg);
                    return;

Label_02B2:
                    this.writer = this.GetFileMailWriter(IisPickupDirectory.GetPickupDirectory());
                    message.Send(this.writer, this.DeliveryMethod != SmtpDeliveryMethod.Network);
                    if (this.writer != null)
                    {
                        this.writer.Close();
                    }
                    this.transport.ReleaseConnection();
                    AsyncCompletedEventArgs args2 = new AsyncCompletedEventArgs(null, false, this.asyncOp.UserSuppliedState);
                    this.InCall = false;
                    this.asyncOp.PostOperationCompleted(this.onSendCompletedDelegate, args2);
                    return;

Label_0329:
                    this.operationCompletedResult = new ContextAwareResult(this.transport.IdentityRequired, true, null, this, _ContextSafeCompleteCallback);
                    lock (this.operationCompletedResult.StartPostingAsyncOp())
                    {
                        this.transport.BeginGetConnection(this.ServicePoint, this.operationCompletedResult, new AsyncCallback(this.ConnectCallback), this.operationCompletedResult);
                        this.operationCompletedResult.FinishPostingAsyncOp();
                    }
                }
                catch (Exception exception)
                {
                    this.InCall = false;
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.Web, this, "Send", exception);
                    }
                    if ((exception is SmtpFailedRecipientException) && !((SmtpFailedRecipientException)exception).fatal)
                    {
                        throw;
                    }
                    this.Abort();
                    if (this.timedOut)
                    {
                        throw new SmtpException(SR.GetString("net_timeout"));
                    }
                    if (((exception is SecurityException) || (exception is AuthenticationException)) || (exception is SmtpException))
                    {
                        throw;
                    }
                    throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception);
                }
            }
            finally
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, this, "SendAsync", (string)null);
                }
            }
        }
        public void Send(MailMessage message)
        {
            if (Logging.On)
            {
                Logging.Enter(Logging.Web, this, "Send", message);
            }
            if (this.disposed)
            {
                throw new ObjectDisposedException(base.GetType().FullName);
            }
            try
            {
                if (Logging.On)
                {
                    Logging.PrintInfo(Logging.Web, this, "Send", "DeliveryMethod=" + this.DeliveryMethod.ToString());
                }
                if (Logging.On)
                {
                    Logging.Associate(Logging.Web, this, message);
                }
                SmtpFailedRecipientException exception = null;
                if (this.InCall)
                {
                    throw new InvalidOperationException(SR.GetString("net_inasync"));
                }
                if (message == null)
                {
                    throw new ArgumentNullException("message");
                }
                if (this.DeliveryMethod == SmtpDeliveryMethod.Network)
                {
                    this.CheckHostAndPort();
                }
                MailAddressCollection recipients = new MailAddressCollection();
                if (message.From == null)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpFromRequired"));
                }
                if (message.To != null)
                {
                    foreach (MailAddress address in message.To)
                    {
                        recipients.Add(address);
                    }
                }
                if (message.Bcc != null)
                {
                    foreach (MailAddress address2 in message.Bcc)
                    {
                        recipients.Add(address2);
                    }
                }
                if (message.CC != null)
                {
                    foreach (MailAddress address3 in message.CC)
                    {
                        recipients.Add(address3);
                    }
                }
                if (recipients.Count == 0)
                {
                    throw new InvalidOperationException(SR.GetString("SmtpRecipientRequired"));
                }
                this.transport.IdentityRequired = false;
                try
                {
                    this.InCall   = true;
                    this.timedOut = false;
                    this.timer    = new System.Threading.Timer(new TimerCallback(this.TimeOutCallback), null, this.Timeout, this.Timeout);
                    switch (this.DeliveryMethod)
                    {
                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                        if (this.EnableSsl)
                        {
                            throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                        }
                        break;

                    case SmtpDeliveryMethod.PickupDirectoryFromIis:
                        if (this.EnableSsl)
                        {
                            throw new SmtpException(SR.GetString("SmtpPickupDirectoryDoesnotSupportSsl"));
                        }
                        goto Label_0235;

                    default:
                        goto Label_0244;
                    }
                    MailWriter fileMailWriter = this.GetFileMailWriter(this.PickupDirectoryLocation);
                    goto Label_0276;
Label_0235:
                    fileMailWriter = this.GetFileMailWriter(IisPickupDirectory.GetPickupDirectory());
                    goto Label_0276;
Label_0244:
                    this.GetConnection();
                    fileMailWriter = this.transport.SendMail((message.Sender != null) ? message.Sender : message.From, recipients, message.BuildDeliveryStatusNotificationString(), out exception);
Label_0276:
                    this.message = message;
                    message.Send(fileMailWriter, this.DeliveryMethod != SmtpDeliveryMethod.Network);
                    fileMailWriter.Close();
                    this.transport.ReleaseConnection();
                    if ((this.DeliveryMethod == SmtpDeliveryMethod.Network) && (exception != null))
                    {
                        throw exception;
                    }
                }
                catch (Exception exception2)
                {
                    if (Logging.On)
                    {
                        Logging.Exception(Logging.Web, this, "Send", exception2);
                    }
                    if ((exception2 is SmtpFailedRecipientException) && !((SmtpFailedRecipientException)exception2).fatal)
                    {
                        throw;
                    }
                    this.Abort();
                    if (this.timedOut)
                    {
                        throw new SmtpException(SR.GetString("net_timeout"));
                    }
                    if (((exception2 is SecurityException) || (exception2 is AuthenticationException)) || (exception2 is SmtpException))
                    {
                        throw;
                    }
                    throw new SmtpException(SR.GetString("SmtpSendMailFailure"), exception2);
                }
                finally
                {
                    this.InCall = false;
                    if (this.timer != null)
                    {
                        this.timer.Dispose();
                    }
                }
            }
            finally
            {
                if (Logging.On)
                {
                    Logging.Exit(Logging.Web, this, "Send", (string)null);
                }
            }
        }