Rust:使用async-imap的 search命令来获取邮件

编程语言 2026-07-09

全部

当我尝试使用async-imap通过服务器查询邮件,代码如下

let messages_set = imap_session.search("ALL").await?;

它返回了最近的34封邮件(UID从 1到 34),但我实际有超过200封邮件。

我希望每次查询最多获取30封邮件,或者一次最多获取10,000,000封邮件。

我按下面的测试进行了尝试

query string                     emails
        ALL                        the last 34
        LARGER 1000                34
        UID 10:*                   34
        SEEN                       34
        SINCE 06-May-2026          6


        UID LARGER 10              0
        SUBJECT "xxxx"             0
        FROM "[email protected]"         0

测试代码

    let imap_addr = (imap_server, 993);
    let tcp_stream = TcpStream::connect(imap_addr).await?;
    let tls = async_native_tls::TlsConnector::new();
    let tls_stream = tls.connect(imap_server, tcp_stream).await?;

    let client = async_imap::Client::new(tls_stream);
    println!("-- connected to {}:{}", imap_addr.0, imap_addr.1);

    // the client we have here is unauthenticated.
    // to do anything useful with the e-mails, we need to log in
    let mut imap_session = client.login(login, password).await.map_err(|e| e.0)?;

    println!("-- logged in a {}", login);

    // we want to fetch the first email in the INBOX mailbox
    imap_session.select("INBOX").await?;
    println!("-- INBOX selected");


    let query = "SINCE 06-May-2026".to_owned();

    let messages_set = imap_session.search(query).await?;
    let messages: Vec<u32> = messages_set.into_iter().collect();


    println!("emails count: {}", messages.len());


    //be nice to the server and log out
    imap_session.logout().await?;

解决方案

我已经找到问题所在。

应该设置服务器配置

POP3/SMTP/IMAP

接收全部

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章