www.pudn.com > antispam-addin.rar > deliver.cxx
#include "unihead.hxx"
#include "deliver.hxx"
Deliver::Deliver()
{
m_pConmunicate = NULL;
}
Deliver::~Deliver()
{
if(m_pConmunicate)
{
// m_pConmunicate->Close();
delete m_pConmunicate;
}
}
Deliver::response_code Deliver::response_table[] =
{
// HELO
{ "220", "HELO - SMTP server not available" },
// MAIL
{ "250", "MAIL - SMTP server error"},
// RCPT
{ "250", "RCPT - RCPT error" },
// DATA
{ "250", "DATA - SMTP server not ready for data" },
// DATA_SUCCESS
{ "354", "DATA_SUCCESS - DATA finishing error" },
// DENY_RELAY
{ "556", "DENY_RELAY - Relay access denied" },
// QUIT
{ "221", "QUIT - SMTP server didn't terminate session" },
};
bool Deliver::SendMail(char *pszMail,int nMailLen)
{
if(!m_pConmunicate)
return false;
sprintf(szCommand,"RCPT TO:%s\r\n","yangyunhua@capinfo.com.cn");
//sprintf(szCommand,"RCPT TO:%s\r\n","nier776@21cn.com");
//sprintf(szCommand,"RCPT TO:%s\r\n","yhyang@bjca.org.cn");
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
m_pszError = "sending RCPT failed";
return false;
}
if(!IsResponseOK(RCPT))//have error
{
return false;
}
sprintf(szCommand,"DATA\r\n");
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
m_pszError = "sending DATA failed";
return false;
}
if(!IsResponseOK(DATA))
{
return false;
}
if(!m_pConmunicate->Send(pszMail,nMailLen))
{
m_pszError = "sending mail failed";
return false;
}
sprintf(szCommand,"\r\n.\r\n");
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
m_pszError = "sending mail failed";
return false;
}
if(!IsResponseOK(DATA_SUCCESS))
{
return false;
}
return true;
}
bool Deliver::IsResponseOK(int nRequestType)
{
bool bRet = true;
int nResultLen = RESPONSE_LEN;
response_code* pResp;
char ResponseCode[5];
memset(ResponseCode,0,5);
memset(m_Result,0,RESPONSE_LEN);
printf("--------%d--------\r\n",nRequestType);
if(!m_pConmunicate->Receive(m_Result,nResultLen))
{
m_pszError = "Receiving failed";
return false;
}
printf(m_Result);
memcpy(ResponseCode,m_Result,3);
printf("-----------------\r\n\r\n");
if(nRequestType == RCPT || nRequestType == DATA)
{//Althourgh sending data is successful,relay access is denied.
pResp = &response_table[ DENY_RELAY ];
if(strcmp(ResponseCode,pResp->sResponse) == 0)
{
m_pszError = pResp->sMessage;
return false;
}
}
pResp = &response_table[ nRequestType ];
if(strcmp(ResponseCode,pResp->sResponse) != 0)
{
m_pszError = pResp->sMessage;
return false;
}
return true;
}
bool Deliver::Login(Configure *pConfigure)
{
Conmunicate::StartAccess();
m_pConmunicate = new Conmunicate();
if(!m_pConmunicate)
{
return false;
}
if(!m_pConmunicate->Create() || !m_pConmunicate->Connect(pConfigure->getSmtpHost(), pConfigure->getSmtpPort()))
{
m_pConmunicate->Close();
delete m_pConmunicate;
m_pConmunicate = NULL;
m_pszError = "connecting failed";
return false;
}
sprintf(szCommand,"HELO %s\r\n",pConfigure->getSmtpHost());
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
m_pszError = "sending HELO failed";
return false;
}
printf(szCommand);
if(!IsResponseOK(HELO))
{
return false;
}
sprintf(szCommand,"MAIL FROM:%s\r\n",pConfigure->getUser());
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
m_pszError = "sending MAIL FROM failed";
return false;
}
printf(szCommand);
if(!IsResponseOK(MAIL))
{
return false;
}
return true;
}
bool Deliver::Logoff()
{
sprintf(szCommand,"QUIT\r\n");
if(!m_pConmunicate->Send(szCommand,strlen(szCommand)))
{
return false;
}
if(!IsResponseOK(QUIT))
{
return false;
}
return true;
}