public MailReceiver GetMailReceiverInstance() { MailReceiver mailReceiver = new MailReceiver(mailServer, port, ssl, Login, Password); mailReceiver.Connect(); return(mailReceiver); }
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)); } }); }