jonhoo / rust-imap

IMAP client library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for STARTTLS

rvuong opened this issue · comments

Hello,

I can't figure out how to connect with STARTTLS enabled.
I'm using the same config & credentials as with a mail client (thunderbird), which also works fine with openssl in CLI :

openssl s_client -showcerts -connect {host}:143 -starttls imap

I tried using the port 993. It then seems like a success, BUT fails afterwards on searching/fetching emails.

Am I acting stupid or something ?

Input:

let tls = native_tls::TlsConnector::builder().build().unwrap();
let client = imap::connect(
   (domain, port),
   domain,
   &tls,
);
eprintln!("{:#?}", client);

Output:

Err(
    TlsHandshake(
        Failure(
            Ssl(
                Error {
                    code: ErrorCode(
                        1,
                    ),
                    cause: Some(
                        Ssl(
                            ErrorStack(
                                [
                                    Error {
                                        code: 336130315,
                                        library: "SSL routines",
                                        function: "ssl3_get_record",
                                        reason: "wrong version number",
                                        file: "../ssl/record/ssl3_record.c",
                                        line: 332,
                                    },
                                ],
                            ),
                        ),
                    ),
                },
                X509VerifyResult {
                    code: 0,
                    error: "ok",
                },
            ),
        ),
    ),
)

When trying port 993 (not sure if it's worth trying it), connect, login, and select seem ok.
Then:

let messages = imap_session.search("UNSEEN");
// the same happens with
// let messages = imap_session.fetch("1", "RFC822")?;

Output:

Error: Parse(Unexpected("MailboxData(Flags([\"\\\\Answered\", \"\\\\Deleted\", \"\\\\Draft\", \"\\\\Flagged\", \"\\\\Seen\", \"$Forwarded\", \"$MDNSent\", \"Forwarded\", \"$Junk\", \"$NotJunk\", \"Junk\", \"JunkRecorded\", \"NonJunk\", \"NotJunk\"]))"))

If you want to use STARTTLS, you should use connect_starttls, not connect. connect attempts to establish a TLS connection immediately, which (as you observed) only works if you connect to the TLS endpoint on port 993.

As for the command that fails, that's pretty interesting. It seems like we get a FLAGS response sent unilaterally to us by the server. I'll dig into that a little!

@rvuong Can you try the latest release and see if that still errors for you?

If you want to use STARTTLS, you should use connect_starttls, not connect. connect attempts to establish a TLS connection immediately, which (as you observed) only works if you connect to the TLS endpoint on port 993.

connect_starttls works like a charm! I can't believe I hadn't found it before by myself. Thanks for pointing me on it.

@rvuong Can you try the latest release and see if that still errors for you?

It's a win! Thank you so much. Any way I can help this project in order to thank you?

Input:

let messages = imap_session.search("UNSEEN")?;
eprintln!("{:#?}", messages);

Output:

{
    70,
    71,
    62,
}

Awesome, so happy that it works! Documentation improvements are always warmly welcome. For example, if you find a way to make connect_starttls more discoverable for others in the future, that'd be great!