Exemple #1
0
        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            if (buttonRefreshClicked == true)
            {
                return;
            }

            if (MailReceiver != null)
            {
                buttonRefreshClicked         = true;
                toolStripButtonRefresh.Image = MailClient.Properties.Resources.loading;
                Task.Run(() =>
                {
                    if (CurrentView == EmailView.Inbox)
                    {
                        ReceivedEmails = MailReceiver.GetInboxEmailList();
                    }
                    else if (CurrentView == EmailView.SentEmails)
                    {
                        SentEmails = MailReceiver.GetSentEmailList();
                    }
                    if (CurrentView == EmailView.CustomFolder)
                    {
                        LoadCustomFolderEmails(OpenedCustomFolderName);
                        toolStripButtonRefresh.Owner.Invoke((Action)(() => toolStripButtonRefresh.Image = MailClient.Properties.Resources.Refresh));
                        buttonRefreshClicked = false;
                        return;
                    }
                    LoadEmails(CurrentView);
                    toolStripButtonRefresh.Owner.Invoke((Action)(() => toolStripButtonRefresh.Image = MailClient.Properties.Resources.Refresh));
                    buttonRefreshClicked = false;
                });
            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            var serverInfo = new ServerInfo(ServerInfo.ServerPreset.Gmail);

            if (radioButtonChoosePreset.Checked)
            {
                if (comboBoxServer.Text == "Yandex")
                {
                    serverInfo = new ServerInfo(ServerInfo.ServerPreset.Yandex);
                }
            }
            else if (radioButtonCustomServer.Checked)
            {
                if (!Regex.IsMatch(textBoxImapPort.Text, @"^\d+$") || !Regex.IsMatch(textBoxSmtpPort.Text, @"^\d+$"))
                {
                    MessageBox.Show("IMAP and SMTP ports must be numberic value.", "Failed");
                    return;
                }

                serverInfo = new ServerInfo(textBoxImap.Text,
                                            Convert.ToInt32(textBoxImapPort.Text), textBoxSmtp.Text, Convert.ToInt32(textBoxSmtpPort.Text));
            }

            btnLogin.Enabled          = false;
            tbUsername.Enabled        = false;
            tbPassword.Enabled        = false;
            groupBox1.Enabled         = false;
            pictureBoxLoading.Visible = true;

            Task.Run(() =>
            {
                try
                {
                    var mailReceiver = new MailReceiver(serverInfo.ImapServer, serverInfo.ImapPort, true, tbUsername.Text, tbPassword.Text, _parentForm);
                    mailReceiver.Connect();

                    this.Invoke((Action)(() => this.Hide()));

                    if (!mailReceiver.Login.Contains("@"))
                    {
                        _parentForm.Invoke((Action)(() =>
                                                    _parentForm.toolStripStatusLabel.Text = "Logged In - " + serverInfo.LoginSuffix));
                    }
                    else
                    {
                        _parentForm.Invoke((Action)(() =>
                                                    _parentForm.toolStripStatusLabel.Text = "Logged In - " + mailReceiver.Login));
                    }

                    _parentForm.ServerInfo = serverInfo;
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = true));

                    _parentForm.MailReceiver   = mailReceiver;
                    _parentForm.ReceivedEmails = mailReceiver.GetInboxEmailList();
                    _parentForm.SentEmails     = mailReceiver.GetSentEmailList();
                    _parentForm.LoadEmails(EmailView.Inbox);

                    this.Invoke((Action)(() => btnLogout.Enabled = true));


                    _parentForm.Invoke((Action)(() => _parentForm.SetEnableToolbarButtons(true)));
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = false));
                    this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                }
                catch (Exception ex)
                {
                    this.Invoke((Action)(() => btnLogin.Enabled = true));
                    this.Invoke((Action)(() => tbUsername.Enabled = true));
                    this.Invoke((Action)(() => tbPassword.Enabled = true));
                    this.Invoke((Action)(() => groupBox1.Enabled = true));
                    this.Invoke((Action)(() => pictureBoxLoading.Visible = false));
                    this.Invoke((Action)(() => MessageBox.Show(ex.Message, "Login Failed")));
                    _parentForm.Invoke((Action)(() => _parentForm.panelLoading.Visible = false));
                }
            });
        }