# Help with (imap-connect ...) using oauth2 (and maybe tls)?

**URL:** <https://racket.discourse.group/t/help-with-imap-connect-using-oauth2-and-maybe-tls/4033>\
**Category:** General\
**Created:** [November 29, 2025, 11:59am UTC](https://racket.discourse.group/t/help-with-imap-connect-using-oauth2-and-maybe-tls/4033 "2025-11-29T11:59:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbhanson](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tbhanson/32/906_2.png) [@tbhanson](https://racket.discourse.group/u/tbhanson)\
**Post date:** [November 29, 2025, 11:59am UTC](https://racket.discourse.group/t/help-with-imap-connect-using-oauth2-and-maybe-tls/4033/1 "2025-11-29T11:59:17Z")

</div>

hi, I've successfully connected to imap servers without using oauth2 but am now trying to do so with oauth2 (and maybe tls).

My attempt seems to be timing out (but it also seems to be prompting me for input, as though I had called (read-line ...) or something, and I'm not sure why.

I'm not sure I'm using the function as intended; here's some code I'm trying:

```scheme
(define (connect-to-gmail username access-token)
  (printf "Connecting to Gmail IMAP server...\n")
  (let ([imap-connection 
         (imap-connect 
          "imap.gmail.com"
          username
          access-token
          "INBOX"
          ;;#:tls? #t
          #:try-tls? #t
          #:xoauth2? #t
          
          )])
    (printf "IMAP connection established.\n")
    imap-connection))

```

The top printf happens, the bottom doesn't and after a minute or two I get:

```scheme
tcp-connect: connection failed
  hostname: imap.gmail.com
  port number: 143
  system error: Operation timed out; errno=60

```

I would appreciate pointers from someone who has an idea what I'm doing wrong.

Cheers, Tim

---

<div class="post-metadata">

**Author:** ![tbhanson](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tbhanson/32/906_2.png) [@tbhanson](https://racket.discourse.group/u/tbhanson)\
**Post date:** [November 29, 2025, 12:52pm UTC](https://racket.discourse.group/t/help-with-imap-connect-using-oauth2-and-maybe-tls/4033/2 "2025-11-29T12:52:42Z")

</div>

I succeeded with imap-connect\* (as I had previously without oauth2). I guess I should have tried that before posting my question, but I can include the toy code that works in case this might help someone else in a similar situation.

```scheme
(define (connect-to-gmail username access-token)
  (let ([port-no 993]
        [hostname "imap.gmail.com"])
    (printf "Connecting to Gmail IMAP server...\n")
    (let-values
        ([(imap-connection messages# nu)
          (let-values ([(in out) (ssl-connect hostname port-no)])
            (imap-connect*
             in
             out
             username
             access-token
             "INBOX"
             ;;#:tls? #t
             #:try-tls? #t
             #:xoauth2? #t
             ))])

      (printf "Connecting to Gmail IMAP server...\n")
      (printf "IMAP connection established.\n")
      imap-connection)))

```
