Beispiel #1
0
        /// <summary>
        /// Authenticates a socket client using plain authentication.
        /// </summary>
        /// <returns>True if the client was successfully authenticated. False otherwise.</returns>
        private async Task <bool> AuthenticateByPlainAsync()
        {
            if (!IsConnected)
            {
                return(false);
            }

            SmtpResponse response = await Socket.Send("Auth Plain");

            if (!response.ContainsStatus(SmtpCode.WaitingForAuthentication))
            {
                return(false);
            }

            string lineAuthentication = string.Format("{0}\0{0}\0{1}", Username, Password);

            SmtpResponse responseAuth = await Socket.Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(lineAuthentication)));

            if (!responseAuth.ContainsStatus(SmtpCode.AuthenticationSuccessful))
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Authenticates a socket client using login authentication.
        /// </summary>
        /// <returns>True if the client was successfully authenticated. False otherwise.</returns>
        private async Task <bool> AuthenticateByLoginAsync()
        {
            if (!IsConnected)
            {
                return(false);
            }

            SmtpResponse response = await Socket.Send("Auth Login");

            if (!response.ContainsStatus(SmtpCode.WaitingForAuthentication))
            {
                return(false);
            }

            SmtpResponse responseUsername = await Socket.Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(Username)));

            if (!responseUsername.ContainsStatus(SmtpCode.WaitingForAuthentication))
            {
                return(false);
            }

            SmtpResponse responsePassword = await Socket.Send(Convert.ToBase64String(Encoding.UTF8.GetBytes(Password)));

            if (!responsePassword.ContainsStatus(SmtpCode.AuthenticationSuccessful))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Connects to the server.
        /// </summary>
        /// <returns>True for successful connection. False otherwise.</returns>
        private async Task <bool> ConnectAsync()
        {
            try
            {
                if (IsConnected)
                {
                    Socket.Dispose();
                    IsConnected = false;
                }

                Socket = new SmtpSocket(Server, Port, SSL);

                SmtpResponse response = await Socket.EstablishConnection();

                if (response.ContainsStatus(SmtpCode.ServiceReady))
                {
                    IsConnected = true;

                    return(true);
                }
            }
            catch
            {
                return(false);
            }

            return(false);
        }
Beispiel #4
0
        /// <summary>
        /// Authenticates a socket client using the specified Username and Password.
        /// </summary>
        /// <returns>True if the client was successfully authenticated. False otherwise.</returns>
        private async Task <bool> AuthenticateAsync()
        {
            if (!IsConnected)
            {
                return(false);
            }

            // get the type of auth
            SmtpResponse response = await Socket.Send("EHLO " + Server);

            if (response.ContainsMessage("STARTTLS"))
            {
                SmtpResponse responseSSL = await Socket.Send("STARTTLS");

                if (responseSSL.ContainsStatus(SmtpCode.ServiceReady))
                {
                    await Socket.UpgradeToSslAsync();

                    return(await AuthenticateAsync());
                }
            }

            if (response.ContainsMessage("AUTH"))
            {
                if (response.ContainsMessage("LOGIN"))
                {
                    IsAuthenticated = await AuthenticateByLoginAsync();
                }
                else if (response.ContainsMessage("PLAIN"))
                {
                    IsAuthenticated = await AuthenticateByPlainAsync();
                }
            }
            else
            {
                await Socket.Send("EHLO " + Server);

                IsAuthenticated = true;
            }

            return(IsAuthenticated);
        }
Beispiel #5
0
        /// <summary>
        /// Sends the specified email message.
        /// </summary>
        /// <param name="message">The email message.</param>
        /// <returns>True if the email was sent successfully. False otherwise.</returns>
        public async Task <SmtpResult> SendMailAsync(EmailMessage message)
        {
            if (!IsConnected)
            {
                await ConnectAsync();
            }

            if (!IsConnected)
            {
                return(SmtpResult.ConnectionFailed);
            }

            if (!IsAuthenticated)
            {
                await AuthenticateAsync();
            }

            if (!IsAuthenticated)
            {
                return(SmtpResult.AuthenticationFailed);
            }

            SmtpResponse response = message.Sender != null && message.Sender.Address.Length != 0 ?
                                    await Socket.Send(string.Format("Mail From:<{0}>", message.Sender.Address)) :
                                    await Socket.Send(string.Format("Mail From:<{0}>", Username));

            if (!response.ContainsStatus(SmtpCode.RequestedMailActionCompleted))
            {
                return(SmtpResult.CouldNotCreateMail);
            }

            foreach (EmailRecipient to in message.To)
            {
                SmtpResponse responseTo = await Socket.Send(string.Format("Rcpt To:<{0}>", to.Address));

                if (!responseTo.ContainsStatus(SmtpCode.RequestedMailActionCompleted))
                {
                    break;
                }
            }

            foreach (EmailRecipient to in message.CC)
            {
                SmtpResponse responseTo = await Socket.Send(string.Format("Rcpt To:<{0}>", to.Address));

                if (!responseTo.ContainsStatus(SmtpCode.RequestedMailActionCompleted))
                {
                    break;
                }
            }

            foreach (EmailRecipient to in message.Bcc)
            {
                SmtpResponse responseTo = await Socket.Send(string.Format("Rcpt To:<{0}>", to.Address));

                if (!responseTo.ContainsStatus(SmtpCode.RequestedMailActionCompleted))
                {
                    break;
                }
            }

            SmtpResponse responseData = await Socket.Send(string.Format("Data"));

            if (!responseData.ContainsStatus(SmtpCode.StartMailInput))
            {
                return(SmtpResult.CouldNotCreateMail);
            }

            SmtpResponse repsonseMessage = await Socket.Send(await BuildSmtpMailInput(message));

            if (!repsonseMessage.ContainsStatus(SmtpCode.RequestedMailActionCompleted))
            {
                return(SmtpResult.CouldNotCreateMail);
            }

            SmtpResponse responseQuit = await Socket.Send("Quit");

            if (!responseQuit.ContainsStatus(SmtpCode.ServiceClosingTransmissionChannel))
            {
                return(SmtpResult.CouldNotCloseTransmissionChannel);
            }

            return(SmtpResult.OK);
        }