Example #1
0
 private async Task AuthenticateAsync(ISmtpConfig config)
 {
     try
     {
         await Smtp.AuthenticateAsync(config.Mail, config.SenderPassword);
     }
     catch (Exception ex)
     {
         throw new SmtpNotAuthenticatedException("Could not authenticate at smtp server", ex);
     }
 }
Example #2
0
        /// <summary>
        /// Connect to the server and display mailbox statistics.
        /// </summary>
        private async void Connect(object sender, EventArgs e)
        {
            _window.Busy = true;
            Smtp client = null;

            try
            {
                client = new Smtp();
                await client.ConnectAsync(ServerName, ServerPort, Mode);
            }
            catch (Exception ex)
            {
                _window.Busy = false;
                Util.ShowException(ex);
                return;
            }

            if (!string.IsNullOrEmpty(UserName))
            {
                try
                {
                    await client.AuthenticateAsync(UserName, Password);
                }
                catch (Exception ex)
                {
                    _window.Busy = false;
                    Util.ShowException(ex);
                    return;
                }
            }

            try
            {
                await client.SendAsync(_message);
            }
            catch (Exception ex)
            {
                _window.Busy = false;
                Util.ShowException(ex);
                return;
            }
            _window.Busy = false;
            Util.ShowMessage("Message sent!", "Message sent successfully.");
        }
Example #3
0
        private void LoginSmtp()
        {
            var secureSocketOptions = SecureSocketOptions.Auto;
            var sslProtocols        = SslProtocols.Default;

            switch (Account.SmtpEncryption)
            {
            case EncryptionType.StartTLS:
                secureSocketOptions = SecureSocketOptions.StartTlsWhenAvailable;
                sslProtocols        = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
                break;

            case EncryptionType.SSL:
                secureSocketOptions = SecureSocketOptions.SslOnConnect;
                sslProtocols        = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
                break;

            case EncryptionType.None:
                secureSocketOptions = SecureSocketOptions.None;
                sslProtocols        = SslProtocols.None;
                break;
            }


            Log.Debug("Smtp.Connect({0}:{1}, {2})", Account.SmtpServer, Account.SmtpPort,
                      Enum.GetName(typeof(SecureSocketOptions), secureSocketOptions));
            try
            {
                Smtp.SslProtocols = sslProtocols;

                var t = Smtp.ConnectAsync(Account.SmtpServer, Account.SmtpPort, secureSocketOptions, CancelToken);

                if (!t.Wait(CONNECT_TIMEOUT, CancelToken))
                {
                    throw new TimeoutException("Smtp.ConnectAsync timeout");
                }

                if (!Account.SmtpAuth)
                {
                    if ((Smtp.Capabilities & SmtpCapabilities.Authentication) != 0)
                    {
                        throw new AuthenticationException("SmtpAuth is required (setup Authentication Type)");
                    }

                    return;
                }

                Smtp.Authenticated += SmtpOnAuthenticated;

                if (string.IsNullOrEmpty(Account.OAuthToken))
                {
                    Log.Debug("Smtp.Authentication({0})", Account.SmtpAccount);

                    Smtp.AuthenticationMechanisms.Remove("XOAUTH2");

                    t = Smtp.AuthenticateAsync(Account.SmtpAccount, Account.SmtpPassword, CancelToken);
                }
                else
                {
                    Log.Debug("Smtp.AuthenticationByOAuth({0})", Account.SmtpAccount);

                    t = Smtp.AuthenticateAsync(Account.SmtpAccount, Account.AccessToken, CancelToken);
                }

                if (!t.Wait(LOGIN_TIMEOUT, CancelToken))
                {
                    Smtp.Authenticated -= SmtpOnAuthenticated;
                    throw new TimeoutException("Smtp.AuthenticateAsync timeout");
                }

                Smtp.Authenticated -= SmtpOnAuthenticated;
            }
            catch (AggregateException aggEx)
            {
                if (aggEx.InnerException != null)
                {
                    throw aggEx.InnerException;
                }
                throw new Exception("LoginSmtp failed", aggEx);
            }
        }
Example #4
0
        /// <summary>
        /// Connects to the SMTP server and sends the saved message asynchronously.
        /// </summary>
        async Task <bool> ConnectAndSend(
            string serverName,
            int port,
            SecurityMode security,
            string userName,
            string password
            )
        {
            try
            {
                _client = new Smtp();

                SetStatus("Connecting to SMTP server {0}:{1}...", serverName, port);
                // Connect to the SMTP server
                await _client.ConnectAsync(serverName, port, security);
            }
            catch (Exception ex)
            {
                HandleException(ex, string.Format("Error while connecting to the SMTP server {0}. ", serverName));
                return(false);
            }

            // Only authenticate when both username and password are specified.
            if (!string.IsNullOrEmpty(userName) &&
                !string.IsNullOrEmpty(password))
            {
                try
                {
                    SetStatus("Authenticating user {0}...", userName);
                    // Authenticate the user
                    await _client.AuthenticateAsync(userName, password);
                }
                catch (Exception ex)
                {
                    HandleException(ex, string.Format("Error while authenticating user {0}. ", userName));
                    return(false);
                }
            }

            #region SendMessage

            // Load message from the Intent's data.
            byte[] messageData = Intent.GetByteArrayExtra("message");
            var    message     = new MailMessage();
            message.Load(messageData);

            try
            {
                SetStatus("Sending mail message...");
                _client.Send(message);
            }
            catch (Exception ex)
            {
                HandleException(ex, "Error while sending message. ");
                return(false);
            }
            finally
            {
                // Disconnect from the SMTP server.
                Disconnect();
            }

            #endregion

            return(true);
        }