public void MYG_IMAP_READ_AND_MARK(Imap4Client imap) { if (!bIMAP_ERROR) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Checking; try { imap.Command("capability"); Mailbox inbox = imap.SelectMailbox("inbox"); int[] ids = inbox.Search("UNSEEN"); if (ids.Length > 0) { iCountIDS = 0; for (var i = 0; i < ids.Length; i++) { if (!MSGS_ID.Contains(ids[i].ToString())) { iCountIDS++; MSGS_ID.Add(ids[i].ToString()); } } if (iCountIDS > 0) { iLastCOUNT = iCountIDS; MSGS_DETAILS = ""; nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Check; bNewMsgICON = true; NEW_MSGS = true; //ActiveUp.Net.Mail.Message msg_first = inbox.Fetch.MessageObject(ids[0]); //XElement xmail = new XElement("gmail", // new XAttribute("count", ids.Length.ToString()), // new XAttribute("modified", msg_first.Date.ToString()) //); //string name = "", address = "", from = ""; //Regex reg_name = new Regex("\"[^\"]+"); //Regex reg_address = new Regex("<[^>]+"); ActiveUp.Net.Mail.Message msg = null; for (var i = 0; i < ids.Length; i++) { msg = inbox.Fetch.MessageObject(ids[i]); //from = msg.HeaderFields["from"]; //name = reg_name.Match(from).Value.Replace("\"", ""); //address = reg_address.Match(from).Value.Replace("<", ""); rtLog.Text += DateTime.Now.ToString() + "\n"; rtLog.Text += "DATA: " + msg.Date + "\n"; rtLog.Text += "MITTENTE: " + msg.From + "\n"; rtLog.Text += "OGGETTO: " + msg.Subject + "\n"; rtLog.Text += "----------------------------\n"; MSGS_DETAILS += "DATA: " + msg.Date + "\n"; MSGS_DETAILS += "MITTENTE: " + msg.From + "\n"; MSGS_DETAILS += "OGGETTO: " + msg.Subject + "\n"; MSGS_DETAILS += "---------------------------\n"; //xmail.Add(new XElement("entry", // new XAttribute("id", msg.MessageId), // new XAttribute("modified", msg.Date.ToString()), // new XAttribute("name", name), // new XAttribute("address", address), // new XElement("subject", msg.Subject), // new XElement("body-text", msg.BodyText.TextStripped), // new XElement("body-html", msg.BodyHtml.Text) //)); //mark as unread var flags = new FlagCollection(); flags.Add("Seen"); inbox.RemoveFlags(ids[i], flags); } //File.WriteAllText("gmail.xml", xmail.ToString()); } else { NEW_MSGS = false; nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Gray; } } else { NEW_MSGS = false; nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Gray; } } catch (Exception err) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Error; rtError.Text += DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP:\n" + err.Message + "\n------------------\n"; ; MGN_ERROR = DateTime.Now.ToString() + "-> ERRORE ELABORAZIONE IMAP: " + err.Message; if (MGN_ERROR.ToLower().Contains("connessione interrotta")) { CONNECT_IMAP(); } if (MyGNotifier.Properties.Settings.Default.MAIN_AUTO_NOTIFY_ERROR) { NOTIFY_ERROR(MGN_ERROR); } } } else { rtLog.Text += "\n---------------\n" + DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP!\n---------------\n"; } if (bNewMsgICON) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Check; } }
public void MYG_IMAP_READ_AND_MARK_ALWAYS_CONNECT() { int iCountNew = 0; string sNewMsgs = ""; DateTime dtInizio = DateTime.Now; nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Checking; nIcon.Text = "STO CONTROLLANDO LA CASELLA DI POSTA..."; Imap4Client imap = new Imap4Client(); btnRECONNECT.Visible = false; # region CONNECT try { imap = new Imap4Client(); imap.ConnectSsl(SERVER, PORTA); imap.Login(USER, PWD); bIMAP_ERROR = false; } catch (Exception errCONN) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Error; nIcon.Text = "ERRORE CONNESSIONE IMAP:\n" + errCONN.Message; bIMAP_ERROR = true; rtError.Text += DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP:\n" + errCONN.Message + "\n------------------\n"; MGN_ERROR = DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP: " + errCONN.Message; if (MyGNotifier.Properties.Settings.Default.MAIN_AUTO_NOTIFY_ERROR) { if (MyGNotifier.Properties.Settings.Default.MAIN_RAD_NOTIFIER) { RAD_NOTIFY_ERROR(MGN_ERROR); } else { NOTIFY_ERROR(MGN_ERROR); } } } # endregion # region ELABORA MESSAGGI if (!bIMAP_ERROR) { try { imap.Command("capability"); Mailbox inbox = imap.SelectMailbox("inbox"); int[] ids = inbox.Search("UNSEEN"); if (ids.Length > 0) { ActiveUp.Net.Mail.Message msg = null; for (var i = 0; i < ids.Length; i++) { msg = inbox.Fetch.MessageObject(ids[i]); string UNIQUE_ID = msg.Date.ToString() + msg.From.ToString(); if (!MSGS_ID.Contains(UNIQUE_ID)) { iCountNew++; MSGS_ID.Add(UNIQUE_ID); rtLog.Text += DateTime.Now.ToString() + "\n"; rtLog.Text += "DATA: " + msg.Date + "\n"; rtLog.Text += "MITTENTE: " + msg.From + "\n"; rtLog.Text += "OGGETTO: " + msg.Subject + "\n"; rtLog.Text += "----------------------------\n"; sNewMsgs += "DATA: " + msg.Date + "\n"; sNewMsgs += "MITTENTE: " + msg.From + "\n"; sNewMsgs += "OGGETTO: " + msg.Subject + "\n"; sNewMsgs += "---------------------------\n"; } //mark as unread var flags = new FlagCollection(); flags.Add("Seen"); inbox.RemoveFlags(ids[i], flags); } if (iCountNew > 0) { iLastCOUNT = iCountNew; MSGS_DETAILS = sNewMsgs; nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Check; nIcon.Text = iCountNew > 1 ? "SONO PRESENTI " + iCountNew + " NUOVI MESSAGGI" : "E' PRESENTE 1 NUOVO MESSAGGIO!"; bNewMsgICON = true; NEW_MSGS = true; } else { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Gray; NEW_MSGS = false; } } else { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Gray; NEW_MSGS = false; } } catch (Exception err) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Error; rtError.Text += DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP:\n" + err.Message + "\n------------------\n"; ; MGN_ERROR = DateTime.Now.ToString() + "-> ERRORE ELABORAZIONE IMAP: " + err.Message; if (MyGNotifier.Properties.Settings.Default.MAIN_AUTO_NOTIFY_ERROR) { if (MyGNotifier.Properties.Settings.Default.MAIN_RAD_NOTIFIER) { RAD_NOTIFY_ERROR(MGN_ERROR); } else { NOTIFY_ERROR(MGN_ERROR); } } } } else { rtLog.Text += "\n---------------\n" + DateTime.Now.ToString() + "-> ERRORE CONNESSIONE IMAP!\n---------------\n"; } # endregion # region DISCONNECT try { imap.Disconnect(); } catch (Exception err) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Empty_Error; nIcon.Text = "ERRORE DISCONNESSIONE IMAP:\n" + err.Message; rtError.Text += DateTime.Now.ToString() + "-> ERRORE DISCONNESSIONE IMAP:\n" + err.Message + "\n------------------\n"; ; MGN_ERROR = DateTime.Now.ToString() + "-> ERRORE DISCONNESSIONE IMAP: " + err.Message; if (MyGNotifier.Properties.Settings.Default.MAIN_AUTO_NOTIFY_ERROR) { if (MyGNotifier.Properties.Settings.Default.MAIN_RAD_NOTIFIER) { RAD_NOTIFY_ERROR(MGN_ERROR); } else { NOTIFY_ERROR(MGN_ERROR); } } } # endregion if (bNewMsgICON) { nIcon.Icon = MyGNotifier.Properties.Resources.MyGNotify_Check; } else { if (!NEW_MSGS) { nIcon.Text = "NESSUN NUOVO MESSAGGIO!"; } } if (MyGNotifier.Properties.Settings.Default.MAIN_CHECK_MONITOR) { DateTime dtFine = DateTime.Now; TimeSpan ts = dtFine - dtInizio; rtLog.Text += DateTime.Now.ToString() + "-> FINE CONTROLLO CASELLA POSTA: DURATA CONTROLLO " + ts.Seconds.ToString() + " secondi.\n"; } }
// gets mailboxes, messages from wich we should get public static IEnumerable<ImapMailboxInfo> GetImapMailboxes(Imap4Client client, string server) { var mailboxes = new List<ImapMailboxInfo>(); var special_domain_folders = new Dictionary<string, MailQueueItemSettings.MailboxInfo>(); if (MailQueueItemSettings.SpecialDomainFolders.Keys.Contains(server)) special_domain_folders = MailQueueItemSettings.SpecialDomainFolders[server]; // get all mailboxes var response = client.Command("LIST \"\" \"*\""); var t = Regex.Split(response, "\r\n"); for (var i = 0; i < t.Length - 2; i++) { var m = SysfolderRegex.Match(t[i]); if (!m.Success) continue; var new_mailbox = new ImapMailboxInfo { folder_id = MailFolder.Ids.inbox, name = m.Groups[3].Value, tags = new string[]{} }; var separator = m.Groups[2].Value; if (new_mailbox.name.ToLower() != "inbox") { var utf8_name = FolderNameDecodeHelper.Replace(new_mailbox.name, DecodeUtf7); if (special_domain_folders.ContainsKey(utf8_name.ToLower())) { var info = special_domain_folders[utf8_name.ToLower()]; if (info.skip) continue; new_mailbox.folder_id = info.folder_id; } else { var look_for_parent = false; var flags_match = ImapFlagRegex.Matches(m.Groups[1].Value.ToLower()); if (flags_match.Count > 0) { var matches = new List<string>(); for (var j = 0; j < flags_match.Count; j++) { matches.Add(flags_match[j].Groups[1].Value); } if ( matches.Any( @group => MailQueueItemSettings.SkipImapFlags.Contains( @group.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()))) continue; var flag = MailQueueItemSettings.ImapFlags.FirstOrDefault(f => matches.Contains(f.Key)); if (null != flag.Key) { new_mailbox.folder_id = flag.Value; // special case for inbox - gmail l10n issue if (MailFolder.Ids.inbox == flag.Value && new_mailbox.name.ToLower() != "inbox") new_mailbox.name = "inbox"; } else { look_for_parent = true; } } else { look_for_parent = true; } if (look_for_parent) { // if mailbox is potentialy child - add tag. Tags looks like Tag1/Tag2/Tag3 const string tag_for_store_separator = "/"; var tag = utf8_name.Replace(separator, tag_for_store_separator); var parent_index = GetParentFolderIndex(mailboxes, new_mailbox, separator); if (parent_index >= 0) { var parent = mailboxes[parent_index]; new_mailbox.folder_id = parent.folder_id; // if system mailbox - removes first tag // if not system mailbox child - removes same count of tags as in parent if (!parent.tags.Any()) tag = tag.Substring(tag.IndexOf(tag_for_store_separator, StringComparison.Ordinal)); } new_mailbox.tags = new[] { tag }; } } } mailboxes.Add(new_mailbox); } return mailboxes; }