Some Winsock Errors
I have recently gotten into winsock programming, and i gotta tell you, i feel like such a hacker when i get replies from the servers, lol.
Anyways, i made a program that is supposed to send me an email, and everything is good, it connects to the smtp.gmail.com server then the HELO command works perfect, but then when i send the "MAIL FROM:<blahblah@server.com>" they say, "502 5.5.1 Unrecognized Command" and idk why and i searched on google and nothing has worked. Here is my code (not the full thing just what you need):
Anyways, i made a program that is supposed to send me an email, and everything is good, it connects to the smtp.gmail.com server then the HELO command works perfect, but then when i send the "MAIL FROM:<blahblah@server.com>" they say, "502 5.5.1 Unrecognized Command" and idk why and i searched on google and nothing has worked. Here is my code (not the full thing just what you need):
Code:
int main()
{
WSAData WsaDat;
if(WSAStartup(MAKEWORD(2,2), &WsaDat) != 0)
{
cout<<"Initialization of Winsock failed.";
WSACleanup();
system("PAUSE");
return 0;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(Socket == INVALID_SOCKET)
{
cout<<"Invalid Socket.";
WSACleanup();
system("PAUSE");
return 0;
}
struct hostent *host;
if((host = gethostbyname("smtp.gmail.com")) == NULL)
{
cout<<"Cannot connect to host";
WSACleanup();
system("PAUSE");
return 0;
}
SOCKADDR_IN sockaddr;
sockaddr.sin_port = htons(587);
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = *((unsigned long*) host->h_addr);
if(connect(Socket, (SOCKADDR*) (&sockaddr), sizeof(sockaddr)) != 0)
{
cout<<"Failed to connect.";
WSACleanup();
system("PAUSE");
return 0;
}
char buffer[1000];
memset(buffer, 0, 1000);
recv(Socket, buffer, 1000, 0);
cout<<buffer;
sprintf(buffer, "HELO %s", "smtp.gmail.com\r\n");
send(Socket, buffer, sizeof(buffer), 0);
recv(Socket, buffer, 200, 0);
cout<<buffer;
sprintf(buffer, "MAIL FROM:<@exaMpLe.com>\r\n");
send(Socket, mailfrom, strlen(mailfrom), 0);
recv(Socket, mailfrom, 200, 0);
cout<<mailfrom;
sprintf(buffer, "RCPT TO:<%s>\r\n", to);
send(Socket, buffer, strlen(buffer), 0);
recv(Socket, buffer, 200, 0);
sprintf(buffer, "STARTTLS\r\n");
send(Socket, buffer, strlen(buffer), 0);
recv(Socket, buffer, 200, 0);
cin.get();
shutdown(Socket, SD_SEND);
closesocket(Socket);
WSACleanup();
return 0;
}
