#include <iostream>
#include <cstdlib>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
using namespace std;
void fillClientStruct(struct sockaddr_in &server, char *ip, char *port)
{
server.sin_family = AF_INET;
struct hostent *hp = gethostbyname(ip);
//Check man gethostbyname for details of hostent
if( hp==0 )
{
cerr<<"Unknown Host! Error in gethostbyname()!\n";
exit(EXIT_FAILURE);
}
bcopy( (char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length );
server.sin_port = htons(atoi(port));
}
void sendUDPPacket(int sock, struct sockaddr_in &server, char *buffer)
{
socklen_t server_len = sizeof(server);
if( sendto(sock, buffer, strlen(buffer), 0, (sockaddr *)&server, server_len) < 0 )
{
cerr<<"Error : "<<errno;
}
}
int recvUDPPacket(int sock, struct sockaddr_in &server)
{
char buffer[32];
socklen_t server_len;
memset(buffer, '\0', sizeof(buffer));
if( recvfrom(sock, buffer, sizeof(buffer), 0, (sockaddr *)&server, &server_len)<0 )
{
cerr<<"Error : "<<errno;
}
return atoi(buffer);
}
int main(int argc, char* argv[])
{
if( argc!=3 )
{
cerr<<"Error in format: Enter SMTP mail server's IP address and port.\n";
return EXIT_FAILURE;
}
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if( sock<0 )
{
cerr<<"Error in socket()!";
return EXIT_FAILURE;
}
struct sockaddr_in toServer, fromServer;
int responseCode;
fillClientStruct(toServer, argv[1], argv[2]);
char temp[32];
char fromID[32], toID[32], myDomain[32], buffer[80], messageBody[1024];
cout<<"Mail From: ";
cin>>fromID;
cout<<"Mail To: ";
cin>>toID;
cout<<"Enter the message to be sent :";
memset(messageBody, '\0', sizeof(messageBody));
do
{
fgets(buffer, sizeof(buffer)-1, stdin);
buffer[strlen(buffer)-1] = '\0';
//Remove \n
if( strcmp(buffer, ".")!=0 )
{
strcat(messageBody, buffer);
strcat(messageBody, "\r\n");
}
}while( strcmp(buffer, ".")!=0 );
//Extract Domain
strcpy(temp, strstr(fromID, "@"));
if(temp != NULL)
strcpy(myDomain, temp+1);
else
{
cerr<<"Wrong email ID!";
return EXIT_FAILURE;
}
strcpy(temp, "HELO ");
strcat(temp, myDomain);
cout<<temp<<endl;
sendUDPPacket(sock, toServer, temp);
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Got Reply :"<<responseCode<<endl;
strcpy(temp, "MAIL FROM:<");
strcat(temp, fromID);
strcat(temp, ">");
cout<<temp<<endl;
sendUDPPacket(sock, toServer, temp);
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
strcpy(temp, "RCPT TO:<");
strcat(temp, toID);
strcat(temp, ">");
cout<<temp<<endl;
sendUDPPacket(sock, toServer, temp);
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
cout<<"DATA"<<endl;
sendUDPPacket(sock, toServer, "DATA");
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
cout<<messageBody<<endl;
sendUDPPacket(sock, toServer, messageBody);
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
cout<<'.'<<endl;
sendUDPPacket(sock, toServer, ".");
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
cout<<"QUIT"<<endl;
sendUDPPacket(sock, toServer, "QUIT");
responseCode = recvUDPPacket(sock, fromServer);
cout<<"Server says :"<<responseCode<<endl;
cout<<"Mail successfully sent!"<<endl;
return EXIT_SUCCESS;
}