Send() public method

The send.
public Send ( string message ) : IAsyncResult
message string /// The message. ///
return IAsyncResult
Example #1
0
        /// <summary>
        /// The rcpt.
        /// </summary>
        /// <param name="connection">
        /// The connection. 
        /// </param>
        /// <param name="parts">
        /// The parts. 
        /// </param>
        private static void RCPT(Connection connection, string[] parts)
        {
            string line = string.Join(" ", parts);

            // Check for the ":"
            if (!line.ToUpper().StartsWith("RCPT TO") || !line.Contains(":"))
            {
                connection.Send("504 Command parameter not implemented");
                return;
            }

            // Check command order
            if (connection.Session.Sender == null || connection.Session.MailFrom == null)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            string address = line.Substring(line.IndexOf(":") + 1).Replace("<", string.Empty).Replace(">", string.Empty).Trim();
            if (!connection.Session.Recipients.Contains(address))
            {
                connection.Session.Recipients.Add(address);
            }

            connection.Send("250 <{0}> OK", address);
        }
Example #2
0
        /// <summary>
        /// The data.
        /// </summary>
        /// <param name="connection">
        /// The connection. 
        /// </param>
        private void DATA(Connection connection)
        {
            // Check command order
            if (connection.Session.Sender == null || connection.Session.MailFrom == null
                    || connection.Session.Recipients.Count == 0)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            string file = null;

            try
            {
                Stream networkStream = new NetworkStream(connection.Client, false);

                var output = new List<string>();

                using (var reader = new StreamReader(networkStream))
                {
                    string line;
                    connection.Send("354 Start mail input; end with <CRLF>.<CRLF>").AsyncWaitHandle.WaitOne();

                    while ((line = reader.ReadLine()) != ".")
                    {
                        // reverse any dot-stuffing per RFC 2821, section 4.5.2
                        if (line.StartsWith(".") && line.Length > 1)
                        {
                            line = line.Substring(1);
                        }

                        output.Add(line);
                    }

                    reader.Close();
                }

                file = _messageFileService.SaveMessage(output);
            }
            catch (IOException e)
            {
                Logger.WriteWarning(
                    "IOException received in Processor.DATA while reading message.  Closing connection.  Message: " + e.Message,
                    connection.ConnectionId);
                connection.Close();
                return;
            }

            OnMessageReceived(connection, file);

            connection.Send("250 OK");
        }
Example #3
0
 /// <summary>
 /// The helo.
 /// </summary>
 /// <param name="connection">
 /// The connection. 
 /// </param>
 /// <param name="parts">
 /// The parts. 
 /// </param>
 private static void HELO(Connection connection, string[] parts)
 {
     connection.Session.Sender = parts.Length < 2 ? string.Empty : parts[1];
     connection.Send("250 {0}", Dns.GetHostName().ToLower());
 }
Example #4
0
        /// <summary>
        /// The mail.
        /// </summary>
        /// <param name="connection">
        /// The connection. 
        /// </param>
        /// <param name="parts">
        /// The parts. 
        /// </param>
        private static void MAIL(Connection connection, string[] parts)
        {
            string line = string.Join(" ", parts);

            // Check for the right number of parameters
            if (parts.Length < 2)
            {
                connection.Send("504 Command parameter not implemented");
                return;
            }

            // Check for the ":"
            if (!parts[1].ToUpper().StartsWith("FROM") || !line.Contains(":"))
            {
                connection.Send("504 Command parameter not implemented");
                return;
            }

            // Check command order
            if (connection.Session.Sender == null)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            // Set the from settings
            connection.Session.Reset();
            string address = line.Substring(line.IndexOf(":") + 1).Replace("<", string.Empty).Replace(">", string.Empty).Trim();
            connection.Session.MailFrom = address;

            // Check for encoding
            foreach (string part in parts.Where(part => part.ToUpper().StartsWith("BODY=")))
            {
                switch (part.ToUpper().Replace("BODY=", string.Empty).Trim())
                {
                    case "8BITMIME":
                        connection.Session.UseUtf8 = true;
                        break;
                    default:
                        connection.Session.UseUtf8 = false;
                        break;
                }

                break;
            }

            connection.Send("250 <{0}> OK", address);
        }
Example #5
0
        /// <summary>
        /// The process command.
        /// </summary>
        /// <param name="connection">
        /// The connection. 
        /// </param>
        /// <param name="data">
        /// The data. 
        /// </param>
        public void ProcessCommand(Connection connection, object data)
        {
            if (data is byte[])
            {
                connection.Session.Message = (byte[])data;
                connection.Send("250 OK");
            }
            else
            {
                // Command mode
                var command = (string)data;
                string[] parts = command.Split(' ');

                switch (parts[0].ToUpper())
                {
                    case "HELO":
                        HELO(connection, parts);
                        break;

                    case "EHLO":
                        EHLO(connection, parts);
                        break;

                    case "SEND":
                    case "SOML":
                    case "SAML":
                    case "MAIL":
                        MAIL(connection, parts);
                        break;

                    case "RCPT":
                        RCPT(connection, parts);
                        break;

                    case "DATA":
                        DATA(connection);
                        break;

                    case "VRFY":
                        connection.Send("252 Cannot VRFY user, but will accept message and attempt delivery");
                        break;

                    case "EXPN":
                        connection.Send("252 Cannot expand upon list");
                        break;

                    case "RSET":
                        connection.Session.Reset();
                        connection.Send("250 OK");
                        break;

                    case "NOOP":
                        connection.Send("250 OK");
                        break;

                    case "QUIT":
                        connection.Send("221 Goodbye!");
                        connection.Close();
                        break;

                    case "HELP":
                    case "TURN":
                        connection.Send("502 Command not implemented");
                        break;

                    default:
                        connection.Send("500 Command not recognized");
                        break;
                }
            }
        }
Example #6
0
        /// <summary>
        /// The data.
        /// </summary>
        /// <param name="connection">
        /// The connection. 
        /// </param>
        private void DATA(Connection connection)
        {
            // Check command order
            if (connection.Session.Sender == null || connection.Session.MailFrom == null
                    || connection.Session.Recipients.Count == 0)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            string file;

            do
            {
                // the file must not exists.  the resolution of DataTime.Now may be slow w.r.t. the speed of the received files
                file = Path.Combine(
                    mailFolder,
                    string.Format("{0}-{1}.eml", DateTime.Now.ToString("yyyyMMddHHmmssFF"), Guid.NewGuid().ToString().Substring(0, 2)));
            }
            while (File.Exists(file));

            try
            {
                Stream networkStream = new NetworkStream(connection.Client, false);

                using (var reader = new StreamReader(networkStream))
                {
                    using (var writer = new StreamWriter(file))
                    {
                        string line;

                        connection.Send("354 Start mail input; end with <CRLF>.<CRLF>").AsyncWaitHandle.WaitOne();

                        while ((line = reader.ReadLine()) != ".")
                        {
                            writer.WriteLine(line);
                        }

                        writer.Close();
                    }

                    reader.Close();
                }
            }
            catch (IOException e)
            {
                Logger.WriteWarning(
                    "IOException received in Processor.DATA while reading message.  Closing connection.  Message: " + e.Message,
                    connection.ConnectionId);
                connection.Close();
                return;
            }

            OnMessageReceived(connection, file);

            connection.Send("250 OK");
        }
Example #7
0
        private static void DATA(Connection connection)
        {
            // Check command order
            if (connection.Session.Sender == null || connection.Session.MailFrom == null || connection.Session.Recipients.Count == 0)
            {
                connection.Send("503 Bad sequence of commands");
                return;
            }

            string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmssFF") + ".eml");

            try
            {
                Stream networkStream = new NetworkStream(connection.Client, false);
                StreamReader reader = new StreamReader(networkStream);
                StreamWriter writer = new StreamWriter(file);
                string line;

                connection.Send("354 Start mail input; end with <CRLF>.<CRLF>").AsyncWaitHandle.WaitOne();

                while ((line = reader.ReadLine()) != ".")
                    writer.WriteLine(line);

                writer.Close();
                reader.Close();
            }
            catch(IOException e)
            {
                Logger.WriteWarning("IOException received in Processor.DATA while reading message.  Closing connection.  Message: " + e.Message, connection.ConnectionId);
                connection.Close();
                return;
            }

            OnMessageReceived(connection, file);

            connection.Send("250 OK");
        }
Example #8
0
        /// <summary>
        /// The process command.
        /// </summary>
        /// <param name="connection">
        /// The connection.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        public void ProcessCommand(Connection connection, object data)
        {
            if (data is byte[])
            {
                connection.Session.Message = (byte[])data;
                connection.Send("250 OK");
            }
            else
            {
                // Command mode
                var      command = (string)data;
                string[] parts   = command.Split(' ');

                switch (parts[0].ToUpper())
                {
                case "HELO":
                    HELO(connection, parts);
                    break;

                case "EHLO":
                    EHLO(connection, parts);
                    break;

                case "SEND":
                case "SOML":
                case "SAML":
                case "MAIL":
                    MAIL(connection, parts);
                    break;

                case "RCPT":
                    RCPT(connection, parts);
                    break;

                case "DATA":
                    DATA(connection);
                    break;

                case "VRFY":
                    connection.Send("252 Cannot VRFY user, but will accept message and attempt delivery");
                    break;

                case "EXPN":
                    connection.Send("252 Cannot expand upon list");
                    break;

                case "RSET":
                    connection.Session.Reset();
                    connection.Send("250 OK");
                    break;

                case "NOOP":
                    connection.Send("250 OK");
                    break;

                case "QUIT":
                    connection.Send("221 Goodbye!");
                    connection.Close();
                    break;

                case "HELP":
                case "TURN":
                    connection.Send("502 Command not implemented");
                    break;

                default:
                    connection.Send("500 Command not recognized");
                    break;
                }
            }
        }
Example #9
0
 /// <summary>
 /// The helo.
 /// </summary>
 /// <param name="connection">
 /// The connection.
 /// </param>
 /// <param name="parts">
 /// The parts.
 /// </param>
 private static void HELO(Connection connection, string[] parts)
 {
     connection.Session.Sender = parts.Length < 2 ? string.Empty : parts[1];
     connection.Send("250 {0}", Dns.GetHostName().ToLower());
 }