Examine the error you are getting carefully. Without the error message it's hard to tell what's wrong.
Make sure your application has enough permissions to open a socket connection to Gmail server. Connection is made on port 993 (default IMAP protocol).
In many cases shared hosting environments don't allow opening connections - consult your administrator.
Side note: don't use Where method more than once. If you want to join expressions use Expression.And:
List<long> uids = imap.Search().Where(
I'm not sure if GmailRawSearch can be used more then once - this is GMail specific command, so I'm not sure if standard IMAP search operators apply.