imap-tls.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <curl/curl.h>
  24. /* This is a simple example showing how to fetch mail using libcurl's IMAP
  25. * capabilities. It builds on the imap-fetch.c example adding transport
  26. * security to protect the authentication details from being snooped.
  27. *
  28. * Note that this example requires libcurl 7.30.0 or above.
  29. */
  30. int main(void)
  31. {
  32. CURL *curl;
  33. CURLcode res = CURLE_OK;
  34. curl = curl_easy_init();
  35. if(curl) {
  36. /* Set username and password */
  37. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  38. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  39. /* This will fetch message 1 from the user's inbox */
  40. curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1");
  41. /* In this example, we'll start with a plain text connection, and upgrade
  42. * to Transport Layer Security (TLS) using the STARTTLS command. Be careful
  43. * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer
  44. * will continue anyway - see the security discussion in the libcurl
  45. * tutorial for more details. */
  46. curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
  47. /* If your server doesn't have a valid certificate, then you can disable
  48. * part of the Transport Layer Security protection by setting the
  49. * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).
  50. * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  51. * curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  52. * That is, in general, a bad idea. It is still better than sending your
  53. * authentication details in plain text though.
  54. * Instead, you should get the issuer certificate (or the host certificate
  55. * if the certificate is self-signed) and add it to the set of certificates
  56. * that are known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH. See
  57. * docs/SSLCERTS for more information. */
  58. curl_easy_setopt(curl, CURLOPT_CAINFO, "/path/to/certificate.pem");
  59. /* Since the traffic will be encrypted, it is very useful to turn on debug
  60. * information within libcurl to see what is happening during the
  61. * transfer */
  62. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  63. /* Perform the fetch */
  64. res = curl_easy_perform(curl);
  65. /* Check for errors */
  66. if(res != CURLE_OK)
  67. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  68. curl_easy_strerror(res));
  69. /* Always cleanup */
  70. curl_easy_cleanup(curl);
  71. }
  72. return (int)res;
  73. }