Пример #1
0
        public Imap(Account acct)
        {
            Client = AccountController.CreateClientByAccount(acct);

            App.Log.Info("User authentication finished (IMAP)");

            /*var obj = new ImapIdle();
            ImapController.IdleThread = new Thread(obj.ThreadStart);
            ImapController.IdleThread.Name = "IMAP IDLE Thread";
            ImapController.IdleThread.Start();*/
        }
        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            Account = new Account
                          {
                              Name = txtAccountName.Text,
                              Credential =
                                  new NetworkCredential {UserName = txtUsername.Text, Password = txtPassword.Password},
                              Host = txtHost.Text,
                              Security = GetSecurity()
                          };

            DialogResult = true;
            Close();
        }
        public static ImapClient CreateClientByAccount(Account account)
        {
            try
            {
                var client = new ImapClient { Security = account.Security };
                //client.ManualSaslAuthenticationRequired += (sender, e) => AuthenticateManually(e, account);
                var port = client.Security == SecurityPolicies.Explicit ? ImapPorts.Ssl : ImapPorts.Default;
                client.Connect(account.Host, port);
                //Thread.Sleep(100);
                client.Authenticate(account.Credential, SaslMechanics.Login);
                //Thread.Sleep(500);

                return client;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        public void Authenticate(ImapClient client, NetworkCredential credential, Account account)
        {
            if (!client.ServerCapability.Items.Contains("AUTH=XOAUTH"))
            {
                return;
            }

            if (string.IsNullOrEmpty(account.XOAuthKey))
            {
                // in this case the username is the email address;
                var email = credential.UserName;

                var token = new OAuthRequest().WithAnonymousConsumer().WithEndpoint("https://www.google.com/accounts/OAuthGetRequestToken").WithParameter("scope", "https://mail.google.com/") // gmail specific
                    .WithParameter(OAuthParameters.OAuthCallback, "oob").WithParameter("xoauth_displayname", "Kolibrie Mail") // xoauth protocol specific
                    .WithSignatureMethod(OAuthSignatureMethods.HmacSha1).Sign().RequestToken();

                var authUrl = new OAuthRequest().WithEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken").WithToken(token).GetAuthorizationUri();

                Process.Start(authUrl.AbsoluteUri);

                string verificationCode;
                using (var form = new OAuthVerificationForm())
                {
                    form.ShowDialog();
                    verificationCode = form.VerificationCode;
                    if (form.DialogResult.HasValue && !form.DialogResult.Value)
                    {
                        return;
                    }
                }

                var accessToken = new OAuthRequest().WithAnonymousConsumer().WithEndpoint("https://www.google.com/accounts/OAuthGetAccessToken").WithParameter(OAuthParameters.OAuthVerifier, verificationCode).WithSignatureMethod(OAuthSignatureMethods.HmacSha1).WithToken(token).Sign().RequestToken();

                var xOUrl = string.Format("https://mail.google.com/mail/b/{0}/imap/", email);
                account.XOAuthKey = new OAuthRequest().WithAnonymousConsumer().WithEndpoint(xOUrl).WithSignatureMethod(OAuthSignatureMethods.HmacSha1).WithToken(accessToken).Sign().CreateXOAuthKey();

                Console.WriteLine("XOAth key: " + account.XOAuthKey);
            }

            client.AuthenticateXOAuth(account.XOAuthKey);
        }