/* Copyright (C) 1994 by Charles Sandmann (sandmann@clio.rice.edu) * * This software comes with ABSOLUTELY NO WARRANTY, and may be used and * copied freely as long as the copyright is left intact. * * This program based on a program by Rush Record. */ #include #include #include #include #include #include #ifdef VMS #ifdef MULTINET #include "multinet_root:[multinet.include.sys]types.h" #include "multinet_root:[multinet.include.sys]socket.h" #include "multinet_root:[multinet.include.netinet]in.h" #include "multinet_root:[multinet.include]netdb.h" #define read socket_read #define write socket_write #define close socket_close #endif #ifdef WOLLONGONG #include #include #include #include #define read netread #define write netwrite #define close netclose #endif #ifdef UCX #include #include #include #include #endif #ifdef LIBCMU #include #include #include #include #define read(a,b,c) recv(a,b,c,0) #define write(a,b,c) send(a,b,c,0) #define close cmu_close #endif #else #include #include #ifdef WIN32 #include #define read(a,b,c) recv(a,b,c,0) #define write(a,b,c) send(a,b,c,0) #define close closesocket #define errno WSAGetLastError() #else #include #include #include #include #endif #endif int read_resp(chan,buf,buflen) int chan; char *buf; int buflen; { static int cur = 0,remain = 0; static char vbuf[5120]; int bufptr; bufptr = 0; buflen--; again: if(!remain) { remain = read(chan,vbuf,sizeof(vbuf)); cur = 0; if(remain <= 0) { remain=0; buf[bufptr] = '\0'; return(bufptr); } } while(remain--) { if((buf[bufptr++] = vbuf[cur++]) == '\n' || bufptr >= buflen) { buf[bufptr] = '\0'; return(bufptr); } } remain = 0; goto again; } int main(argc,argv) int argc; char **argv; { FILE *fp; int controls,outputchan,inputchan,controlport,l,i,j,k,n; char *p; char service[256],host[256],buf[2560],file[512]; struct sockaddr_in controlsocket; struct servent *svc_info; struct hostent *host_info_ptr; #ifdef WIN32 { WSADATA WSAData; int status; if ((status = WSAStartup(MAKEWORD(1,1), &WSAData)) == 0) { printf("Description: %s\n", WSAData.szDescription); printf("Status: %s\n", WSAData.szSystemStatus); } else printf("%d is the err", status); } #endif if(argc < 4 || argc > 5) { printf("Usage: cwsmail smtp-host from-user@host to-user@host [file]\n"); exit(0); } strcpy(host,argv[1]); /* get host info */ if(!(host_info_ptr = gethostbyname(host))) { printf("Unknown host: %s\n",host); exit(1); } else printf("Connecting to smtp host: %s\n",host); controlport = htons(25); /*smtp*/ memset(&controlsocket,0,sizeof(controlsocket)); memcpy(&controlsocket.sin_addr,host_info_ptr->h_addr_list[0],host_info_ptr->h_length); controlsocket.sin_family = host_info_ptr->h_addrtype; controlsocket.sin_port = controlport; /* create control path */ if((controls = socket(host_info_ptr->h_addrtype,SOCK_STREAM,0)) < 0) { printf("Error creating socket for control path.\n"); exit(1); } loop: if(connect(controls,(struct sockaddr *)&controlsocket,sizeof(controlsocket))== -1) { printf("Control connection failed, errno=%d.\n",errno); perror("Control connection"); close(controls); exit(1); } inputchan = controls; outputchan = inputchan; /* dup(controls); */ read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); /* */ write(outputchan,"HELO cwsmail\n",strlen("HELO cwsmail\n")); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); sprintf(buf,"MAIL From: <%s>\n",argv[2]); write(outputchan,buf,strlen(buf)); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); sprintf(buf,"RCPT To: <%s>\n",argv[3]); write(outputchan,buf,strlen(buf)); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); sprintf(buf,"DATA\n"); write(outputchan,buf,strlen(buf)); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); if(argc == 5) fp = fopen(argv[4],"r"); else { printf("Reading message from stdin...\n"); fp = stdin; } while(fgets(buf,sizeof(buf),fp)) { write(outputchan,buf,strlen(buf)); } if(argc ==5) fclose(fp); write(outputchan,".\n",2); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); write(outputchan,"QUIT\n",5); read_resp(inputchan,buf,sizeof(buf)); printf("%s",buf); exit(0); }