Exemplo n.º 1
0
        /// <summary>
        /// Read Next Line
        /// </summary>
        /// <returns></returns>
        private string ReadLine()
        {
            try
            {
                Reader.BaseStream.ReadTimeout = ReadTimeout;
                var line = "[S]: " + SMTPResponse.FixMessage(Reader.ReadLine());
                Log.Add(line);

                return(line);
            }
            catch (Exception excp)
            {
                return(null);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Send Data and read to end. Returns TRUE if expected code is equals to the actual response code.
 /// </summary>
 /// <param name="data">Data to send</param>
 /// <param name="expectedResponse">Expected Response Code</param>
 /// <returns></returns>
 private bool SendAndReadAll(string data, SMTPResponse.ResponseCode expectedResponse)
 {
     Send(data);
     return(SMTPResponse.GetResponseCode(Read(), expectedResponse));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Send Email. Returns TRUE if succeeds. Returns FALSE if fails.
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool SendEmail(Email email)
        {
            Client = new Socket(IPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            Client.Connect(Hostname, Port);

            Stream = new NetworkStream(Client);

            if (UseSSL)
            {
                Log.Add($"<Connecting via SSL to {Hostname}:{Port}>");

                SSLStream = new SslStream(Stream, true, new RemoteCertificateValidationCallback(ValidateSSLCertificate), null);

                try
                {
                    SSLStream.AuthenticateAsClient(Hostname);
                }
                catch (Exception excp)
                {
                    Log.Add($"<Invalid SSL Certificate {Hostname}:{Port} ({Enum.GetName(typeof(SslPolicyErrors), SSLError)})>");
                    return(false);
                }

                Reader = new StreamReader(SSLStream);
                Writer = new StreamWriter(SSLStream)
                {
                    AutoFlush = true
                };
            }
            else
            {
                Log.Add($"<Connecting to {Hostname}:{Port}>");

                Reader = new StreamReader(Stream);
                Writer = new StreamWriter(Stream)
                {
                    AutoFlush = true
                };
            }


            // Connection Message
            // Expecting 220
            if (!SMTPResponse.GetResponseCode(ReadLine(), SMTPResponse.ResponseCode.ServiceReady))
            {
                Log.Add($"<Cannot establish connection to {Hostname}:{Port}>");
                return(false);
            }

            // Send greetings
            // Expecting 250
            if (!SendAndReadAll("EHLO " + Hostname, SMTPResponse.ResponseCode.OK))
            {
                Log.Add($"<Error after greetings {Hostname}:{Port}>");
                return(false);
            }


            // Do Login if required
            if (DoLogin)
            {
                switch (Authentication)
                {
                case AuthType.Login:
                    if (!AuthLogin())
                    {
                        Log.Add($"<Authentication Error {Hostname}:{Port}>");
                        return(false);
                    }
                    break;
                }
            }

            // Send MAIL FROM
            // Send RCPT TO
            if (!SendSenderAndRecipientAddresses(email))
            {
                Log.Add($"<Error after MAIL FROM & RCPT TO {Hostname}:{Port}>");
                return(false);
            }


            // Send DATA
            // Expecting 354
            if (!SendAndReadLine("DATA", SMTPResponse.ResponseCode.StartMailInput))
            {
                Log.Add($"<Error after DATA {Hostname}:{Port}>");
                return(false);
            }

            // Send Headers
            // Expecting nothing
            var headers = email.Headers.GetAllHeaders(email.Headers);

            foreach (var header in headers)
            {
                Send(header);
            }


            // Send Email body
            // Expecting nothing
            Send(email.EmailBody);

            // Send the end of message
            // Expecting 250
            var emailSent = SendEndOfMessage();

            if (emailSent)
            {
                Log.Add("<E-Mail sent>");
            }
            else
            {
                Log.Add("<E-Mail not sent>");
            }

            // Send QUIT
            // Expecting nothing
            Send("QUIT");

            return(emailSent);
        }