private SecureSocketOptions GetSecureSocketOption(SmtpEncryption encryption)
        {
            switch (encryption)
            {
            case SmtpEncryption.StartTLS:
                return(SecureSocketOptions.StartTls);

            case SmtpEncryption.SSL:
                return(SecureSocketOptions.SslOnConnect);

            default:
                return(SecureSocketOptions.Auto);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Creates a connection to the SMTP server and authenticates the specified user.</summary>
        /// <param name="host">
        ///     SMTP host name.</param>
        /// <param name="port">
        ///     SMTP host port.</param>
        /// <param name="username">
        ///     SMTP username.</param>
        /// <param name="password">
        ///     SMTP password.</param>
        /// <param name="encryption">
        ///     Encryption mode.</param>
        /// <param name="log">
        ///     The SMTP client logs various messages to this log at various verbosity levels.</param>
        /// <param name="timeout">
        ///     Network stream read/write timeout, in milliseconds.</param>
        /// <exception cref="RTSmtpException">
        ///     SMTP protocol error, or authentication failed.</exception>
        /// <exception cref="IOException">
        ///     Network error or timeout.</exception>
        public RTSmtpClient(string host, int port, string username = null, string password = null, SmtpEncryption encryption = SmtpEncryption.None, LoggerBase log = null, int timeout = 10000)
        {
            _log = log ?? new NullLogger();
            _log.Debug(2, "Connecting to {0}:{1}...".Fmt(host, port));
            _tcp       = new TcpClient(host, port);
            _tcpStream = _tcp.GetStream();
            if (encryption == SmtpEncryption.Ssl || encryption == SmtpEncryption.SslIgnoreCert)
            {
                if (encryption == SmtpEncryption.Ssl)
                {
                    _sslStream = new SslStream(_tcpStream);
                }
                else
                {
                    _sslStream = new SslStream(_tcpStream, false, (_, __, ___, ____) => true);
                }
                _sslStream.AuthenticateAsClient(host);
                _log.Debug(3, "SSL: authenticated as client");
            }
            (_sslStream ?? _tcpStream).ReadTimeout  = timeout;
            (_sslStream ?? _tcpStream).WriteTimeout = timeout;
            _writer = new StreamWriter(_sslStream ?? _tcpStream, new UTF8Encoding(false))
            {
                NewLine = "\r\n", AutoFlush = true
            };
            _reader       = new StreamReader(_sslStream ?? _tcpStream, Encoding.UTF8);
            _conversation = new List <string>();

            sendAndExpect(null, 220);
            sendAndExpect("EHLO localhost", 250);
            if (username == null || password == null)
            {
                _log.Debug(3, "Connected without authentication.");
                return;
            }

            var result    = sendAndExpect("AUTH LOGIN", 334).Trim();
            var resultDec = Convert.FromBase64String(result).FromUtf8();

            if (resultDec != "Username:"******"Expected 'Username:'******'{0}'".Fmt(resultDec), _conversation);
            }
            result    = sendAndExpect(Convert.ToBase64String(username.ToUtf8()), 334).Trim();
            resultDec = Convert.FromBase64String(result).FromUtf8();
            if (resultDec != "Password:"******"Expected 'Password:'******'{0}'".Fmt(resultDec), _conversation);
            }
            sendAndExpect(Convert.ToBase64String(password.ToUtf8()), 235);
            _log.Debug(3, "Connected.");
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Creates a connection to the SMTP server and authenticates the specified user.</summary>
        /// <param name="host">
        ///     SMTP host name.</param>
        /// <param name="port">
        ///     SMTP host port.</param>
        /// <param name="username">
        ///     SMTP username.</param>
        /// <param name="password">
        ///     SMTP password.</param>
        /// <param name="encryption">
        ///     Encryption mode.</param>
        /// <param name="log">
        ///     The SMTP client logs various messages to this log at various verbosity levels.</param>
        /// <param name="timeout">
        ///     Network stream read/write timeout, in milliseconds.</param>
        /// <exception cref="RTSmtpException">
        ///     SMTP protocol error, or authentication failed.</exception>
        /// <exception cref="IOException">
        ///     Network error or timeout.</exception>
        public RTSmtpClient(string host, int port, string username, string password, SmtpEncryption encryption = SmtpEncryption.None, LoggerBase log = null, int timeout = 10000)
        {
            _log = log ?? new NullLogger();
            _log.Debug(2, "Connecting to {0}:{1}...".Fmt(host, port));
            _tcp = new TcpClient(host, port);
            _tcpStream = _tcp.GetStream();
            if (encryption == SmtpEncryption.Ssl || encryption == SmtpEncryption.SslIgnoreCert)
            {
                if (encryption == SmtpEncryption.Ssl)
                    _sslStream = new SslStream(_tcpStream);
                else
                    _sslStream = new SslStream(_tcpStream, false, (_, __, ___, ____) => true);
                _sslStream.AuthenticateAsClient(host);
                _log.Debug(3, "SSL: authenticated as client");
            }
            (_sslStream ?? _tcpStream).ReadTimeout = timeout;
            (_sslStream ?? _tcpStream).WriteTimeout = timeout;
            _writer = new StreamWriter(_sslStream ?? _tcpStream, new UTF8Encoding(false)) { NewLine = "\r\n", AutoFlush = true };
            _reader = new StreamReader(_sslStream ?? _tcpStream, Encoding.UTF8);
            _conversation = new List<string>();

            sendAndExpect(null, 220);
            sendAndExpect("EHLO localhost", 250);
            var result = sendAndExpect("AUTH LOGIN", 334).Trim();
            var resultDec = Convert.FromBase64String(result).FromUtf8();
            if (resultDec != "Username:"******"Expected 'Username:'******'{0}'".Fmt(resultDec), _conversation);
            result = sendAndExpect(Convert.ToBase64String(username.ToUtf8()), 334).Trim();
            resultDec = Convert.FromBase64String(result).FromUtf8();
            if (resultDec != "Password:"******"Expected 'Password:'******'{0}'".Fmt(resultDec), _conversation);
            sendAndExpect(Convert.ToBase64String(password.ToUtf8()), 235);
            _log.Debug(3, "Connected.");
        }