C语言写一个可直接运行的简单电子邮件接收与发送代码
时间: 2024-03-25 21:40:13 浏览: 13
以下是一个简单的C代码示例,用于接收与发送电子邮件,需要使用libcurl库:
```c
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define SMTP_SERVER "smtp.gmail.com"
#define SMTP_PORT 587
#define IMAP_SERVER "imap.gmail.com"
#define IMAP_PORT 993
#define EMAIL_ADDRESS "your_email@gmail.com"
#define EMAIL_PASSWORD "your_email_password"
void send_email(char *to, char *subject, char *body, char *file) {
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct curl_slist *headers = NULL;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
// Set recipient
recipients = curl_slist_append(recipients, to);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
// Set email headers
headers = curl_slist_append(headers, "Content-Type: multipart/mixed");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set email body
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "subject", CURLFORM_COPYCONTENTS, subject, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "body", CURLFORM_COPYCONTENTS, body, CURLFORM_END);
if (file) {
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, file, CURLFORM_END);
}
curl_easy_setopt(curl, CURLOPT_URL, "smtps://" SMTP_SERVER ":" SMTP_PORT);
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long) CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_USERNAME, EMAIL_ADDRESS);
curl_easy_setopt(curl, CURLOPT_PASSWORD, EMAIL_PASSWORD);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, EMAIL_ADDRESS);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_MAIL_AUTH, CURLAUTH_LOGIN);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "Failed to send email: %s\n", curl_easy_strerror(res));
} else {
printf("Email sent successfully.\n");
}
curl_slist_free_all(recipients);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_formfree(formpost);
}
}
void receive_email() {
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *headers = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
// Set email headers
headers = curl_slist_append(headers, "Content-Type: text/plain");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_USERNAME, EMAIL_ADDRESS);
curl_easy_setopt(curl, CURLOPT_PASSWORD, EMAIL_PASSWORD);
curl_easy_setopt(curl, CURLOPT_URL, "imaps://" IMAP_SERVER ":" IMAP_PORT "/INBOX");
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long) CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "Failed to receive email: %s\n", curl_easy_strerror(res));
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
}
int main() {
send_email("recipient@example.com", "Test email", "This is a test email.", "test.jpg");
receive_email();
return 0;
}
```
请记得将`EMAIL_ADDRESS`和`EMAIL_PASSWORD`更改为您自己的电子邮件地址和密码,以及将`SMTP_SERVER`和`IMAP_SERVER`更改为您的电子邮件提供商的SMTP和IMAP服务器。同时,确保您已经安装了libcurl库,并在编译时链接了它。