如何使用c套接字通过postfix发送电子邮件? 如何创建消息程序
struct sockaddr_in addr;
char message[] = "MAIL From: vladyslav@vladyslav-virtual-machine\n \"vladyslav@vladyslav-virtual-machine\"\n\"Test mail\"\n\"This is a test email\"";
char buf[512];
//creating socket
int sock = socket(AF_INET, SOCK_DGRAM , 0);
//address parameters
addr.sin_family = AF_INET;
//connection port
addr.sin_port = htons(8);
// Inet 127.0.0.1.
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sendto(sock, message, sizeof(message), 0, (struct sockaddr *)&addr, sizeof(addr));
close(sock);
答案 0 :(得分:-1)
找到解决方案
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(int argc, char** argv)
{
int i;
struct sockaddr_in addr;
//commands for server
char* commands[] = {"eclo localhost\n", "mail from:test@example.com\n", "rcpt to:vladyslav@vladyslav-virtual-machine\n", "data\n", "Subject: Тест\n\nТест\n", "\n.\n", "quit\n"};
//creating socket
int sock = socket(AF_INET, SOCK_STREAM , 0);
if(sock < 0)
{
perror("error with creation of socket");
return -errno;
}
//parameters
addr.sin_family = AF_INET;
//port 25
addr.sin_port = htons(25);
// Inet 127.0.0.1.
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
//connecting to server
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
perror("error with creation of socket");
return -errno;
}
//sending commands
for(i = 0; i < sizeof(commands) / 4; i++)
send(sock, commands[i], strlen(commands[i]), 0);
//closing connection
close(sock);
return (EXIT_SUCCESS);
}