コード例 #1
0
        /// <summary>
        /// Authenticates a socket client using plain authentication.
        /// </summary>
        /// <returns>True if the client was successfully authenticated. False otherwise.</returns>
        public async Task <Boolean> AuthenticateByPlain()
        {
            if (!IsConnected)
            {
                return(false);
            }

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

            if (!response.Contains(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.Contains(SmtpCode.AuthenticationSuccessful))
            {
                return(false);
            }

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

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

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

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

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

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

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

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Connects to the server.
        /// </summary>
        /// <returns>True for successful connection. False otherwise.</returns>
        public async Task <bool> Connect()
        {
            try
            {
                if (IsConnected)
                {
                    Socket.Close();
                    IsConnected = false;
                }

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

                SmtpResponse response = await Socket.EstablishConnection();

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

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

            return(false);
        }
コード例 #4
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 <bool> SendMail(SmtpMessage message)
        {
            if (!IsConnected)
            {
                await Connect();
            }

            if (!IsConnected)
            {
                throw new Exception("Can't connect to the SMTP server.");
            }

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

            SmtpResponse response = await Socket.Send(string.Format("Mail From:<{0}>", message.From.EmailAddress));

            if (!response.Contains(SmtpCode.RequestedMailActionCompleted))
            {
                return(false);
            }

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

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

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

            if (!responseData.Contains(SmtpCode.StartMailInput))
            {
                return(false);
            }

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

            if (!repsonseMessage.Contains(SmtpCode.RequestedMailActionCompleted))
            {
                return(false);
            }

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

            if (!responseQuit.Contains(SmtpCode.ServiceClosingTransmissionChannel))
            {
                return(false);
            }

            return(true);
        }
コード例 #5
0
        /// <summary>
        /// Authenticates a socket client using the specified Username and Password.
        /// </summary>
        /// <returns>True if the client was successfully authenticated. False otherwise.</returns>
        public async Task <bool> Authenticate()
        {
            if (!IsConnected)
            {
                throw new Exception("Client is not connected");
            }

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

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

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

                    return(await Authenticate());
                }
            }

            if (response.Contains("AUTH"))
            {
                if (response.Contains("LOGIN"))
                {
                    IsAuthenticated = await AuthenticateByLogin();
                }
                else if (response.Contains("PLAIN"))
                {
                    IsAuthenticated = await AuthenticateByPlain();
                }
            }
            else
            {
                await Socket.Send("EHLO " + Server);

                IsAuthenticated = true;
            }

            return(IsAuthenticated);
        }