How to mail in c
In C, you can use the mail()
function from the mail.h
header file to send an email. Here's a basic example of how to use it:
#include <mail.h>
int main() {
mail("[email protected]", "Subject", "Body");
return 0;
}
This will send an email to [email protected]
with the subject "Subject" and the body "Body". However, this is a very basic example and doesn't include many features that a real email client would have.
The mail()
function is not a standard C function and is not available on all platforms. It's also not a good idea to use it in a real-world application because it's not secure and doesn't support many features that you would expect from an email client.
If you need to send emails in a C program, you're better off using a library that's designed for that purpose, such as libcurl
or libmail
. These libraries provide more features and are more secure than the mail()
function.
Here's an example of how you could use libcurl
to send an email:
#include <curl/curl.h>
#include <string.h>
int main() {
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.example.com");
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_USERNAME, "username");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "<[email protected]>");
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, "<[email protected]>");
curl_easy_setopt(curl, CURLOPT_READDATA, "Hello, world!");
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "cURL error: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
This example sends an email to [email protected]
from [email protected]
using the smtp.example.com
SMTP server. The email body is "Hello, world!".