public static void Read() { try { int log = $"[...] Loading file {FileCore.PathMailData.GetNameFromPath()}".DActionLog(); MailSecureList.Clear(); if (!File.Exists(FileCore.PathMailData)) { FileCore.PathMailData.ClearFile(); } var json = FileCore.PathMailData.ReadFromFile(); if (string.IsNullOrWhiteSpace(json) || json == "\r\n") { log.DActionLog(); return; } MailSecureList = JsonConvert.DeserializeObject <List <MailSecureItem> >(json); log.DActionLog(); } catch (Exception ex) { ex.Error(); } }
public static void Save() { try { int log = $"[...] Saving file {FileCore.PathMailData.GetNameFromPath()}".DActionLog(); if (MailSecureList.Any()) { var json = JsonConvert.SerializeObject(MailSecureList, Formatting.Indented); FileCore.PathMailData.SaveToFile(json); } log.DActionLog(); } catch (Exception ex) { ex.Error(); } }
public static bool LoginMail(string host, int port, string username, string password, bool ssl, RMessagesData.RCmdsListCollect.RCommandsItem rCommandItem, string accessToken) { try { LogCore.Log("Login mail...", null, EnumData.LogTypeCommand.System, EnumData.LogSourceCommand.Mail); ImapClient imapClient = null; try { imapClient = new ImapClient(host, port, username, password, AuthMethod.Login, ssl); } catch (InvalidCredentialsException ice) { LogCore.Log(ice.Message, null, EnumData.LogTypeCommand.Attention, EnumData.LogSourceCommand.Mail); var answer = ice.Message; var loginLink = Regex.Match(answer, @"http[^ \]]+").Value; if (!string.IsNullOrWhiteSpace(loginLink)) { var shortLink = SiteUtils.GetShortLink(accessToken, loginLink); answer = answer.Replace(loginLink, shortLink); if (answer.Contains("WEBALERT")) { SiteUtils.SendMessage(accessToken, rCommandItem, $"Подтвердите вход в почту или разрешить небезопасным приложениям доступ к аккаунту. Ответ сервера :\n{answer}"); } else { if (answer.Contains("ALERT")) { SiteUtils.SendMessage(accessToken, rCommandItem, $"Ошибка вода в почту. Ответ сервера :\n{answer}"); } } } } if (imapClient != null) { if (imapClient.Authed) { var mailSecureItem = new MailSecureItem(host, port, ssl, username, password, rCommandItem); MailSecureList.Add(mailSecureItem); Save(); var hash = mailSecureItem.GetHashCode(); ImapClients.Add(hash, imapClient); AutoResetEvents.Add(hash, new AutoResetEvent(false)); Action action = () => Initialize(mailSecureItem); var thread = new Thread(action.Invoke) { IsBackground = true }; Threads.Add(hash, thread); thread.Start(); LogCore.Log("Login complete!", null, EnumData.LogTypeCommand.System, EnumData.LogSourceCommand.Mail); } else { LogCore.Log("Login NOT complete!", null, EnumData.LogTypeCommand.Attention, EnumData.LogSourceCommand.Mail); } return(imapClient.Authed); } return(false); } catch (Exception ex) { ex.Error(); return(false); } }