Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Sends the specified email message.
        /// </summary>
        /// <returns>True if the message was sent successfully. False otherwise.</returns>
        public async Task <bool> SendAsync()
        {
            SmtpClient  client  = new SmtpClient(Server, Port, SSL, Username, Password);
            SmtpMessage message = new SmtpMessage(From, To, null, Subject, Message);

            bool result = await client.SendMail(message);

            return(result);
        }
Пример #3
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;
        }
Пример #4
0
        /// <summary>
        /// Sends the specified email message.
        /// </summary>
        /// <returns>True if the message was sent successfully. False otherwise.</returns>
        public async Task<bool> SendAsync()
        {
            SmtpClient client = new SmtpClient(Server, Port, SSL, Username, Password);
            SmtpMessage message = new SmtpMessage(From, To, null, Subject, Message);

            bool result = await client.SendMail(message);

            return result;
        }