예제 #1
0
        private SmtpResponseReader SendSenders(Message message)
        {
            SmtpResponseReader reader = null;

            foreach (var from in message.From)
            {
                var text    = string.Format("MAIL FROM:<{0}>", from.Address.FullAddress);
                var command = new SmtpCommand(text);
                reader = SendAndReceive(command);

                if (reader.IsFatalOrErroneous)
                {
                    return(reader);
                }
            }
            return(reader);
        }
예제 #2
0
        /// <summary>
        ///   Cram-MD5 authorization.
        ///   http://tools.ietf.org/html/rfc2195
        /// </summary>
        private bool AuthenticateCramMd5(NetworkCredential credentials)
        {
            var command  = new SmtpCommand("AUTH CRAM-MD5");
            var response = _client.SendAndReceive(command);

            var base64    = response.CurrentLine.Substring(4).TrimEnd();
            var challenge = Base64Encoder.Decode(base64, Encoding.UTF8);

            var username = credentials.UserName;
            var password = credentials.Password;

            var hash = CramMd5Hasher.ComputeHash(password, challenge);

            var authentication = username + " " + hash;
            var reader         = _client.SendAndReceive(new SmtpCommand(Base64Encoder.Encode(authentication)));

            return(reader.IsOk);
        }
예제 #3
0
        private SmtpResponseReader SendData(Message message)
        {
            SendAndReceive(new SmtpCommand("DATA"));

            var mime = message.ToMime();

            using (var reader = new StringReader(mime)) {
                var chunkSize = UpdateUploadProgressTriggerChunkSize.Bytes;
                var trigger   = chunkSize;
                var current   = 0;
                var total     = mime.Length;

                var command = new SmtpCommand(string.Empty);
                while (true)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    command.Text = StuffPeriodIfNecessary(line);
                    Send(command);

                    // dont forget the newline characters
                    current += command.Text.Length + 2;

                    if (trigger - current < 0)
                    {
                        InvokeUploadProgressChanged(current, total);
                        trigger += chunkSize;
                    }

                    if (current >= total)
                    {
                        InvokeUploadProgressChanged(total, total);
                    }
                }
                SendDataTermination();
            }

            return(Receive());
        }
예제 #4
0
        private SmtpServerCapability Hello()
        {
            var host    = Dns.GetHostName();
            var command = new SmtpCommand("EHLO " + host);

            Send(command);

            // Timeout suggestion
            // http://tools.ietf.org/html/rfc5321#section-4.5.3.2.1
            var timeout  = TimeSpan.FromMinutes(5);
            var response = Receive(timeout);

            // fallback to HELO if EHLO is not supported
            if (response.IsFatalOrErroneous)
            {
                command = new SmtpCommand("HELO " + host);
                Send(command);
                response = Receive(timeout);
            }

            return(StaticResponseParser.ParseCapability(response));
        }
예제 #5
0
        /// <summary>
        ///   Login authorization, same as plain but split into two separate command/responses.
        ///   http://tools.ietf.org/html/rfc4616
        /// </summary>
        private bool AuthenticateLogin(NetworkCredential credentials)
        {
            var command  = new SmtpCommand("AUTH LOGIN");
            var response = _client.SendAndReceive(command);

            if (response.ResponseCode != 334)
            {
                return(false);
            }

            var username         = Base64Encoder.Encode(credentials.UserName);
            var userNameResponse = _client.SendAndReceive(new SmtpCommand(username));

            if (userNameResponse.ResponseCode != 334)
            {
                return(false);
            }

            var password         = Base64Encoder.Encode(credentials.Password);
            var passwordResponse = _client.SendAndReceive(new SmtpCommand(password));

            return(passwordResponse.ResponseCode == ResponseCodes.AuthenticationSuccessful);
        }
예제 #6
0
        private SmtpResponseReader SendRecipients(Message message)
        {
            SmtpResponseReader reader = null;
            var recipients            = new List <EmailContact>();

            recipients.AddRange(message.Ccs);
            recipients.AddRange(message.Bccs);
            recipients.AddRange(message.To);
            recipients.RemoveDuplicates();

            foreach (var recipient in recipients)
            {
                var text    = string.Format("RCPT TO:<{0}>", recipient.Address.FullAddress);
                var command = new SmtpCommand(text);
                reader = SendAndReceive(command);

                if (reader.IsFatalOrErroneous)
                {
                    return(reader);
                }
            }

            return(reader);
        }
예제 #7
0
        private SmtpResponseReader SendData(Message message)
        {
            SendAndReceive(new SmtpCommand("DATA"));

            var mime = message.ToMime();
            using (var reader = new StringReader(mime)) {
                var chunkSize = UpdateUploadProgressTriggerChunkSize.Bytes;
                var trigger = chunkSize;
                var current = 0;
                var total = mime.Length;

                var command = new SmtpCommand(string.Empty);
                while (true) {
                    var line = reader.ReadLine();
                    if (line == null) {
                        break;
                    }

                    command.Text = StuffPeriodIfNecessary(line);
                    Send(command);

                    // dont forget the newline characters
                    current += command.Text.Length + 2;

                    if (trigger - current < 0) {
                        InvokeUploadProgressChanged(current, total);
                        trigger += chunkSize;
                    }

                    if (current >= total) {
                        InvokeUploadProgressChanged(total, total);
                    }
                }
                SendDataTermination();
            }

            return Receive();
        }
예제 #8
0
        private SmtpServerCapability Hello()
        {
            var host = Dns.GetHostName();
            var command = new SmtpCommand("EHLO " + host);
            Send(command);

            // Timeout suggestion
            // http://tools.ietf.org/html/rfc5321#section-4.5.3.2.1
            var timeout = TimeSpan.FromMinutes(5);
            var response = Receive(timeout);

            // fallback to HELO if EHLO is not supported
            if (response.IsFatalOrErroneous) {
                command = new SmtpCommand("HELO " + host);
                Send(command);
                response = Receive(timeout);
            }

            return StaticResponseParser.ParseCapability(response);
        }
예제 #9
0
 internal SmtpResponseReader SendAndReceive(SmtpCommand command)
 {
     Send(command);
     return Receive();
 }
예제 #10
0
 internal void Send(SmtpCommand command)
 {
     WriteLine(command.Text);
 }
예제 #11
0
        public bool AuthenticateXOAuth(string key)
        {
            var command = new SmtpCommand(string.Format("AUTH XOAUTH {0}", key));

            return(_client.SendAndReceive(command).IsOk);
        }
예제 #12
0
        /// <summary>
        ///   Cram-MD5 authorization.
        ///   http://tools.ietf.org/html/rfc2195
        /// </summary>
        private bool AuthenticateCramMd5(NetworkCredential credentials)
        {
            var command = new SmtpCommand("AUTH CRAM-MD5");
            var response = _client.SendAndReceive(command);

            var base64 = response.CurrentLine.Substring(4).TrimEnd();
            var challenge = Base64Encoder.Decode(base64, Encoding.UTF8);

            var username = credentials.UserName;
            var password = credentials.Password;

            var hash = CramMd5Hasher.ComputeHash(password, challenge);

            var authentication = username + " " + hash;
            var reader = _client.SendAndReceive(new SmtpCommand(Base64Encoder.Encode(authentication)));
            return reader.IsOk;
        }
예제 #13
0
 internal SmtpResponseReader SendAndReceive(SmtpCommand command)
 {
     Send(command);
     return(Receive());
 }
예제 #14
0
 internal void Send(SmtpCommand command)
 {
     WriteLine(command.Text);
 }
예제 #15
0
        /// <summary>
        ///   Login authorization, same as plain but split into two separate command/responses.
        ///   http://tools.ietf.org/html/rfc4616
        /// </summary>
        private bool AuthenticateLogin(NetworkCredential credentials)
        {
            var command = new SmtpCommand("AUTH LOGIN");
            var response = _client.SendAndReceive(command);

            if (response.ResponseCode != 334) {
                return false;
            }

            var username = Base64Encoder.Encode(credentials.UserName);
            var userNameResponse = _client.SendAndReceive(new SmtpCommand(username));

            if (userNameResponse.ResponseCode != 334) {
                return false;
            }

            var password = Base64Encoder.Encode(credentials.Password);
            var passwordResponse = _client.SendAndReceive(new SmtpCommand(password));
            return passwordResponse.ResponseCode == ResponseCodes.AuthenticationSuccessful;
        }
예제 #16
0
        private SmtpResponseReader SendRecipients(Message message)
        {
            SmtpResponseReader reader = null;
            var recipients = new List<EmailContact>();
            recipients.AddRange(message.Ccs);
            recipients.AddRange(message.Bccs);
            recipients.AddRange(message.To);
            recipients.RemoveDuplicates();

            foreach (var recipient in recipients) {
                var text = string.Format("RCPT TO:<{0}>", recipient.Address.FullAddress);
                var command = new SmtpCommand(text);
                reader = SendAndReceive(command);

                if (reader.IsFatalOrErroneous) {
                    return reader;
                }
            }

            return reader;
        }
예제 #17
0
        private SmtpResponseReader SendSenders(Message message)
        {
            SmtpResponseReader reader = null;
            foreach (var from in message.From) {
                var text = string.Format("MAIL FROM:<{0}>", from.Address.FullAddress);
                var command = new SmtpCommand(text);
                reader = SendAndReceive(command);

                if (reader.IsFatalOrErroneous) {
                    return reader;
                }
            }
            return reader;
        }
예제 #18
0
 public bool AuthenticateXOAuth(string key)
 {
     var command = new SmtpCommand(string.Format("AUTH XOAUTH {0}", key));
     return _client.SendAndReceive(command).IsOk;
 }