Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET IMAP

 AfterLogic Forum : MailBee.NET IMAP
Subject Topic: Gmail IMAP Connection failing Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
i3000
Newbie
Newbie


Joined: 21 October 2010
Location: Australia
Online Status: Offline
Posts: 9
Posted: 03 November 2013 at 3:25am | IP Logged Quote i3000

Hi,

I have an application that is used by various users for accessing Gmail by IMAP. All seems to go well except for seemingly random occurrences where we have timeouts or delays authenticating.

The Most common error message that we are seeing is:

IOException occurred. InnerException message follows: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.     

We are authenticating with Gmail using 2LO. Code Snippet below:

_________________________________________________________

SaslLoginMethod slm = new SaslLoginMethod();
               slm.key = tl.iLinkConsumerKey;
               slm.secret = tl.iLinkConsumerSecret;
               slm.email = tl.loadedUser.emailAccount;
              
               imap.Connect("imap.gmail.com", 993);
               imap.Login(null, null, AuthenticationMethods.SaslUserDefined, AuthenticationOptions.TryUnsupportedMethods, slm);

__________________________________________________________________


I see in the Mailbee sample code that their is a more up to date method (since our initial implementation) for authenticating using Mailbee OAuth.

__________________________________________________________________

OAuth myOAuth = new OAuth(consumerKey, consumerSecret);

        StringDictionary parameters = new StringDictionary();
        parameters.Add("xoauth_requestor_id", userEmail);

        // Get XOAuth key for IMAP.
        string imapXOAuthKey = myOAuth.GetXOAuthKey(
            string.Format("https://mail.google.com/mail/b/{0}/imap/", userEmail));

        // Last: Login to Gmail IMAP server using XOAuth.
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com", 993);
        imp.Login(null, imapXOAuthKey, AuthenticationMethods.SaslOAuth, AuthenticationOptions.None, null);

_______________________________________________________________________

Should we look to update our code as above or does this have no bearing on the issue...

Any thoughts on how to deal with this issue is appreciated.

Paul C
Back to Top View i3000's Profile Search for other posts by i3000
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 03 November 2013 at 5:10am | IP Logged Quote Alex

OAuth class supports OAuth v1.0 only (which have recently been deprecated by Google). I think your custom implementation uses OAuth v2.0.

We'll soon be releasing a new version of MailBee.NET Objects which supports OAuth v2.0 through dotnetopenauth.net library. But I guess you're using the same library. So, I don't think using MailBee's own implementation will make much difference. Gmail is known for its not stable behavior when it comes to using IMAP and (especially) POP3. Intermittent errors are normal with them. Also, I think they implement some kind of throttling. If you make many connections to them, you occasionally start getting delays and denials of service. For instance, their OAuth v2.0 guide literally says: "To prevent abuse, Google places limits on API requests. Using a valid OAuth token or API key allows you to exceed anonymous limits by connecting requests back to your project."

It's not clear how many requests you can make with a valid key, but I guess the idea remains the same - if you got an error, try again.

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 
i3000
Newbie
Newbie


Joined: 21 October 2010
Location: Australia
Online Status: Offline
Posts: 9
Posted: 03 November 2013 at 3:51pm | IP Logged Quote i3000

Hi Alex,

Thanks for your detailed response - that is concerning the instability of Gmail IMAP.

We are using a Google Marketplace Application (for the last 2 years) which does not currently support OAuth2 - although I believe this is due in the near future.

The implementation that we have used involves a custom SASL method (code after this).

Should we be able to replace this with the example at:

http://www.afterlogic.com/mailbee-net/docs/MailBee.OAuth.html

Thanks for your support.

_________________________________________________________________________

public class SaslLoginMethod : SaslMethod
{
    public string email { get; set; }
    public string key { get; set; }
    public string secret { get; set; }
    
    public override string GetSaslID()
    {
        return "XOAUTH";
    }

    public override void CreateNextClientAnswer()
    {
        switch (Stage)
        {
            case 0:
               ClientAnswer = ClientAnswerEncoding.GetBytes(getXoauthString());
               Stage++;
               break;
        }
    }

    public override bool RequiresCredentials()
    {
        return false;
    }

    protected string UnixTimestamp()
    {
        var utcEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var utcNow = DateTime.UtcNow;
        var span = utcNow - utcEpoch;
        return ((int)span.TotalSeconds).ToString();
    }

    protected string getXoauthString()
    {
        string consumerKey = key;
        string consumerSecret = secret;       
        Uri uri = new Uri("https://mail.google.com/mail/b/" + email + "/imap/?xoauth_requestor_id=" + HttpUtility.UrlEncode(email));
        OAuthBase oAuth = new OAuthBase();
        string nonce = oAuth.GenerateNonce();
        string timeStamp = oAuth.GenerateTimeStamp();
        string normalisedUrl;
        string normalisedRequestParameters;
        string sig = oAuth.GenerateSignature(uri, key, secret, string.Empty, string.Empty, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1, out normalisedUrl, out normalisedRequestParameters);
        sig = System.Web.HttpUtility.UrlEncode(sig);
        StringBuilder sb = new StringBuilder("GET " + uri.ToString());
        sb.AppendFormat(" oauth_consumer_key=\"{0}\"", key);
        sb.AppendFormat(",oauth_nonce=\"{0}\"", nonce);
        sb.AppendFormat(",oauth_signature=\"{0}\"", sig);
        sb.AppendFormat(",oauth_signature_method=\"{0}\"", "HMAC-SHA1");
        sb.AppendFormat(",oauth_timestamp=\"{0}\"", timeStamp);
        sb.AppendFormat(",oauth_version=\"{0}\"", "1.0");
        return sb.ToString();
    }

    public override bool IsSecure()
    {
        return true;
    }
}

________________________________________________________________________
Back to Top View i3000's Profile Search for other posts by i3000
 
Alex
AfterLogic Support
AfterLogic Support
Avatar

Joined: 19 November 2003
Online Status: Offline
Posts: 2206
Posted: 04 November 2013 at 4:12am | IP Logged Quote Alex

I'd recommend you try our new OAuth2 class when it gets released (in a week or so).

Regards,
Alex
Back to Top View Alex's Profile Search for other posts by Alex
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide