fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <curl/curl.h>
  4.  
  5. #define GMAIL_USERNAME "your_email@gmail.com"
  6. #define GMAIL_PASSWORD "your_password"
  7.  
  8. #define SMTP_SERVER "smtps://smtp.gmail.com"
  9. #define SMTP_PORT 465
  10.  
  11. #define EMAIL_FROM GMAIL_USERNAME
  12. #define EMAIL_TO "recipient@example.com"
  13. #define EMAIL_SUBJECT "Hello"
  14. #define EMAIL_BODY "Hi there,\n\nThis is an automated email."
  15.  
  16. int main() {
  17. CURL *curl;
  18. CURLcode res = CURLE_OK;
  19.  
  20. curl = curl_easy_init();
  21. if (curl) {
  22. curl_easy_setopt(curl, CURLOPT_USERNAME, GMAIL_USERNAME);
  23. curl_easy_setopt(curl, CURLOPT_PASSWORD, GMAIL_PASSWORD);
  24. curl_easy_setopt(curl, CURLOPT_URL, SMTP_SERVER);
  25. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
  26. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, EMAIL_FROM);
  27.  
  28. struct curl_slist *recipients = NULL;
  29. recipients = curl_slist_append(recipients, EMAIL_TO);
  30. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  31.  
  32. curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
  33. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  34.  
  35. struct curl_slist *headers = NULL;
  36. headers = curl_slist_append(headers, "Content-Type: text/plain; charset=\"UTF-8\"");
  37. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  38.  
  39. curl_easy_setopt(curl, CURLOPT_READDATA, NULL);
  40. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, EMAIL_BODY);
  41. curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(EMAIL_BODY));
  42.  
  43. res = curl_easy_perform(curl);
  44.  
  45. if (res != CURLE_OK)
  46. fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  47.  
  48. curl_slist_free_all(recipients);
  49. curl_slist_free_all(headers);
  50. curl_easy_cleanup(curl);
  51. } else {
  52. fprintf(stderr, "Failed to initialize libcurl\n");
  53. return 1;
  54. }
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0.03s 26152KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

#define GMAIL_USERNAME "your_email@gmail.com"
#define GMAIL_PASSWORD "your_password"

#define SMTP_SERVER "smtps://smtp.gmail.com"
#define SMTP_PORT 465

#define EMAIL_FROM GMAIL_USERNAME
#define EMAIL_TO "recipient@example.com"
#define EMAIL_SUBJECT "Hello"
#define EMAIL_BODY "Hi there,\n\nThis is an automated email."

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

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, GMAIL_USERNAME);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, GMAIL_PASSWORD);
        curl_easy_setopt(curl, CURLOPT_URL, SMTP_SERVER);
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, EMAIL_FROM);

        struct curl_slist *recipients = NULL;
        recipients = curl_slist_append(recipients, EMAIL_TO);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

        curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: text/plain; charset=\"UTF-8\"");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(curl, CURLOPT_READDATA, NULL);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, EMAIL_BODY);
        curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(EMAIL_BODY));

        res = curl_easy_perform(curl);

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

        curl_slist_free_all(recipients);
        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
    } else {
        fprintf(stderr, "Failed to initialize libcurl\n");
        return 1;
    }

    return 0;
}