C mail send code

Here is an example of how to send an email using the c programming language:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define SMTP_PORT 25

int main() {
    int sockfd, connfd;
    struct sockaddr_in serv_addr, cli_addr;
    char buffer[256];
    char *message = "Subject: Test Email\n\nHello, this is a test email.";

    // Create a socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("socket creation failed");
        exit(1);
    }

    // Set up the server address
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SMTP_PORT);
    inet_pton(AF_INET, "smtp.example.com", &serv_addr.sin_addr);

    // Connect to the server
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("connect failed");
        exit(1);
    }

    // Send the email
    send(sockfd, "HELO example.com\r\n", strlen("HELO example.com\r\n"), 0);
    send(sockfd, "MAIL FROM: <[email protected]>\r\n", strlen("MAIL FROM: <[email protected]>\r\n"), 0);
    send(sockfd, "RCPT TO: <[email protected]>\r\n", strlen("RCPT TO: <[email protected]>\r\n"), 0);
    send(sockfd, "DATA\r\n", strlen("DATA\r\n"), 0);
    send(sockfd, message, strlen(message), 0);
    send(sockfd, "\r\n.\r\n", strlen("\r\n.\r\n"), 0);

    // Close the socket
    close(sockfd);

    return 0;
}

This code creates a socket and connects to an SMTP server (in this case, smtp.example.com on port 25). It then sends a series of commands to the server to establish a connection, specify the sender and recipient, and send the email.

Note that this is a very basic example, and you may need to add additional error handling and functionality to make it work in a real-world scenario. Additionally, this code assumes that the SMTP server is configured to allow unauthenticated connections, which is not recommended in a production environment.

Also, you need to replace [email protected] and [email protected] with your actual email address and the recipient's email address.

You can compile this code using the following command:

gcc -o send_email send_email.c

And then run it using:

./send_email

This will send the email to the specified recipient.