Пример #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);
        }