USING C PROGRAMMING IN LINUX Today you will implement the classic \'mail\' comma
ID: 3602952 • Letter: U
Question
USING C PROGRAMMING IN LINUX
Today you will implement the classic 'mail' command of UNIX that was (once upon a time) used by most of us to send emails. 'mail' is a program that negotiates a connection with a server and sends an email using SMTP (Simple Mail Transfer Protocol). SMTP is a simple text protocol. The following messages are sent between a client and an SMTP server on port 25 in the following order: 1. Client sends the "EHLO command with an argument equal to the hostname of the machine initiating the connection on port 25 2. Server sends an ack or a reject 3. Client sends the MAIL command with the return address for the message, which has the following form mail to: , and is ended by . 4. Server sends an ack or a reject 5. Client sends the RCPT command with the recipient address for the message, which has the following form rcpt to: and is ended by . 6. Server sends an ack or a reject 7. Client sends the "DATA" command to signal the beginning of the message text 8. Server sends an ack or a reject (If it can receive the message or not) 9. The message contain one or more lines of text. To terminate the message, the last line must be a period with a single linefeed. The message is terminated with: In 10. Server sends an ack or a reject (If it accepts or rejects the methods)Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
using namespace std;
int main(void)
{
// set family and socket type
struct addrinfo hints, *result;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// resolve hostname to IP address: specify host and port number (in char not int)
if(getaddrinfo("smtp.123.com", "25", &hints, &result) != 0)
{
freeaddrinfo(result);
puts("Could not resolve hostname.");
exit(1);
}
// create socket and free addrinfo memory
int newsocket = socket(result->ai_family, result->ai_socktype, 0);
if(newsocket == -1)
{
puts("Could not create socket.");
freeaddrinfo(result); // free addrinfo memory
close(newsocket);
exit(1);
}
// set socket timeouts
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout)); // zero timeout struct before use
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(newsocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); // send timeout
setsockopt(newsocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // receive timeout
// connect to website
if(connect(newsocket, result->ai_addr, result->ai_addrlen) == -1)
{
puts("Could not connect.");
freeaddrinfo(result); // free addrinfo memory
close(newsocket);
exit(1);
}
char t[500];
int y=recv(newsocket,t,100,0);
t[y]='';
cout<<t<<" ";
strcpy(t,"EHLO ");
send(newsocket,t,sizeof(t),0);
memset(t,'',100);
y=recv(newsocket,t,100,0);
t[y]='';
cout<<"message "<<t;
memset(t,'',100);
strcpy(t,"MAIL FROM: robo.123@312.com ");
send(newsocket,t,sizeof(t),0);
memset(t,'',100);
y=recv(newsocket,t,100,0);
t[y]='';
cout<<"message: "<<t<<" ";
strcpy(t,"RCPT TO: robo.@123mail.com> ");
send(newsocket,t,sizeof(t),0);
memset(t,'',100);
y=recv(newsocket,t,100,0);
t[y]='';
cout<<t<<" ok ";
return 0;
}
let me know if its help. as i have tested this code at my end and working fine .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.