示例#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);
            }
        }
        public new async Task SendMailAsync(MailMessage message)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("SmtpClientEx");
            }

            if (DeliveryMethod == SmtpDeliveryMethod.Network)
            {
                if (string.IsNullOrEmpty(_host))
                {
                    if (_dnsClient == null)
                    {
                        _dnsClient = new DnsClient();
                    }

                    IReadOnlyList <string> mxServers = await _dnsClient.ResolveMXAsync(message.To[0].Host);

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

                    _port       = 25;
                    Credentials = null;
                }

                if (_proxy == null)
                {
                    if (_enableSslWrapper)
                    {
                        EndPoint remoteEP = EndPointExtension.GetEndPoint(_host, _port);

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

                        if ((_tunnelProxy == null) || _tunnelProxy.IsBroken)
                        {
                            IPEndPoint ep = await remoteEP.GetIPEndPointAsync();

                            Socket socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                            await socket.ConnectAsync(ep);

                            _tunnelProxy = new TunnelProxy(socket, remoteEP, _enableSslWrapper, _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, _enableSslWrapper, _ignoreCertificateErrors);
                    }

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

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