Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
SMTP.cpp
Package: MailMulti.rar [view]
Upload User: tjsct008
Upload Date: 2016-12-02
Package Size: 3750k
Code Size: 5k
Category:
WEB Mail
Development Platform:
Visual C++
- // SMTP.cpp: implementation of the CSMTP class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "MailMulti.h"
- #include "SMTP.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSMTP::response_code CSMTP::response_table[]=
- {
- //GENERIC_SUCCESS
- {250,"SMTP服务器错误!"},
- //CONNECT_SUCCESS
- {220,"SMTP服务器不可用!"},
- //DATA_SUCCESS
- {354,"SMTP服务器不能接收数据!"},
- //QUIT_SUCCESS
- {221,"SMTP服务器没有终止会话!" }
- };
- CSMTP::CSMTP(LPCSTR szSMTPServerName,UINT nPort /* = SMTP_PORT */)
- {
- ASSERT(szSMTPServerName!=NULL);
- AfxSocketInit();
- m_sSTMPServerHostName = szSMTPServerName;
- m_nPort = nPort;
- m_bConnected = FALSE;
- m_sError = "OK";
- response_buf = NULL;
- }
- CSMTP::~CSMTP()
- {
- DisConnect();
- }
- BOOL CSMTP::Connect()
- {
- CString sHello;
- TCHAR local_host[80];
- if(m_bConnected)
- return TRUE;
- try
- {
- response_buf = new TCHAR[RESPONSE_BUFFER_SIZE];
- if(response_buf == NULL)
- {
- m_sError = "内存不足!";
- return FALSE;
- }
- }
- catch (CException * e)
- {
- response_buf = NULL;
- m_sError = "内存不足!";
- delete e;
- return FALSE;
- }
- if(!m_wsSMTPServer.Create())
- {
- m_sError = "无法创建套接字!";
- delete response_buf;
- response_buf = NULL;
- return FALSE;
- }
- if(!m_wsSMTPServer.Connect(GetServerHostName(),GetPort()))
- {
- m_sError = "无法链接到服务器!";
- delete response_buf;
- response_buf = NULL;
- return FALSE;
- }
- if(!get_response(CONNECT_SUCCESS))
- {
- m_sError = "服务器无反应";
- m_wsSMTPServer.Close();
- delete response_buf;
- response_buf = NULL;
- return FALSE;
- }
- gethostname(local_host,80);
- sHello.Format(_T("HELLO%srn"),local_host);
- m_wsSMTPServer.Send((LPCSTR)sHello,sHello.GetLength());
- if(!get_response(GENERIC_SUCCESS))
- {
- m_wsSMTPServer.Close();
- delete response_buf;
- response_buf = NULL;
- return FALSE;
- }
- m_bConnected = TRUE;
- return TRUE;
- }
- BOOL CSMTP::get_response(UINT response_expect)
- {
- ASSERT(response_expect>=GENERIC_SUCCESS);
- ASSERT(response_expect<LAST_RESPONSE);
- CString sResponse;
- UINT response;
- response_code * pResp;
- if(m_wsSMTPServer.Receive(response_buf,RESPONSE_BUFFER_SIZE==SOCKET_ERROR))
- {
- m_sError = "套接字错误!";
- return FALSE;
- }
- sResponse = response_buf;
- scanf((LPCSTR)sResponse.Left(3),_T("%d"),&response);
- pResp = &response_table[response_expect];
- if(response!=pResp->nResponse)
- {
- m_sError.Format(_T("%d:%s"),response,(LPCSTR)pResp->sMessage);
- return FALSE;
- }
- return TRUE;
- }
- CString CSMTP::GetServerHostName()
- {
- return m_sSTMPServerHostName;
- }
- UINT CSMTP::GetPort()
- {
- return m_nPort;
- }
- BOOL CSMTP::SendMessage(CMailMessage * msg)
- {
- ASSERT(msg!=NULL);
- if(m_bConnected == NULL)
- {
- m_sError = "必须首先创建连接!";
- return FALSE;
- }
- if(FormatMailMessage(msg)==FALSE)
- {
- return FALSE;
- }
- if(transmit_message(msg)==FALSE)
- return FALSE;
- return TRUE;
- }
- BOOL CSMTP::FormatMailMessage(CMailMessage * msg)
- {
- ASSERT(msg!=NULL);
- msg->FormatMessage();
- return TRUE;
- }
- BOOL CSMTP::transmit_message(CMailMessage * msg)
- {
- CString sFrom;
- CString sTo;
- CString sTemp;
- CString sMail;
- ASSERT(msg!=NULL);
- if(m_bConnected==FALSE)
- {
- m_sError = "首先创建连接!";
- return FALSE;
- }
- sFrom.Format(_T("Mail From:<%s>rn"),(LPCTSTR)msg->m_strFrom);
- m_wsSMTPServer.Send((LPCTSTR)sFrom,sFrom.GetLength());
- if(!get_response(GENERIC_SUCCESS))
- return FALSE;
- sMail = (LPCTSTR)msg->m_strTo;
- sTo.Format(_T("RCPT To:<%s>rn"),(LPCTSTR)sMail);
- m_wsSMTPServer.Send((LPCTSTR)sTo,sTo.GetLength());
- get_response(GENERIC_SUCCESS);
- sTemp = "DATArn";
- m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
- if(!get_response(DATA_SUCCESS))
- return FALSE;
- m_wsSMTPServer.Send((LPCTSTR)msg->m_strHead,msg->m_strHead.GetLength());
- sTemp = cook_body(msg);
- m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
- sTemp = "rn.rn";
- m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
- if(!get_response(GENERIC_SUCCESS))
- return FALSE;
- return TRUE;
- }
- CString CSMTP::cook_body(CMailMessage * msg)
- {
- ASSERT(msg!=NULL);
- CString sTemp;
- CString sCooked = "";
- LPCTSTR szBad = "rn.rn";
- LPCTSTR szGood = "rn.rn";
- int nPos;
- int nStart =0;
- int nBadlength = strlen(szBad);
- sTemp = msg->m_strBody;
- if(sTemp.Left(3)==".rn")
- sTemp = "."+sTemp;
- while((nPos=sTemp.Find(szBad))>-1)
- {
- sCooked = sTemp.Mid(nStart,nPos);
- sCooked+=szGood;
- sTemp = sCooked+
- sTemp.Right(sTemp.GetLength()-(nPos+nBadlength));
- }
- return sTemp;
- }
- BOOL CSMTP::DisConnect()
- {
- BOOL ret;
- if(!m_bConnected)
- return TRUE;
- CString sQuit = "QUITrn";
- m_wsSMTPServer.Send((LPCTSTR)sQuit,sQuit.GetLength());
- ret = get_response(QUIT_SUCCESS);
- m_wsSMTPServer.Close();
- if(response_buf!=NULL)
- {
- delete []response_buf;
- response_buf = NULL;
- }
- m_bConnected = FALSE;
- return ret;
- }
- CString CSMTP::GetLastError()
- {
- return m_sError;
- }