-
100+ errors
I am kind of new to C++ but know some. I am using Dev C++ and am getting all sorts of errors from this code.
Can someone please tell me what I am doing wrong.
Code:
#include <cstdlib>
#include < winsock.h >
WORD version = MAKEWORD(1,1);
WSADATA wsaData;
SOCKET theSocket;
char Buf[256],myBuf[256]; // Buf -for server answer and myBuf -for your commands
int nRet; // For eventual errors
int conect(char *server) { //Connects to a server using "Winsock"
// Start up Winsock
nRet=WSAStartup(version, &wsaData);
if (nRet!=0) {return(0);}
// Store information about the server
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(server);
if (lpHostEntry == NULL) {
WSACleanup();
return(0);
}
// Create the socket
theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (theSocket == INVALID_SOCKET) {
WSACleanup();
return(0);
}
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
saServer.sin_port = htons(25); // Port 25
// Connect to the server
nRet = connect(theSocket,(LPSOCKADDR)&saServer,sizeof(struct sockaddr));
if (nRet == SOCKET_ERROR) {
WSACleanup();
return(0);
}
nRet = recv(theSocket,Buf,sizeof(Buf),0);
if (nRet == SOCKET_ERROR) {
WSACleanup();
return(0);
}
if (Buf[0]=='4' || Buf[0]=='5') return(0); // Not a '220' Hello
if (Buf[0]=='2' && Buf[1]=='2' && Buf[2]=='0') {
sendmail(); // Ok to send mails
}
//Close the connection
closesocket(theSocket);
// Shutdown Winsock
WSACleanup();
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char from [100], mailto [100], msg [500];
cout << "What is your e-mail address?";
cin >> from;
cout << endl << "Who do you want to send to?";
cin >> mailto;
cout << "Sending From: " << from << endl << "Sending To: " << mailto << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void sendmail() { //Sends an e-mail with MIME encoding
int err=0;
char ch[1];
// "HELO" the server
strcpy(myBuf, "HELO <");> strcat(myBuf,helo);
strcat(myBuf,">\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
recv(theSocket,Buf,sizeof(Buf),0);
// MAIL FROM...
if (Buf[0]=='2' && Buf[1]=='5' && Buf[2]=='0') {
strcpy(myBuf, "MAIL FROM:<");> strcat(myBuf,from);
strcat(myBuf,">\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
recv(theSocket,Buf,sizeof(Buf),0);
}
if (Buf[0]=='4' || Buf[0]=='5') err=1;
if (Buf[0]=='2' && Buf[1]=='5' && Buf[2]=='0' && err==0) {
// MAIL TO...
strcpy(myBuf, "RCPT TO:<");> strcat(myBuf, mailto);
strcat(myBuf, ">\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
recv(theSocket,Buf,sizeof(Buf),0);
}
if (Buf[0]=='4' || Buf[0]=='5') err=1;
if (Buf[0]=='2' && Buf[1]=='5' && err==0) {
strcpy(myBuf, "DATA\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
recv(theSocket,Buf,sizeof(Buf),0);
}
if (Buf[0]=='4' || Buf[0]=='5') err=1;
// MIME encoded message
if (Buf[0]=='3' && Buf[1]=='5' && Buf[2]=='4' && err==0) {
strcpy(myBuf, "From: <");> strcat(myBuf, email); // Can change "email" to anything
strcat(myBuf, ">\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "Subject: Hello\x0d\x0a"); // Your subject goes here
send(theSocket,myBuf,strlen(myBuf),0);
// MIME stuff
strcpy(myBuf, "MIME-Version: 1.0\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "Content-Type: multipart/mixed;\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, " boundary = \"bla\"\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "X-Priority: 3\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "X -MSMail - Priority: Normal\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "X-Mailer: mailer@localhost\x0d\x0a\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "This is a multi-part message in MIME format.\x0d\x0a\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "--bla\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "Content-Type: text/plain; charset:us-ascii\x0d\x0a\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
// This is the message (change it as you whish)
strcpy(myBuf, msg << "\x0d\x0a\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "--bla\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "Content-Type: application/x-msdownload;\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
}
f.close();
strcpy(myBuf, "\x0d\x0a--bla--\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
strcpy(myBuf, "\x0d\x0a.\x0d\x0a"); // End transmission ;)
send(theSocket,myBuf,strlen(myBuf),0);
recv(theSocket,Buf,sizeof(Buf),0);
}
if (Buf[0]=='4' || Buf[0]=='5') err=1;
// QUIT (bye bye)
strcpy(myBuf, "QUIT\x0d\x0a");
send(theSocket,myBuf,strlen(myBuf),0);
}
-
Well you said you are getting over 100 errors. Try posting the first few. Generally when you have that many errors you either are making the same mistake over and over or you have an error that cascades into other compile time errors. But thats all a guess since you haven't given us anything to start from
-
Quote:
Code:
#include <cstdlib>
#include < winsock.h >
WORD version = MAKEWORD(1,1);
Put code in functions.
-
You know, you can press "compile" long before the program is complete. That way you can more easily spot when you start to do something wrong, rather than relying on people on message boards to fix a whole big mess.
-
Also check your spelling. I noticed a "conect" command, later used as "connect". I can't really do socket prorgamming myself, but I can still spot a typo in it. I'm assuming that most of your errors would come from small typos like that. C++ is kinda picky....
-
As far as I can tell you never connect from main.
Also sendmail needs to be declared before connect otherwise the compiler will give you an error.