Exemple #1
0
        public new async Task SendMailAsync(MailMessage message)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SmtpClientEx");
            }

            if (message.To.Count == 0)
            {
                throw new ArgumentException("Message does not contain receipent email address.");
            }

            if (DeliveryMethod == SmtpDeliveryMethod.Network)
            {
                string host = _host;

                if (string.IsNullOrEmpty(host))
                {
                    //resolve MX for the receipent domain using IDnsClient
                    if (_dnsClient == null)
                    {
                        _dnsClient = new DnsClient()
                        {
                            Proxy = _proxy
                        }
                    }
                    ;

                    IReadOnlyList <string> mxDomains = await Dns.DnsClient.ResolveMXAsync(_dnsClient, message.To[0].Host);

                    if (mxDomains.Count > 0)
                    {
                        host = mxDomains[0];
                    }
                    else
                    {
                        host = message.To[0].Host;
                    }

                    _port       = 25;
                    Credentials = null;
                }

                if (_proxy == null)
                {
                    if (_smtpOverTls)
                    {
                        EndPoint remoteEP = EndPointExtension.GetEndPoint(host, _port);

                        if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                        {
                            _tunnelProxy.Dispose();
                            _tunnelProxy = null;
                        }

                        if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                        {
                            _tunnelProxy = await TunnelProxy.CreateTunnelProxyAsync(remoteEP, true, _ignoreCertificateErrors);
                        }

                        base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                        base.Port = _tunnelProxy.TunnelEndPoint.Port;
                    }
                    else
                    {
                        base.Host = host;
                        base.Port = _port;
                    }

                    await base.SendMailAsync(message);
                }
                else
                {
                    EndPoint remoteEP = EndPointExtension.GetEndPoint(host, _port);

                    if ((_tunnelProxy != null) && !_tunnelProxy.RemoteEndPoint.Equals(remoteEP))
                    {
                        _tunnelProxy.Dispose();
                        _tunnelProxy = null;
                    }

                    if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                    {
                        _tunnelProxy = await _proxy.CreateTunnelProxyAsync(remoteEP, _smtpOverTls, _ignoreCertificateErrors);
                    }

                    base.Host = _tunnelProxy.TunnelEndPoint.Address.ToString();
                    base.Port = _tunnelProxy.TunnelEndPoint.Port;

                    await base.SendMailAsync(message);
                }
            }
            else
            {
                await base.SendMailAsync(message);
            }
        }