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; }); } }
public MailReceiver GetMailReceiverInstance() { MailReceiver mailReceiver = new MailReceiver(mailServer, port, ssl, Login, Password); mailReceiver.Connect(); return(mailReceiver); }
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { var result = MessageBox.Show($"Are you sure you want to delete {dataGridViewEmails.SelectedRows.Count} email/s?", "Delete", MessageBoxButtons.YesNoCancel); if (result != DialogResult.Yes) { return; } Task.Run(() => { if (CurrentView == EmailView.CustomFolder) { foreach (DataGridViewRow row in dataGridViewEmails.SelectedRows) { CollectionEmail collectionEmail; var selectedEmail = EmailCollection[(string)row.Cells[0].Value]; --FolderList[OpenedCustomFolderName].ItemCount; EmailCollection.TryRemove(selectedEmail.Id, out collectionEmail); } } else { foreach (DataGridViewRow row in dataGridViewEmails.SelectedRows) { if (CurrentView == EmailView.Inbox) { InboxEmail inboxEmail; var selectedEmail = ReceivedEmails[(string)row.Cells[0].Value]; MailReceiver.DeleteEmail(EmailType.Inbox, selectedEmail.UniqueId); ReceivedEmails.TryRemove(selectedEmail.Id, out inboxEmail); } else { SentEmail sentEmail; var selectedEmail = SentEmails[(string)row.Cells[0].Value]; MailReceiver.DeleteEmail(EmailType.SentEmails, selectedEmail.UniqueId); SentEmails.TryRemove(selectedEmail.Id, out sentEmail); } } } this.Invoke((Action)(() => toolStripButtonRefresh.PerformClick())); }); }
private void moveToToolStripMenuItem1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { Task.Run(() => { if (CurrentView == EmailView.CustomFolder) { foreach (DataGridViewRow row in dataGridViewEmails.SelectedRows) { var selectedEmail = GetCustomFolderList(OpenedCustomFolderName)[(string)row.Cells[0].Value]; var oldFolder = FolderList[selectedEmail.CustomFolderName]; --oldFolder.ItemCount; if (e.ClickedItem.Text == "Sent Emails") { var message = new MimeMessage(); message.From.Add(new MailboxAddress(selectedEmail.From)); message.To.Add(new MailboxAddress(selectedEmail.To)); message.Subject = selectedEmail.Subject; message.Body = new TextPart("plain") { Text = selectedEmail.TextBody }; message.Date = selectedEmail.Date; MailReceiver.AddMessageToFolder(EmailType.SentEmails, message); CollectionEmail collectionEmail; EmailCollection.TryRemove(selectedEmail.Id, out collectionEmail); } else if (e.ClickedItem.Text == "Inbox") { var message = new MimeMessage(); message.From.Add(new MailboxAddress(selectedEmail.From)); message.To.Add(new MailboxAddress(selectedEmail.To)); message.Subject = selectedEmail.Subject; message.Body = new TextPart("plain") { Text = selectedEmail.TextBody }; message.Date = selectedEmail.Date; MailReceiver.AddMessageToFolder(EmailType.Inbox, message); CollectionEmail collectionEmail; EmailCollection.TryRemove(selectedEmail.Id, out collectionEmail); } else { var selectedFolderId = FolderList.FirstOrDefault(x => x.Value.FolderName == e.ClickedItem.Text).Value.FolderName; var selectedFolder = FolderList[selectedFolderId]; ++selectedFolder.ItemCount; selectedEmail.CustomFolderName = selectedFolderId; } } } else { foreach (DataGridViewRow row in dataGridViewEmails.SelectedRows) { if (CurrentView == EmailView.Inbox) { var selectedEmail = ReceivedEmails[(string)row.Cells[0].Value]; var selectedFolderName = FolderList.FirstOrDefault(x => x.Value.FolderName == e.ClickedItem.Text).Value.FolderName; var selectedFolder = FolderList[selectedFolderName]; if (EmailCollection.ContainsKey(selectedEmail.Id)) { MessageBox.Show("This email already exists in collection.", "Moving failed"); return; } var email = MailReceiver.GetEmail(EmailType.Inbox, selectedEmail.UniqueId); ++selectedFolder.ItemCount; EmailCollection.TryAdd(selectedEmail.Id, new CollectionEmail { Id = selectedEmail.Id, From = selectedEmail.From, To = selectedEmail.To, Subject = selectedEmail.Subject, TextBody = email.TextBody, HtmlBody = email.HtmlBody, Date = selectedEmail.ArrivalTime, CustomFolderName = selectedFolderName, OriginalFolder = EmailType.Inbox, }); MailReceiver.DeleteEmail(EmailType.Inbox, selectedEmail.UniqueId); } else if (CurrentView == EmailView.SentEmails) { var selectedEmail = SentEmails[(string)row.Cells[0].Value]; var selectedFolderName = FolderList.FirstOrDefault(x => x.Value.FolderName == e.ClickedItem.Text).Value.FolderName; var selectedFolder = FolderList[selectedFolderName]; if (EmailCollection.ContainsKey(selectedEmail.Id)) { MessageBox.Show("This email already exists in collection.", "Moving failed"); return; } ++selectedFolder.ItemCount; var email = MailReceiver.GetEmail(EmailType.SentEmails, selectedEmail.UniqueId); ++selectedFolder.ItemCount; EmailCollection.TryAdd(selectedEmail.Id, new CollectionEmail { Id = selectedEmail.Id, From = selectedEmail.From, To = selectedEmail.To, Subject = selectedEmail.Subject, TextBody = email.TextBody, HtmlBody = email.HtmlBody, Date = selectedEmail.SentTime, CustomFolderName = selectedFolderName, OriginalFolder = EmailType.SentEmails, }); MailReceiver.DeleteEmail(EmailType.SentEmails, selectedEmail.UniqueId); } } } this.Invoke((Action)(() => toolStripButtonRefresh.PerformClick())); }); }
public void ImportEmailsFromServer(ServerInfo importFrom, EmailType sourceType, EmailType destinationType, string username, string password, string destinationFolderName = null) { MailReceiver mailReceiver = new MailReceiver(importFrom.ImapServer, importFrom.ImapPort, true, username, password, _parentForm); try { mailReceiver.Connect(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Login Failed"); return; } List <MimeMessage> messages = new List <MimeMessage>(); if (sourceType == EmailType.Inbox) { messages = mailReceiver.GetInboxMimeMessages(); } else if (sourceType == EmailType.SentEmails) { messages = mailReceiver.GetSentMimeMessages(); } switch (destinationType) { case EmailType.Inbox: { foreach (var email in messages) { _parentForm.MailReceiver.AddMessageToFolder(EmailType.Inbox, email); } _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick())); } break; case EmailType.SentEmails: { foreach (var email in messages) { _parentForm.MailReceiver.AddMessageToFolder(EmailType.SentEmails, email); } _parentForm.Invoke((Action)(() => _parentForm.toolStripButtonRefresh.PerformClick())); } break; case EmailType.CollectionEmail: { foreach (var email in messages) { if (_parentForm.EmailCollection.ContainsKey(email.MessageId)) { MessageBox.Show("Item already exists in collection", "Import failed"); } else { var selectedFolder = _parentForm.FolderList[destinationFolderName]; ++selectedFolder.ItemCount; var fromAddresses = new List <string>(); foreach (var address in email.From) { fromAddresses.Add(((MailboxAddress)address).Address); } var toAddresses = new List <string>(); foreach (var address in email.To) { toAddresses.Add(((MailboxAddress)address).Address); } var collectionEmail = new Models.CollectionEmail { Id = email.MessageId, From = string.Join(";", fromAddresses), To = string.Join(";", toAddresses), Subject = email.Subject, TextBody = email.TextBody, HtmlBody = email.HtmlBody, Date = email.Date.UtcDateTime, CustomFolderName = destinationFolderName, }; _parentForm.EmailCollection.TryAdd(collectionEmail.Id, collectionEmail); } } } break; } mailReceiver.Disconnect(); }
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)); } }); }