Beispiel #1
0
        private void SendMailCallback(IAsyncResult result)
        {
            try
            {
                _writer = SmtpTransport.EndSendMail(result);
                // If some recipients failed but not others, send the e-mail anyway, 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.AsyncState !);
                return;
            }

            try
            {
                if (_cancelled)
                {
                    Complete(null, result.AsyncState !);
                }
                else
                {
                    _message !.BeginSend(_writer, DeliveryMethod != SmtpDeliveryMethod.Network,
                                         IsUnicodeSupported(), new AsyncCallback(SendMessageCallback), result.AsyncState !);
                }
            }
            catch (Exception e)
            {
                Complete(e, result.AsyncState !);
            }
        }
 internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules)
 {
     this.client                = client;
     this.credentials           = credentials;
     this.authenticationModules = authenticationModules;
     this.parent                = parent;
     this.onCloseHandler        = new EventHandler(this.OnClose);
 }
 internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules)
 {
     this.client = client;
     this.credentials = credentials;
     this.authenticationModules = authenticationModules;
     this.parent = parent;
     this.onCloseHandler = new EventHandler(this.OnClose);
 }
Beispiel #4
0
 internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules)
 {
     _client = client;
     _credentials = credentials;
     _authenticationModules = authenticationModules;
     _parent = parent;
     _tcpClient = new TcpClient();
     _onCloseHandler = new EventHandler(OnClose);
 }
Beispiel #5
0
 internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules)
 {
     _client                = client;
     _credentials           = credentials;
     _authenticationModules = authenticationModules;
     _parent                = parent;
     _tcpClient             = new TcpClient();
     _onCloseHandler        = new EventHandler(OnClose);
 }
Beispiel #6
0
 private void ConnectCallback(IAsyncResult result)
 {
     try
     {
         SmtpTransport.EndGetConnection(result);
         if (_cancelled)
         {
             Complete(null, result.AsyncState !);
         }
         else
         {
             SendMailAsync(result.AsyncState !);
         }
     }
     catch (Exception e)
     {
         Complete(e, result.AsyncState !);
     }
 }
Beispiel #7
0
 private void ConnectCallback(IAsyncResult result)
 {
     try
     {
         SmtpTransport.EndGetConnection(result);
         if (_cancelled)
         {
             Complete(null, result);
         }
         else
         {
             // Detected during Begin/EndGetConnection, restrictable using DeliveryFormat
             bool allowUnicode = IsUnicodeSupported();
             ValidateUnicodeRequirement(_message !, _recipients !, allowUnicode);
             _transport.BeginSendMail(_message !.Sender ?? _message.From !, _recipients !,
                                      _message.BuildDeliveryStatusNotificationString(), allowUnicode,
                                      new AsyncCallback(SendMailCallback), result.AsyncState !);
         }
     }
     catch (Exception e)
     {
         Complete(e, result);
     }
 }
 private void Initialize()
 {
     if ((this.port == defaultPort) || (this.port == 0))
     {
         new SmtpPermission(SmtpAccess.Connect).Demand();
     }
     else
     {
         new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
     }
     this.transport = new SmtpTransport(this);
     if (Logging.On)
     {
         Logging.Associate(Logging.Web, this, this.transport);
     }
     this.onSendCompletedDelegate = new SendOrPostCallback(this.SendCompletedWaitCallback);
     if (MailConfiguration.Smtp != null)
     {
         if (MailConfiguration.Smtp.Network != null)
         {
             if ((this.host == null) || (this.host.Length == 0))
             {
                 this.host = MailConfiguration.Smtp.Network.Host;
             }
             if (this.port == 0)
             {
                 this.port = MailConfiguration.Smtp.Network.Port;
             }
             this.transport.Credentials = MailConfiguration.Smtp.Network.Credential;
             this.transport.EnableSsl = MailConfiguration.Smtp.Network.EnableSsl;
             if (MailConfiguration.Smtp.Network.TargetName != null)
             {
                 this.targetName = MailConfiguration.Smtp.Network.TargetName;
             }
             this.clientDomain = MailConfiguration.Smtp.Network.ClientDomain;
         }
         this.deliveryMethod = MailConfiguration.Smtp.DeliveryMethod;
         if (MailConfiguration.Smtp.SpecifiedPickupDirectory != null)
         {
             this.pickupDirectoryLocation = MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
         }
     }
     if ((this.host != null) && (this.host.Length != 0))
     {
         this.host = this.host.Trim();
     }
     if (this.port == 0)
     {
         this.port = defaultPort;
     }
     if (this.targetName == null)
     {
         this.targetName = "SMTPSVC/" + this.host;
     }
     if (this.clientDomain == null)
     {
         string hostName = IPGlobalProperties.InternalGetIPGlobalProperties().HostName;
         StringBuilder builder = new StringBuilder();
         for (int i = 0; i < hostName.Length; i++)
         {
             char ch = hostName[i];
             if (ch <= '\x007f')
             {
                 builder.Append(ch);
             }
         }
         if (builder.Length > 0)
         {
             this.clientDomain = builder.ToString();
         }
         else
         {
             this.clientDomain = "LocalHost";
         }
     }
 }
Beispiel #9
0
        private void Initialize()
        {
            if (_port == DefaultPort || _port == 0)
            {
                new SmtpPermission(SmtpAccess.Connect).Demand();
            }
            else
            {
                new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
            }

            _transport = new SmtpTransport(this);
            if (MailEventSource.Log.IsEnabled()) MailEventSource.Log.Associate(this, _transport);
            _onSendCompletedDelegate = new SendOrPostCallback(SendCompletedWaitCallback);

            if (_host != null && _host.Length != 0)
            {
                _host = _host.Trim();
            }

            if (_port == 0)
            {
                _port = DefaultPort;
            }

            if (_targetName == null)
                _targetName = "SMTPSVC/" + _host;

            if (clientDomain == null)
            {
                // We use the local host name as the default client domain
                // for the client's EHLO or HELO message. This limits the 
                // information about the host that we share. Additionally, the 
                // FQDN is not available to us or useful to the server (internal
                // machine connecting to public server).

                // SMTP RFC's require ASCII only host names in the HELO/EHLO message.
                string clientDomainRaw = IPGlobalProperties.GetIPGlobalProperties().HostName;
                IdnMapping mapping = new IdnMapping();
                try
                {
                    clientDomainRaw = mapping.GetAscii(clientDomainRaw);
                }
                catch (ArgumentException) { }

                // For some inputs GetAscii may fail (bad Unicode, etc).  If that happens
                // we must strip out any non-ASCII characters.
                // If we end up with no characters left, we use the string "LocalHost".  This 
                // matches Outlook behavior.
                StringBuilder sb = new StringBuilder();
                char ch;
                for (int i = 0; i < clientDomainRaw.Length; i++)
                {
                    ch = clientDomainRaw[i];
                    if ((ushort)ch <= 0x7F)
                        sb.Append(ch);
                }
                if (sb.Length > 0)
                    clientDomain = sb.ToString();
                else
                    clientDomain = "LocalHost";
            }
        }
Beispiel #10
0
        private void Initialize()
        {
            _transport = new SmtpTransport(this);
            if (NetEventSource.Log.IsEnabled())
            {
                NetEventSource.Associate(this, _transport);
            }
            _onSendCompletedDelegate = new SendOrPostCallback(SendCompletedWaitCallback);

            if (_host != null && _host.Length != 0)
            {
                _host = _host.Trim();
            }

            if (_port == 0)
            {
                _port = DefaultPort;
            }

            if (_targetName == null)
            {
                _targetName = "SMTPSVC/" + _host;
            }

            if (_clientDomain == null)
            {
                // We use the local host name as the default client domain
                // for the client's EHLO or HELO message. This limits the
                // information about the host that we share. Additionally, the
                // FQDN is not available to us or useful to the server (internal
                // machine connecting to public server).

                // SMTP RFC's require ASCII only host names in the HELO/EHLO message.
                string clientDomainRaw = IPGlobalProperties.GetIPGlobalProperties().HostName;

                IdnMapping mapping = new IdnMapping();
                try
                {
                    clientDomainRaw = mapping.GetAscii(clientDomainRaw);
                }
                catch (ArgumentException) { }

                // For some inputs GetAscii may fail (bad Unicode, etc).  If that happens
                // we must strip out any non-ASCII characters.
                // If we end up with no characters left, we use the string "LocalHost".  This
                // matches Outlook behavior.
                StringBuilder sb = new StringBuilder();
                char          ch;
                for (int i = 0; i < clientDomainRaw.Length; i++)
                {
                    ch = clientDomainRaw[i];
                    if ((ushort)ch <= 0x7F)
                    {
                        sb.Append(ch);
                    }
                }
                if (sb.Length > 0)
                {
                    _clientDomain = sb.ToString();
                }
                else
                {
                    _clientDomain = "LocalHost";
                }
            }
        }
Beispiel #11
0
        void Initialize() {
            if (port == defaultPort || port == 0) {
                new SmtpPermission(SmtpAccess.Connect).Demand();
            }
            else {
                new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
            }

            transport = new SmtpTransport(this);
            if (Logging.On) Logging.Associate(Logging.Web, this, transport);
            onSendCompletedDelegate = new SendOrPostCallback(SendCompletedWaitCallback);

            if (MailConfiguration.Smtp != null) 
            {
                if (MailConfiguration.Smtp.Network != null) 
                {
                    if (host == null || host.Length == 0) {
                        host = MailConfiguration.Smtp.Network.Host;
                    }
                    if (port == 0) {
                        port = MailConfiguration.Smtp.Network.Port;
                    }

                    transport.Credentials = MailConfiguration.Smtp.Network.Credential;
                    transport.EnableSsl = MailConfiguration.Smtp.Network.EnableSsl;

                    if (MailConfiguration.Smtp.Network.TargetName != null)
                        targetName = MailConfiguration.Smtp.Network.TargetName;

                    // If the config file contains a domain to be used for the 
                    // domain element in the client's EHLO or HELO message, 
                    // use it.
                    //
                    // We do not validate whether the domain specified is valid.
                    // It is up to the administrators or user to use the right
                    // value for their scenario. 
                    //
                    // Note: per section 4.1.4 of RFC2821, the domain element of 
                    // the HELO/EHLO should be used for logging purposes. An 
                    // SMTP server should not decide to route an email based on
                    // this value.
                    clientDomain = MailConfiguration.Smtp.Network.ClientDomain;
                }

                deliveryFormat = MailConfiguration.Smtp.DeliveryFormat;

                deliveryMethod = MailConfiguration.Smtp.DeliveryMethod;
                if (MailConfiguration.Smtp.SpecifiedPickupDirectory != null)
                    pickupDirectoryLocation = MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
            }

            if (host != null && host.Length != 0) {
                host = host.Trim();
            }

            if (port == 0) {
                port = defaultPort;
            }

            if (this.targetName == null)
                targetName = "SMTPSVC/" + host;

            if (clientDomain == null) {
                // We use the local host name as the default client domain
                // for the client's EHLO or HELO message. This limits the 
                // information about the host that we share. Additionally, the 
                // FQDN is not available to us or useful to the server (internal
                // machine connecting to public server).

                // SMTP RFC's require ASCII only host names in the HELO/EHLO message.
                string clientDomainRaw = IPGlobalProperties.InternalGetIPGlobalProperties().HostName;
                IdnMapping mapping = new IdnMapping();
                try
                {
                    clientDomainRaw = mapping.GetAscii(clientDomainRaw);
                }
                catch (ArgumentException) { }

                // For some inputs GetAscii may fail (bad Unicode, etc).  If that happens
                // we must strip out any non-ASCII characters.
                // If we end up with no characters left, we use the string "LocalHost".  This 
                // matches Outlook behavior.
                StringBuilder sb = new StringBuilder();
                char ch;
                for (int i = 0; i < clientDomainRaw.Length; i++) {
                    ch = clientDomainRaw[i];
                    if ((ushort)ch <= 0x7F)
                        sb.Append(ch);
                }
                if (sb.Length > 0)
                    clientDomain = sb.ToString();
                else
                    clientDomain = "LocalHost";
            }
        }
Beispiel #12
0
        void Initialize()
        {
            if (port == defaultPort || port == 0)
            {
                new SmtpPermission(SmtpAccess.Connect).Demand();
            }
            else
            {
                new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
            }

            transport = new SmtpTransport(this);
            if (Logging.On)
            {
                Logging.Associate(Logging.Web, this, transport);
            }
            onSendCompletedDelegate = new SendOrPostCallback(SendCompletedWaitCallback);

            if (MailConfiguration.Smtp != null)
            {
                if (MailConfiguration.Smtp.Network != null)
                {
                    if (host == null || host.Length == 0)
                    {
                        host = MailConfiguration.Smtp.Network.Host;
                    }
                    if (port == 0)
                    {
                        port = MailConfiguration.Smtp.Network.Port;
                    }

                    transport.Credentials = MailConfiguration.Smtp.Network.Credential;
                    transport.EnableSsl   = MailConfiguration.Smtp.Network.EnableSsl;

                    if (MailConfiguration.Smtp.Network.TargetName != null)
                    {
                        targetName = MailConfiguration.Smtp.Network.TargetName;
                    }

                    // If the config file contains a domain to be used for the
                    // domain element in the client's EHLO or HELO message,
                    // use it.
                    //
                    // We do not validate whether the domain specified is valid.
                    // It is up to the administrators or user to use the right
                    // value for their scenario.
                    //
                    // Note: per section 4.1.4 of RFC2821, the domain element of
                    // the HELO/EHLO should be used for logging purposes. An
                    // SMTP server should not decide to route an email based on
                    // this value.
                    clientDomain = MailConfiguration.Smtp.Network.ClientDomain;
                }

                deliveryFormat = MailConfiguration.Smtp.DeliveryFormat;

                deliveryMethod = MailConfiguration.Smtp.DeliveryMethod;
                if (MailConfiguration.Smtp.SpecifiedPickupDirectory != null)
                {
                    pickupDirectoryLocation = MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
                }
            }

            if (host != null && host.Length != 0)
            {
                host = host.Trim();
            }

            if (port == 0)
            {
                port = defaultPort;
            }

            if (this.targetName == null)
            {
                targetName = "SMTPSVC/" + host;
            }

            if (clientDomain == null)
            {
                // We use the local host name as the default client domain
                // for the client's EHLO or HELO message. This limits the
                // information about the host that we share. Additionally, the
                // FQDN is not available to us or useful to the server (internal
                // machine connecting to public server).

                // SMTP RFC's require ASCII only host names in the HELO/EHLO message.
                string     clientDomainRaw = IPGlobalProperties.InternalGetIPGlobalProperties().HostName;
                IdnMapping mapping         = new IdnMapping();
                try
                {
                    clientDomainRaw = mapping.GetAscii(clientDomainRaw);
                }
                catch (ArgumentException) { }

                // For some inputs GetAscii may fail (bad Unicode, etc).  If that happens
                // we must strip out any non-ASCII characters.
                // If we end up with no characters left, we use the string "LocalHost".  This
                // matches Outlook behavior.
                StringBuilder sb = new StringBuilder();
                char          ch;
                for (int i = 0; i < clientDomainRaw.Length; i++)
                {
                    ch = clientDomainRaw[i];
                    if ((ushort)ch <= 0x7F)
                    {
                        sb.Append(ch);
                    }
                }
                if (sb.Length > 0)
                {
                    clientDomain = sb.ToString();
                }
                else
                {
                    clientDomain = "LocalHost";
                }
            }
        }
Beispiel #13
0
 private void Initialize()
 {
     if (this.port == SmtpClient.defaultPort || this.port == 0)
     new SmtpPermission(SmtpAccess.Connect).Demand();
       else
     new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
       this.transport = new SmtpTransport(this);
       if (Logging.On)
     Logging.Associate(Logging.Web, (object) this, (object) this.transport);
       this.onSendCompletedDelegate = new SendOrPostCallback(this.SendCompletedWaitCallback);
       if (SmtpClient.MailConfiguration.Smtp != null)
       {
     if (SmtpClient.MailConfiguration.Smtp.Network != null)
     {
       if (this.host == null || this.host.Length == 0)
     this.host = SmtpClient.MailConfiguration.Smtp.Network.Host;
       if (this.port == 0)
     this.port = SmtpClient.MailConfiguration.Smtp.Network.Port;
       this.transport.Credentials = (ICredentialsByHost) SmtpClient.MailConfiguration.Smtp.Network.Credential;
       this.transport.EnableSsl = SmtpClient.MailConfiguration.Smtp.Network.EnableSsl;
       if (SmtpClient.MailConfiguration.Smtp.Network.TargetName != null)
     this.targetName = SmtpClient.MailConfiguration.Smtp.Network.TargetName;
       this.clientDomain = SmtpClient.MailConfiguration.Smtp.Network.ClientDomain;
     }
     this.deliveryFormat = SmtpClient.MailConfiguration.Smtp.DeliveryFormat;
     this.deliveryMethod = SmtpClient.MailConfiguration.Smtp.DeliveryMethod;
     if (SmtpClient.MailConfiguration.Smtp.SpecifiedPickupDirectory != null)
       this.pickupDirectoryLocation = SmtpClient.MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
       }
       if (this.host != null && this.host.Length != 0)
     this.host = this.host.Trim();
       if (this.port == 0)
     this.port = SmtpClient.defaultPort;
       if (this.targetName == null)
     this.targetName = "SMTPSVC/" + this.host;
       if (this.clientDomain != null)
     return;
       string unicode = IPGlobalProperties.InternalGetIPGlobalProperties().HostName;
       IdnMapping idnMapping = new IdnMapping();
       try
       {
     unicode = idnMapping.GetAscii(unicode);
       }
       catch (ArgumentException ex)
       {
       }
       StringBuilder stringBuilder = new StringBuilder();
       for (int index = 0; index < unicode.Length; ++index)
       {
     char ch = unicode[index];
     if ((int) ch <= (int) sbyte.MaxValue)
       stringBuilder.Append(ch);
       }
       if (stringBuilder.Length > 0)
     this.clientDomain = ((object) stringBuilder).ToString();
       else
     this.clientDomain = "LocalHost";
 }
 private void Initialize()
 {
     if ((this.port == defaultPort) || (this.port == 0))
     {
         new SmtpPermission(SmtpAccess.Connect).Demand();
     }
     else
     {
         new SmtpPermission(SmtpAccess.ConnectToUnrestrictedPort).Demand();
     }
     this.transport = new SmtpTransport(this);
     if (Logging.On)
     {
         Logging.Associate(Logging.Web, this, this.transport);
     }
     this.onSendCompletedDelegate = new SendOrPostCallback(this.SendCompletedWaitCallback);
     if (MailConfiguration.Smtp != null)
     {
         if (MailConfiguration.Smtp.Network != null)
         {
             if ((this.host == null) || (this.host.Length == 0))
             {
                 this.host = MailConfiguration.Smtp.Network.Host;
             }
             if (this.port == 0)
             {
                 this.port = MailConfiguration.Smtp.Network.Port;
             }
             this.transport.Credentials = MailConfiguration.Smtp.Network.Credential;
             this.transport.EnableSsl   = MailConfiguration.Smtp.Network.EnableSsl;
             if (MailConfiguration.Smtp.Network.TargetName != null)
             {
                 this.targetName = MailConfiguration.Smtp.Network.TargetName;
             }
             this.clientDomain = MailConfiguration.Smtp.Network.ClientDomain;
         }
         this.deliveryMethod = MailConfiguration.Smtp.DeliveryMethod;
         if (MailConfiguration.Smtp.SpecifiedPickupDirectory != null)
         {
             this.pickupDirectoryLocation = MailConfiguration.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation;
         }
     }
     if ((this.host != null) && (this.host.Length != 0))
     {
         this.host = this.host.Trim();
     }
     if (this.port == 0)
     {
         this.port = defaultPort;
     }
     if (this.targetName == null)
     {
         this.targetName = "SMTPSVC/" + this.host;
     }
     if (this.clientDomain == null)
     {
         string        hostName = IPGlobalProperties.InternalGetIPGlobalProperties().HostName;
         StringBuilder builder  = new StringBuilder();
         for (int i = 0; i < hostName.Length; i++)
         {
             char ch = hostName[i];
             if (ch <= '\x007f')
             {
                 builder.Append(ch);
             }
         }
         if (builder.Length > 0)
         {
             this.clientDomain = builder.ToString();
         }
         else
         {
             this.clientDomain = "LocalHost";
         }
     }
 }