#include /* for socket(), connect(), send() and recv() */ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() */ #include /* for memset() */ #include /* for close() */ #include #include #include #include #include /* for gethostbyname() */ #include /* for MAXHOSTNAMELEN */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void err_sys( char *); int main(int argc, char ** argv) { char *inputName; /* inputName is the Sever's name you input */ char ipAddr[INET_ADDRSTRLEN]; char buf[50], wbuf[80]; struct hostent *hostInfo; struct sockaddr_in servaddr; unsigned short ServPort; char *ServIP; int sockfd, n; /*socket name*/ char localhostName[MAXHOSTNAMELEN]; /* store the local host name, MAXHOSTNAMELEN is the max host name length */ char revaddr[100]; /* for the output for the receiver's address */ if ((argc < 2) || (argc > 3)) { printf("Usage: %s [MailServerName]> []\n", argv[0]); exit(1); } if (argc == 3) { ServPort = atoi(argv[2]); /* use given port, if any */ } else { ServPort = 25; /* 25 is the default port number */ } /* get the server's IP address */ //your code /* socket connection */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ err_sys("Socket Error:"); exit(0); } /* initiaize servaddr object*/ //your code if ( inet_pton(AF_INET, inet_ntop(AF_INET, hostInfo->h_addr_list[0], ipAddr, INET_ADDRSTRLEN), &servaddr.sin_addr ) <= 0 ){ err_sys("System Error:"); exit(0); } /* connect to server, hint: servaddr*/ //your code /* get the local host name */ if (gethostname(localhostName, MAXHOSTNAMELEN) < 0) { printf("LocalHostName is not available.\n"); exit(1); } strcat(localhostName, "\n"); /* receive hello message from the server */ int BytesRcvd = 0 ; char RcvBuffer[100]; char *Rcv220; /* receive data from server, the return value is put to BytesRcvd*/ RcvBuffer[BytesRcvd] = '\0'; /* terminate the RcvBuffer! */ printf(RcvBuffer); /* print the Rcv buffer */ printf("Helo "); printf(localhostName,'\n'); /* send helo message to the server */ char SendString[100]; strcpy(SendString, "helo "); strcat(SendString, localhostName); /* send SendString to server*/ //your code /* get the information of sender's email address */ /*receive data from server, return value is put to BytesRcvd*/ // your code RcvBuffer[BytesRcvd] = '\0'; /* terminate the string! */ printf(RcvBuffer); /* print the echo buffer */ mail: printf("\nMAIL FROM:"); strcpy(SendString,"MAIL FROM:"); /* Prepare the sender's message for the Sever */ fgets(wbuf,80,stdin); strcat(SendString,wbuf); /*send out SendString to server*/ //your code /*receive data from server, return value is put to BytesRcvd*/ // your code RcvBuffer[BytesRcvd] = '\0'; /* terminate the string! */ printf(RcvBuffer); /* print the echo buffer */ /* if the reply is not ok, then try again */ int Rcv250 = 250; if (atoi(RcvBuffer) != Rcv250){ goto mail; } /* get the information of receiver's email address */ RcvBuffer[BytesRcvd] = '\0'; /* terminate the string! */ printf(RcvBuffer); /* print the echo buffer */ strcpy(revaddr, "To:"); receive: printf("RCPT to:"); strcpy(SendString,"RCPT to:"); /* Prepare the sender's message for the Sever */ fgets(wbuf,80,stdin); strcat(SendString,wbuf); wbuf[strlen(wbuf)-1]=0; strcat(revaddr,wbuf); strcat(revaddr,","); /*send SendString to server*/ //your code /*receive data from server*/ //your code RcvBuffer[BytesRcvd] = '\0'; /* terminate the string! */ printf(RcvBuffer); /* print the echo buffer */ /* if the reply is not ok, then try again */ if (atoi(RcvBuffer) != Rcv250){ goto receive; } char no[]="no\n"; /* send DATA to the Server */ strcpy (SendString, "DATA\n"); /*send SendString to server*/ //your code /*receive data from server*/ // your code /* send the content of the email */ printf ("Subject:"); strcpy(SendString, revaddr); strcat(SendString, "\r\n"); strcat(SendString,"Subject:"); /* Prepare the sender's message for the Sever */ fgets(wbuf,80,stdin); strcat(SendString,wbuf); strcat(SendString,"\r\n"); printf("Content of Email:(End with .)\n"); fflush(stdout); //strcat(SendString,wbuf); char mychar[3000]; content: fgets(mychar,3000,stdin); while ((strcmp(mychar,".\n")) != 0) { strcat(SendString,mychar); fgets(mychar,3000,stdin); } /* deal with the tricky single period */ printf("Do you want to send?(Type no to continue inputing the content)"); if((strcmp(fgets(mychar,3000,stdin),no)) == 0) { strcat(SendString,"..\n"); goto content; } /* the whole SendSting is ready to send */ strcat(SendString,"\r\n.\r\n"); /*Send SendString to server*/ //your code printf("send over\n"); fflush(stdout); /*receive data from server*/ //your code /* Quit procedure */ exit: printf("Type QUIT to quit this program.\n"); char quit[]="QUIT\n"; fgets(buf,50,stdin); if ((strcmp(buf, quit)) == 0){ strcpy(SendString,"QUIT\n"); /*send SendString to server*/ //your code /*receive data from server*/ // your code } else{ goto exit; } /* final stage of the program */ printf("Thank you for using my SMTP client.\n"); exit(0); } void err_sys( char * str) { perror(str); }