/* SMTP.c: TCP/IP client side, use socket functions. Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) MSVC 1.52 Compile mode: large Project: ICMP.c ..\..\..\Lib\uPAC5000.lib ..\..\lib\tcp_dm32.lib [Jan 05, 2012] by Liam */ #include #include #include #include #include "..\..\..\lib\upac5000.h" #include "..\..\lib\Tcpip32.h" #define SMTP 25 int Quit=0, Quit1=0; fd_set rfds; // set of socket file descriptors for reading struct sockaddr_in sin; char *ipaddr="168.95.4.211"; // change for your SMTP server address char InDataBuf[1460], DataBuf[200]; int length, SmtpMode=0; char HELO[]="HELLO ICPDAS\r\n"; char CC[50], BCC[50]; char content[1024]; char MAIL[64]; char RCPT[64]; void key_string(char *sinput, char *skeyin) { char data; int i=0; Print("", sinput); while((data=Getch())!='\r') { Putch(data); if(data=='\b') { i--; if(i<0) i=0; } else skeyin[i++]=data; } if(i>0) skeyin[i]=0; else strcpy(skeyin, sinput); } void main(int argc, char *argv[]) { unsigned long t1; struct sockaddr_in ssin; /* client Internet endpoing address */ struct timeval seltime={6, 0}; unsigned ServicePort=SMTP; int sin_len; /* length of sockaddr_in */ int client; int i, err=0; char kin; InitLib(); sprintf(MAIL, "%s", "user@icpdas.com"); sprintf(RCPT, "%s", "user@icpdas.com"); sprintf(content, "%s", "hello"); if(NetStart()!=NoError) { Print("Inint Ethernet error.\r\n"); return; } // In general it will show 10ms while(!Quit) { Print("\r\n"); Print("1.Change the SMTP server.(default:168.95.4.211)\r\n"); Print("2.Input the 'Mail From' address.(xxx@xxx.xxx.xxx)\r\n"); Print("3.Input the Mail 'RCPT To' address.(xxx@xxx.xxx.xxx)\r\n"); Print("4.Input Mail CC address (xxx@xxx.xxx.xxx)\r\n"); Print("5.Input Mail BCC address (xxx@xxx.xxx.xxx)\r\n"); Print("6.Connect to the SMTP server and send a mail.\r\n"); Print("Q to exit program.\r\n"); Print("Please Input your select? "); kin=Getch(); Print("%c\r\n", kin); i=0; switch(kin) { case 'Q': case 'q': Quit=1; break; case '1': key_string(ipaddr, InDataBuf); strcpy(ipaddr, InDataBuf); Print("\n%s", ipaddr); break; case '2': key_string(MAIL,InDataBuf); strcpy(MAIL, InDataBuf); Print("\n%s", MAIL); break; case '3': key_string(RCPT, InDataBuf); strcpy(RCPT, InDataBuf); Print("\n%s", RCPT); break; case '4': key_string(CC, InDataBuf); strcpy(CC, InDataBuf); Print("\n%s", CC); break; case '5': key_string(BCC, InDataBuf); strcpy(BCC, InDataBuf); Print("\n%s", BCC); break; case '6': err=client=socket(PF_INET, SOCK_STREAM, 0); // TCP/IP if(client<0) { Print( "cannot create socket! error %d\r\n", err); Quit=1; break; } // addressing for master socket memset(&sin, 0, sizeof(sin)); // bzero is a unix system call sin.sin_family=AF_INET; sin.sin_addr.s_addr=inet_addr(ipaddr); sin.sin_port=htons(ServicePort); FD_ZERO(&rfds); t1=GetTimeTicks(); SOCKET_NOBLOCK(client); // on success return 0 if(connect(client, (struct sockaddr *) &sin, sizeof(sin))) { extern int errno; unsigned cnt=0; if(errno==EINPROGRESS) { #define ConnectTimeout 4000 while(GetTimeTicks()-t10) { length=readsocket(client, InDataBuf, sizeof(InDataBuf)); if(length>0) { for(i=0; i\r\n", MAIL); err=writesocket(client, DataBuf, strlen(DataBuf)); SmtpMode++; break; case 2: if((strlen(CC)>0) && (strlen(BCC)>0)) sprintf(DataBuf, "RCPT TO:<%s>;<%s>;<%s>\r\n", RCPT, CC, BCC); else if(strlen(CC)>0) sprintf(DataBuf, "RCPT TO:<%s>;<%s>\r\n", RCPT, CC); else if(strlen(BCC)>0) sprintf(DataBuf, "RCPT TO:<%s>;<%s>\r\n", RCPT, BCC); else sprintf(DataBuf, "RCPT TO:<%s>\r\n", RCPT); err=writesocket(client, DataBuf, strlen(DataBuf)); SmtpMode++; break; case 3: Print("Send:DATA\r\n"); err=writesocket(client, "DATA\r\n", 6); SmtpMode++; break; case 4: sprintf(DataBuf, "From:<%s>\r\nTo:<%s>\r\nCc:<%s>\r\nSubject:This is a SMTP demo\r\n%s\r\n.\r\n", MAIL, RCPT, CC, content); Print(DataBuf); err=writesocket(client, DataBuf, strlen(DataBuf)); SmtpMode++; break; case 5: Print("Send:QUIT\r\n"); err=writesocket(client, "QUIT\r\n", 6); SmtpMode++; break; } } else if(length==0) { // socket is disconnected Print("[Disconnected by remote server.]"); Quit1=1; } else { // error Print("[Error:Socket read error!]"); Quit1=1; } } } closesocket(client); SmtpMode=0; Quit1=0; break; } } Nterm(); }