-Register for Learning.
-Read forum rules before register.
-Register for see full topics.
*Active on Forum*

Mailer Buatan Sendiri Dengan C++

Share

anitawulandari
leaderregional
leaderregional

Jumlah posting : 14
Reputation : 0
Join date : 17.05.13

Mailer Buatan Sendiri Dengan C++

Post by anitawulandari on Thu Aug 28, 2014 4:36 pm

langsung aja nih scriptnya

Code:
#include <iostream>
#include <string>
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
// UWAGA
// settings > compiler > global settings > linker settings > link libraries > add
// C:/Program Files/CodeBlocks/MinGW/lib/linws2_32.a (WinSock2)
// Good Mailer by Kashiki

using namespace std;
string base64_encode(string text) //kali
{
string b = "kali";
if(!strcmp(text.c_str(), "doooooopa"))
b = "ZG9vb29vb3Bh";
else if(!strcmp(text.c_str(), "cppmailer"))
b = "Y3BwbWFpbGVy";
else if(!strcmp(text.c_str(), "cppmailer@o2.pl"))
b = "Y3BwbWFpbGVyQG8yLnBs";
return b;
}
class cppMailer
{
private:
string the_host;
int the_port;
string the_username;
string the_password;
int retval, loopflag = 0;
int i, loopcount, maxloop=-1;
unsigned int addr;
int socket_type = SOCK_STREAM;
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
SOCKET conn_socket;

public:
void setUser(string user, string pass)
{
the_username = user;
the_password = pass;
}
void setServer(string h, int p)
{
the_host = h;
the_port = p;
}
void connectionOpen()
{
if ((retval = WSAStartup(0x202, &wsaData)) != 0)
{
fprintf(stderr,"Client: WSAStartup() failed with error %d\n", retval);
WSACleanup();
return;
}
//else
// printf("Client: WSAStartup() is OK.\n");

hp = gethostbyname(the_host.c_str());

if (hp == NULL )
{
fprintf(stderr, "Client: Cannot resolve address \"%s\": Error %d\n", the_host.c_str(), WSAGetLastError());
WSACleanup();
exit(1);
}
//else
// printf("Client: gethostbyaddr() is OK.\n");
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(the_port);

conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
if (conn_socket <0 )
{
fprintf(stderr,"Client: Error Opening socket: Error %d\n", WSAGetLastError());
WSACleanup();
return;
}
//else
// printf("Client: socket() is OK.\n");

//printf("Client: Client connecting to: %s.\n", hp->h_name);
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
fprintf(stderr,"Client: connect() failed: %d\n", WSAGetLastError());
WSACleanup();
return;
}
//else
//printf("Client: connect() is OK.\n");
}
string connectionCmd()
{
//Sleep(500);
string ret = "";
char Buffer[128];
int sizeofbufer = sizeof(Buffer);
do
{
retval = recv(conn_socket, Buffer, sizeofbufer, 0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"Client: recv() failed: error %d.\n", WSAGetLastError());
closesocket(conn_socket);
WSACleanup();
return "ERR 2";
}
else
{
//printf("Client: recv() is OK.\n");
ret = ret + string(Buffer);
}

}
while(retval == sizeofbufer);

// We are not likely to see this with UDP, since there is no
// 'connection' established.
/*
if (retval == 0)
{
//printf("Client: Server closed connection.\n");
closesocket(conn_socket);
WSACleanup();
return "ERR 3";
}
*/

//printf("Client: Received %d bytes, data \"%s\" from server.\n", retval, Buffer);

return ret;
}
string connectionCmd(string cmd)
{
return connectionCmd(cmd, true);
}
string connectionCmd(string cmd, bool endline)
{
char Buffer[128];
cout<<"<< "<<cmd<<endl;
if(endline)
cmd = cmd+"\r\n";
wsprintf(Buffer, cmd.c_str(), 0);
retval = send(conn_socket, Buffer, cmd.length(), 0);
if(retval == SOCKET_ERROR)
{
fprintf(stderr,"Client: send() failed: error %d.\n", WSAGetLastError());
WSACleanup();
return "ERR 1";
}
//else
//printf("Client: send() is OK.\n");
//printf("Client: Sent data \"%s\"\n", Buffer);
return connectionCmd();
}
void connectionClose()
{
closesocket(conn_socket);
WSACleanup();
}
bool sendMessage(string to_name, string to_mail, string subject, string body)
{
string str, msg;
connectionOpen();
//string httpget = "";
str = connectionCmd();
cout<<">> "<<str<<endl;
msg = str.substr(0, 3);
// cout<<"CODE '"<<msg<<"'"<<endl;
if(strcmp(msg.c_str(), "220"))
{
connectionClose();
return false;
}
str = connectionCmd("EHLO "+the_host);
cout<<">> "<<str<<endl;
str = connectionCmd("AUTH LOGIN");
cout<<">> "<<str<<endl;
msg = str.substr(0, 3);
// cout<<"CODE '"<<msg<<"'"<<endl;
if(strcmp(msg.c_str(), "334"))
{
connectionClose();
return false;
}
str = connectionCmd(base64_encode(the_username));
cout<<">> "<<str<<endl;
msg = str.substr(0, 3);
// cout<<"CODE '"<<msg<<"'"<<endl;
if(strcmp(msg.c_str(), "334"))
{
connectionClose();
return false;
}
str = connectionCmd(base64_encode(the_password));
cout<<">> "<<str<<endl;
msg = str.substr(0, 3);
// cout<<"CODE '"<<msg<<"'"<<endl;
if(strcmp(msg.c_str(), "235"))
{
connectionClose();
return false;
}
str = connectionCmd("MAIL FROM: <cppmailer@o2.pl>");
cout<<">> "<<str<<endl;

str = connectionCmd("RCPT TO: <"+to_mail+">");
cout<<">> "<<str<<endl;
string cmd = "DATA\r\n";
cmd += "From: C++MAILER <cppmailer@o2.pl>\r\n";
cmd += "To: "+to_name+" <"+to_mail+">\r\n";
cmd += "Subject: "+subject+"\r\n\r\n";
cmd += body+"\r\n";
cmd += ".\r\n";
str = connectionCmd(cmd);
cout<<">> "<<str<<endl;

str = connectionCmd("QUIT");
cout<<">> "<<str<<endl;

connectionClose();
return true;
}
};
string prompt(bool multi)
{
string all;
string line;
do
{
getline(cin, line);
//cout<<"["<<line<<"]";
all+="\r\n"+string(line);
}
while(strcmp(line.c_str(), "") and multi);
if(multi)
all = all.substr(0, all.length()-2);
return all.substr(2);
}
string prompt()
{
return prompt(false);
}
int main()
{
cppMailer mailer;
mailer.setServer("poczta.o2.pl", 587);
http://mailer.setServer("192.168.1.101", 80);
mailer.setUser("cppmailer", "doooooopa");

string imie, mail, subject, body, line;
cout<<"Tresc: ";
body = prompt(true);
cout<<"Podaj imie: ";
imie = prompt();
cout<<"Podaj mail: ";
mail = prompt();
cout<<"Temat: ";
subject = prompt();
//cout<<"I: "<<imie<<endl;
//cout<<"M: "<<mail<<endl;
//cout<<"T: "<<subject<<endl;
//cout<<"B: "<<body<<endl;
mailer.sendMessage(imie, mail, subject, body);
system("PAUSE");
return 0;


}

kalau udah di compile terus di run aja

tapi kalau saya mending rebuild all

    Waktu sekarang Sat Dec 10, 2016 8:11 am