public void AuthHandler(ImapClient client, NetworkCredential credential, ImapAccountInfo account) { if (null != oAuthProvider) { oAuthProvider.OAuthHandler(client, credential, account); } }
// needed as a callback if IMAP uses OAuth (not necessary if username/pwd will be provided) public void OAuthHandler(ImapClient client, NetworkCredential credential, ImapAccountInfo 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(XOAuthRequestTokenEndPoint). WithParameter("scope", XOAuthRequestScope). WithParameter(OAuthParameters.OAuthCallback, "oob"). WithParameter("xoauth_displayname", account.XOAuthAppDisplayName). WithSignatureMethod(OAuthSignatureMethods.HmacSha1). Sign(). RequestToken(); var authUrl = new OAuthRequest(). WithEndpoint(XOAuthAuthTokenEndPoint). WithToken(token). GetAuthorizationUri(); // TODO: change this to (a) redirect the user to the authUri, // (b) get back the verification code in a callback from the authUri // // Probably means we have to break this method into at least two different methods // with some sort of flow between them. // account.XOAuthSecret = GetXOAuthSecret(authUrl); var accessToken = new OAuthRequest(). WithAnonymousConsumer(). WithEndpoint(XOAuthAccessTokenEndPoint). WithParameter(OAuthParameters.OAuthVerifier, account.XOAuthSecret). WithSignatureMethod(OAuthSignatureMethods.HmacSha1). WithToken(token). Sign(). RequestToken(); var authUri = String.Format(XOAuthUrlFormat, email); account.XOAuthKey = new OAuthRequest(). WithAnonymousConsumer(). WithEndpoint(authUri). WithSignatureMethod(OAuthSignatureMethods.HmacSha1). WithToken(accessToken). Sign(). CreateXOAuthKey(); } client.AuthenticateXOAuth(account.XOAuthKey); }
public ImapClient InitClient(ImapAccountInfo account) { Debug.Assert(null != account); ImapClient imapClient = new Crystalbyte.Equinox.Imap.ImapClient(); imapClient.Security = SecurityPolicies.Explicit; imapClient.Connect(this.Host, this.Port); if ((null != account.UserName) && (null != account.UserPwd)) { // doesn't need OAuth --- try regular LOGIN imapClient.Authenticate(account.UserName, account.UserPwd, SaslMechanics.Login); } else { imapClient.ManualSaslAuthenticationRequired += (sender, e) => this.AuthHandler(e.Client, e.UserCredentials, account); imapClient.Authenticate(account.UserName, account.UserPwd); } return imapClient; }
private void StartGmailClient() { string userName = ConfigurationManager.AppSettings["GMAIL_USERNAME"]; string userPwd = ConfigurationManager.AppSettings["GMAIL_USERPWD"]; if ((null == userName) || (null == userPwd)) { GmailClient = null; return; } ImapAccountInfo imapAccount = new ImapAccountInfo(); imapAccount.UserName = userName; imapAccount.UserPwd = userPwd; imapAccount.Provider = ImapProvider.GMail; SmtpAccountInfo smtpAccount = new SmtpAccountInfo(); smtpAccount.UserName = userName; smtpAccount.UserPwd = userPwd; smtpAccount.Provider = SmtpProvider.GMail; GmailClient = new MailClient(imapAccount, smtpAccount); }
/// This method creates a mail client that connects to a well-defined IMAP server and SMTP server. public MailClient(ImapAccountInfo ImapAccount, SmtpAccountInfo SmtpAccount) { imapClient = ImapAccount.Provider.InitClient(ImapAccount); smtpClient = SmtpAccount.Provider.InitClient(SmtpAccount); }