示例#1
0
        public static void AuthenticateSmtpGoogleOAuth2(this SmtpClient imap, MailBox account, ILogger log = null)
        {
            if (log == null)
            {
                log = new NullLogger();
            }

            var auth          = new GoogleOAuth2Authorization(log);
            var grantedAccess = auth.RequestAccessToken(account.RefreshToken);

            if (grantedAccess == null)
            {
                throw new ProtocolException("Access denied");
            }

            log.Info("Smtp SSL connecting to {0}", account.EMail);
            imap.ConnectSsl(account.SmtpServer, account.SmtpPort);

            log.Info("Smtp connecting OK {0}", account.EMail);

            imap.SendEhloHelo();

            log.Info("Smtp logging to {0} via OAuth 2.0", account.EMail);

            imap.Authenticate(account.SmtpAccount, grantedAccess.AccessToken, SaslMechanism.OAuth2);

            log.Info("Smtp logged to {0} via OAuth 2.0", account.EMail);
        }
        private string GetOauthAccessToken(string refreshToken)
        {
            var auth          = new GoogleOAuth2Authorization(Log);
            var grantedAccess = auth.RequestAccessToken(refreshToken);

            if (grantedAccess == null)
            {
                throw new AuthenticationException("XOAUTH2: Access denied.");
            }

            return(grantedAccess.AccessToken);
        }
示例#3
0
        public static void TestGoogleSmtpLoginViaOAuth2(string account, string refresh_token)
        {
            var authorizatior = new GoogleOAuth2Authorization();

            var granted_access = authorizatior.RequestAccessToken(refresh_token);

            if (granted_access == null)
            {
                return;
            }
            var smtp = MailClientBuilder.Smtp();

            smtp.ConnectSsl("smtp.googlemail.com", 465);
            smtp.Authenticate(account, granted_access.AccessToken, SaslMechanism.OAuth2);
            //Do some work...
            smtp.Disconnect();
        }
示例#4
0
        public static void TestGoogleImapLoginViaOAuth2(string account, string refresh_token)
        {
            var authorizatior = new GoogleOAuth2Authorization();

            var granted_access = authorizatior.RequestAccessToken(refresh_token);

            if (granted_access == null)
            {
                return;
            }
            var imap = MailClientBuilder.Imap();

            imap.ConnectSsl("imap.googlemail.com", 993);
            imap.LoginOAuth2(account, granted_access.AccessToken);
            //Do some work...
            imap.Disconnect();
        }
示例#5
0
        private void AuthenticateImapGoogleOAuth2(Imap4Client imap)
        {
            var auth           = new GoogleOAuth2Authorization();
            var granted_access = auth.RequestAccessToken(Account.RefreshToken);

            if (granted_access == null)
            {
                return;
            }
            _log.Info("IMAP SSL connecting to {0}", Account.EMail);
            imap.ConnectSsl(Account.Server, Account.Port);

            _log.Info("IMAP connecting OK {0}", Account.EMail);

            _log.Info("IMAP logging to {0} via OAuth 2.0", Account.EMail);
            imap.LoginOAuth2(Account.Account, granted_access.AccessToken);
            _log.Info("IMAP logged to {0} via OAuth 2.0", Account.EMail);
        }
        private void AuthenticateImapGoogleOAuth2(Imap4Client imap)
        {
            var auth           = new GoogleOAuth2Authorization(_log);
            var granted_access = auth.RequestAccessToken(Account.RefreshToken);

            if (granted_access == null)
            {
                throw new DotNetOpenAuth.Messaging.ProtocolException("Access denied");
            }
            _log.Info("IMAP SSL connecting to {0}", Account.EMail);
            imap.ConnectSsl(Account.Server, Account.Port);

            _log.Info("IMAP connecting OK {0}", Account.EMail);

            _log.Info("IMAP logging to {0} via OAuth 2.0", Account.EMail);
            imap.LoginOAuth2(Account.Account, granted_access.AccessToken);
            _log.Info("IMAP logged to {0} via OAuth 2.0", Account.EMail);
        }
        private string GetAccessToken(MailBox mbox)
        {
            var service_type = (AuthorizationServiceType)mbox.ServiceType;

            switch (service_type)
            {
            case AuthorizationServiceType.Google:
                var granted_access = new GoogleOAuth2Authorization(log)
                                     .RequestAccessToken(mbox.RefreshToken);

                if (granted_access != null)
                {
                    return(granted_access.AccessToken);
                }
                break;
            }

            return("");
        }
示例#8
0
        public static void Send(this SmtpClient smptClient, MailBox maibox, Message mimeMessage, ILogger log)
        {
            if (maibox.RefreshToken != null)
            {
                var grantedAccess = new GoogleOAuth2Authorization(log)
                                    .RequestAccessToken(maibox.RefreshToken);

                var accessToken = grantedAccess != null ? grantedAccess.AccessToken : "";


                smptClient.SendSsl(mimeMessage, maibox.SmtpServer, maibox.SmtpPort,
                                   maibox.SmtpAccount, accessToken,
                                   SaslMechanism.OAuth2);
            }
            else if (maibox.OutcomingEncryptionType == EncryptionType.None)
            {
                if (maibox.AuthenticationTypeSmtp == SaslMechanism.None)
                {
                    smptClient.Send(mimeMessage, maibox.SmtpServer, maibox.SmtpPort);
                }
                else
                {
                    smptClient.Send(mimeMessage, maibox.SmtpServer, maibox.SmtpPort,
                                    maibox.SmtpAccount, maibox.SmtpPassword,
                                    maibox.AuthenticationTypeSmtp);
                }
            }
            else
            {
                if (maibox.AuthenticationTypeSmtp == SaslMechanism.None)
                {
                    smptClient.SendSsl(mimeMessage, maibox.SmtpServer, maibox.SmtpPort,
                                       maibox.OutcomingEncryptionType);
                }
                else
                {
                    smptClient.SendSsl(mimeMessage, maibox.SmtpServer, maibox.SmtpPort,
                                       maibox.SmtpAccount, maibox.SmtpPassword,
                                       maibox.AuthenticationTypeSmtp,
                                       maibox.OutcomingEncryptionType);
                }
            }
        }
        public static void AuthenticateImapGoogleOAuth2(this Imap4Client imap, MailBox account, ILogger log = null)
        {
            if (log == null)
            {
                log = new NullLogger();
            }

            var auth           = new GoogleOAuth2Authorization(log);
            var granted_access = auth.RequestAccessToken(account.RefreshToken);

            if (granted_access == null)
            {
                throw new DotNetOpenAuth.Messaging.ProtocolException("Access denied");
            }
            log.Info("IMAP SSL connecting to {0}", account.EMail);
            imap.ConnectSsl(account.Server, account.Port);

            log.Info("IMAP connecting OK {0}", account.EMail);

            log.Info("IMAP logging to {0} via OAuth 2.0", account.EMail);
            imap.LoginOAuth2(account.Account, granted_access.AccessToken);
            log.Info("IMAP logged to {0} via OAuth 2.0", account.EMail);
        }