Example #1
0
        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);
            }
        }