Beispiel #1
0
 /// <summary>
 ///     Constructor.</summary>
 /// <param name="log">
 ///     Whenever a wait is triggered, a warning is reported through this logger.</param>
 /// <param name="limits">
 ///     The limits to be observed.</param>
 public Waiter(LoggerBase log, params WaiterLimit[] limits)
 {
     if (limits.Length == 0)
         throw new ArgumentException("Must specify at least one limit");
     Limits = limits.ToList();
     Log = log == null ? new NullLogger() : log;
 }
Beispiel #2
0
 /// <summary>
 ///     Creates a connection to the SMTP server and authenticates the specified user.</summary>
 /// <param name="settings">
 ///     An object containing the relevant SMTP settings.</param>
 /// <param name="log">
 ///     The SMTP client logs various messages to this log at various verbosity levels.</param>
 /// <exception cref="RTSmtpException">
 ///     SMTP protocol error, or authentication failed.</exception>
 public RTSmtpClient(RTSmtpSettings settings, LoggerBase log = null)
     : this(settings.Host, settings.Port, settings.Username, settings.PasswordDecrypted, settings.Encryption, log)
 {
 }
Beispiel #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 = 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.");
        }
Beispiel #4
0
 /// <summary>
 ///     Creates a connection to the SMTP server and authenticates the specified user.</summary>
 /// <param name="settings">
 ///     An object containing the relevant SMTP settings.</param>
 /// <param name="log">
 ///     The SMTP client logs various messages to this log at various verbosity levels.</param>
 /// <exception cref="RTSmtpException">
 ///     SMTP protocol error, or authentication failed.</exception>
 public RTSmtpClient(RTSmtpSettings settings, LoggerBase log = null)
     : this(settings.Host, settings.Port, settings.Username, settings.PasswordDecrypted, settings.Encryption, log)
 {
 }
Beispiel #5
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.");
        }