?? client.cpp
字號:
// Client.cpp : Defines the entry point for the console application.
//by caucy 2005.12
#include "stdafx.h"
#include "Client.h"
#include <winsock2.h>
#include <afxsock.h>
#include <assert.H>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#include <sys/TIMEB.H>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
#define BUFFER_BLOCK_SIZE 4096
#define DEAL_RETURN_CODE(retCode) { if((retCode)!=0) return retCode;}
#define DEAL_SOCK_ERROR(retCode,sock) if((retCode)==SOCKET_ERROR || (retCode)==0)\
{ \
cout<<"Sock error: "<<GetLastError()<<endl;\
closesocket(sock); \
return -1; \
}
//Auxiliary Functions
int ClientCommandResolve();
void InputLine(string & str);
int GetResponseCode(SOCKET sock,int correctCode,string& str);
BOOL ResponsedCodeFinished(const string& str);
u_long ConvertHostnameToLongHostAddress(const char * destAddress);
BOOL SendMessage(SOCKET sock, const char* buffer, int bufferLen);
//Sub-Functions in Client Command
void EncodingBase64(const char* src, char* des);
void DecodingBase64(const char* src, char* des);
void EncodingBase64Adv(const char* src, int length, char* des);
void DecodingBase64Adv(const char* src, int length, char* des);
UCHAR SixBitDecodeIndex(char a);
BOOL EncodeFileBase64(const string& filename, string& code);
BOOL ReadFileToStr(const string& filename, string& code);
void HeadTextTemple(const string& command,const string& addr,string & buffer, BOOL bPrintCommond=TRUE);
void Date(string& buffer);
void From(const string& addr,string & buffer);
void To(const vector<string >& to,string & buffer);
void Cc(const vector<string >& cc,string & buffer);
void Bcc(const vector<string >& bcc,string & buffer);
void DataHead(const string& from,
const vector<string >& to,
const vector<string >& cc,
const vector<string >& bcc,
const string & subject,
const string& bodytext,
const string& bodytexthtml,
BOOL bHtml,
BOOL bAttachment,
string& majorSplitTag,
string& buffer);
void DataBody( BOOL bHtmlBody,
BOOL bAttachment,
const string& majorSplitTag,
const string& bodytext,
const string& bodytexthtml,
const vector<string> & attachments,
string& buffer);
void DataBody_PureText(const string& bodytext_base64, string& buffer);
void DataBody_TextAndAttachments(const string& bodytext_base64,
const vector<string >& attachments,
const string& majorSplitTag,
string& buffer);
void DataBody_HtmlOnly(const string& bodytext_base64,
const string& html_base64,
const string& majorSplitTag,
string& buffer);
void DataBody_HtmlAndAttachments(const string& bodytext_base64,
const string& html_base64,
const vector<string >& attachments,
const string& majorSplitTag,
const string& subSplitTag,
string& buffer);
//Agent Application Functions
int SendMail(const string& proxyServerName,
UINT proxyServerPort,
const string& proxyUser,
const string& proxyPass,
const string& serverHostName,
UINT serverPort,
const string& username,
const string pass,
const string& senderAddr,
const vector<string >& recptAddress,
const vector<string >& from, //for most mail server, it only support youself address
const vector<string >& to,
const vector<string >& cc,
const vector<string >& bcc,
const string & subject,
const string & bodytext,
const string & bodyhtml,
const vector<string >&attachments);
int Socks5StartIPv4(const string& proxyServerHostName,
u_int proxyServerPort,
const string& username,
const string& pass,
u_long destAddress,
u_int destPort,
SOCKET& sock);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
ClientCommandResolve();
return 0;
}
void InputLine(string & str)
{
char ch;
str="";
while((ch = getchar())!= '\n')
str+=ch;
}
int ClientCommandResolve()
{
string serverHostName;
UINT serverPort=0;
string senderAddr;
vector<string> recptAddrs;
string username;
string pass;
vector<string > from;
vector<string > to;
vector<string > cc;
vector<string > bcc;
string subject;
string bodytext;
string bodytexthtml;
vector<string >attachments;
//proxy
string proxyServerName;
UINT proxyServerPort;
string proxyUser;
string proxyPass;
// serverPort=25;
// username="";
// pass="";
// proxyServerName="";
// proxyServerPort=;
// proxyUser="";
// proxyPass="";
char paraCommand[BUFFER_BLOCK_SIZE];
int pos=0;
int iResult;
string str;
string::size_type startPos;
WSADATA wsaData;
if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)!=0)
{
cerr<<"socket init error"<<endl;
return -1;
}
while(1)
{
//SMTP Server host name
if(serverHostName.length()==0) //for testing
while(1)
{
cout<<"SMTP Server Host Name:";
cout.flush();
InputLine(str);
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
{
cout<<"Input server host name"<<endl;
continue;
}
serverHostName=paraCommand;
break;
}
//SMPT Server Port
if(serverPort==0) //for testing
while(1)
{
cout<<"SMTP Server Port:";
cout.flush();
InputLine(str);
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult!=EOF)
{
serverPort=atoi(paraCommand);
if(serverPort<0)
{
cout<<"Invalid port."<<endl;
continue;
}
}
break;
}
//USER NAME
if(username.length()==0) //for testing
while(1)
{
cout<<"USER NAME:";
cout.flush();
InputLine(str);
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
{
cout<<"Input user name for authentication."<<endl;
continue;
}
username=paraCommand;
break;
}
//PASS
if(pass.length()==0) //for testing
while(1)
{
cout<<"PASSWORD:";
cout.flush();
InputLine(str);
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
{
cout<<"Input password."<<endl;
continue;
}
pass=paraCommand;
break;
}
//Proxy
if(proxyServerName.length()==0) //for testing
{
cout<<"Proxy?";
cout.flush();
InputLine(str);
if(str=="y" || str=="Y")
{
cout<<"Proxy Server Addr:";
InputLine(proxyServerName);
cout<<"Proxy Server Port:";
InputLine(str);
sscanf(str.c_str(),"%d",&proxyServerPort);
cout<<"Proxy Server Username:";
InputLine(proxyUser);
cout<<"Proxy Server Pass:";
InputLine(proxyPass);
}
}
cout.flush();
//MAIL FROM
//Though rfc822 supports muti-senders, most email server today only support authentication login.
//So, the fact is one sender one mail.
if(senderAddr.length()==0) //for testing
while(1)
{
cout<<"MAIL FROM:";
cout.flush();
InputLine(str);
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
{
cout<<"Input sender address."<<endl;
continue;
}
senderAddr=paraCommand;
break;
}
from.push_back(senderAddr);
/*
//RCPT TO
//If having multiple receivers, here using spaces to split them
if(recptAddrs.empty()) //for testing
while(1)
{
cout<<"RCPT TO:";
cout.flush();
InputLine(str);
while(1)
{
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
break;
startPos=str.find(paraCommand,0,strlen(paraCommand));
startPos+=strlen(paraCommand);
str=str.substr(startPos,str.length()-startPos);
recptAddrs.push_back(string(paraCommand));
}
if(recptAddrs.size()==0)
{
cout<<"Input receiver address."<<endl;
continue;
}
break;
}
*/
//to
if(to.empty()) //for testing
{
cout<<"To: ";
cout.flush();
InputLine(str);
while(1)
{
if(str.length()==0)
break;
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
break;
startPos=str.find(paraCommand,0,strlen(paraCommand));
startPos+=strlen(paraCommand);
str=str.substr(startPos,str.length()-startPos);
to.push_back(string(paraCommand));
}
}
//Cc
if(cc.empty()) //for testing
{
cout<<"Cc: ";
cout.flush();
InputLine(str);
while(1)
{
if(str.length()==0)
break;
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
break;
startPos=str.find(paraCommand,0,strlen(paraCommand));
startPos+=strlen(paraCommand);
str=str.substr(startPos,str.length()-startPos);
cc.push_back(string(paraCommand));
}
}
//Bcc
if(cc.empty()) //for testing
{
cout<<"Bcc: ";
cout.flush();
InputLine(str);
while(1)
{
if(str.length()==0)
break;
iResult=sscanf(str.c_str(),"%s",paraCommand);
if(iResult==EOF)
break;
startPos=str.find(paraCommand,0,strlen(paraCommand));
startPos+=strlen(paraCommand);
str=str.substr(startPos,str.length()-startPos);
bcc.push_back(string(paraCommand));
}
}
//Subject
if(subject.length()==0) //for testing
{
cout<<"Subject:"<<endl;
cout.flush();
InputLine(subject);
}
//DATA body: text
if(bodytext.length()==0) //for testing
{
bodytext="";
cout<<"DATA:"<<endl;
cout.flush();
while(1)
{
InputLine(str);
if(str=="_Quit")
break;
bodytext+=str+"\r\n";
}
}
//DATA body: html
//Most email reader-agents support html coded email. So we support it too.
if(bodytexthtml.length()==0) //for testing
{
cout<<"Do you want peer-agent to see a html coded email?Y/N?";
cout.flush();
InputLine(str);
if(str=="Y" || str=="y")
{
cout<<"Input the html file's directory."<<endl;
InputLine(str);
if(str!="")
{
ReadFileToStr(str,bodytexthtml);
}
}
}
//DATA body: attachment
if(attachments.empty()) //for testing
{
cout<<"Do you want to send attachments?Y/N?";
cout.flush();
InputLine(str);
if(str=="Y" || str=="y")
{
cout<<"Input the attachment file's directory."<<endl;
while(1)
{
InputLine(str);
if(str!="")
attachments.push_back(str);
else
break;
}
}
}
cout<<"Sending Mail....."<<endl;
//regroup rectpAddres array
set<string> stringSet;
int i;
for(i=0; i< to.size(); i++)
{
stringSet.insert(to[i]);
}
for(i=0; i< cc.size(); i++)
{
stringSet.insert(cc[i]);
}
for(i=0; i< bcc.size(); i++)
{
stringSet.insert(bcc[i]);
}
for(set<string >::iterator iteSet=stringSet.begin();
iteSet!=stringSet.end(); iteSet++)
{
recptAddrs.push_back(*iteSet);
}
iResult=SendMail(proxyServerName,proxyServerPort,proxyUser,proxyPass,
serverHostName,serverPort,username,pass,
senderAddr,recptAddrs,
from,to,cc, bcc,subject,bodytext,bodytexthtml,attachments);
if(iResult==-1)
{
cout<<"Unsuccessfully sent this mail. Save it to file."<<endl;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -