imap-multi.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * Get IMAP email with the multi interface
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. /* This is a simple example showing how to fetch mail using libcurl's IMAP
  32. * capabilities. It builds on the imap-fetch.c example to demonstrate how to
  33. * use libcurl's multi interface.
  34. */
  35. int main(void)
  36. {
  37. CURL *curl;
  38. CURLM *mcurl;
  39. int still_running = 1;
  40. curl_global_init(CURL_GLOBAL_DEFAULT);
  41. curl = curl_easy_init();
  42. if(!curl)
  43. return 1;
  44. mcurl = curl_multi_init();
  45. if(!mcurl)
  46. return 2;
  47. /* Set username and password */
  48. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  49. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  50. /* This will fetch message 1 from the user's inbox */
  51. curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1");
  52. /* Tell the multi stack about our easy handle */
  53. curl_multi_add_handle(mcurl, curl);
  54. do {
  55. CURLMcode mc = curl_multi_perform(mcurl, &still_running);
  56. if(still_running)
  57. /* wait for activity, timeout or "nothing" */
  58. mc = curl_multi_poll(mcurl, NULL, 0, 1000, NULL);
  59. if(mc)
  60. break;
  61. } while(still_running);
  62. /* Always cleanup */
  63. curl_multi_remove_handle(mcurl, curl);
  64. curl_multi_cleanup(mcurl);
  65. curl_easy_cleanup(curl);
  66. curl_global_cleanup();
  67. return 0;
  68. }