C - Libcurl - 如何搜索带有特殊字符的邮件(ä,ö,ü)

时间:2018-05-12 21:42:27

标签: c search special-characters imap libcurl

我使用libcurl在我的邮箱中搜索邮件。 但我不能用邮件搜索包含特定字符(ä,ö,ü)的邮件。

int main(void)
{
  CURL *curl;
  CURLcode res = CURLE_OK;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");

    curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993/INBOX");

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH SUBJECT Spülmaschine");

    res = curl_easy_perform(curl);

    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    curl_easy_cleanup(curl);
  }

  return (int)res;
}

无法找到邮件。

我搜索了一段时间但我找不到一些东西。

有人有任何想法吗?

编辑://以下任何一个试用都不会起作用:

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH CHARSET UTF-8 SUBJECT \x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65");


//try it with UTF-7
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH SUBJECT Sp+APw-lmaschine");


sprintf(strSearchString, "SEARCH CHARSET UTF-8 SUBJECT {%i}\r\n\x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65", strlen("\x53\x70\xc3\xbc\x6c\x6d\x61\x73\x63\x68\x69\x6e\x65"));
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, strSearchString);

2 个答案:

答案 0 :(得分:0)

虽然技术上反对包含8位字符的协议,但请尝试SEARCH CHARSET UTF-8 SUBJECT "spülmaschine",并确保您确实发送的是UTF-8而不是其他编码。这适用于许多服务器。我不知道你是否可以使libcurl以正确的方式使用IMAP文字。

注意:某些服务器软件不支持搜索。请记住,您始终可以使用socat或python' imaplib之类的工具,这样可以让您对协议进行非常低级别的访问以进行试验。

答案 1 :(得分:0)

很长一段时间后,我找到了解决方法:

int main(void) {
    CURL *curl;
    CURLcode res = CURLE_OK;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypass");
        curl_easy_setopt(curl, CURLOPT_URL, "imaps://imap.gmail.com:993");

        // see RFC6855 IMAP Support for UTF-8
        // imap.gmail.com states UTF8=ACCEPT in CAPABILITY response,
        // so enable it to use UTF-8 in quoted strings.
        // Must come after AUTHENTICATE and before SELECT.
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "ENABLE UTF8=ACCEPT");
        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        // SELECT INBOX broken out from URL
        if (res == CURLE_OK) {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SELECT INBOX");
            res = curl_easy_perform(curl);
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
        }

        if (res == CURLE_OK) {
            // U+00FC LATIN SMALL LETTER U WITH DIAERESIS is \xC3\xBC in UTF-8
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SEARCH
SUBJECT \"Sp\xC3\xBClmaschine\"");
            res = curl_easy_perform(curl);
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
    }
    return (int) res;
}