📄 sendmail1.cpp
字号:
// SendMail1.cpp: implementation of the CSendMail class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SendMail1.h"
#include "send.h"
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSendMail::CSendMail()
{
bMime=FALSE;
bRcpt=FALSE;
nComplete=0;
Data=NULL;
Subject=NULL;
szFiles=NULL;
strcpy(&(ErrorStr[0][0]),"No error occur.");
strcpy(&(ErrorStr[1][0]),"Initlize Socket Error.");
strcpy(&(ErrorStr[2][0]),"Can not get host or DNS Error.");
strcpy(&(ErrorStr[3][0]),"Socket alloc Error.");
strcpy(&(ErrorStr[4][0]),"Can not connect the host.");
strcpy(&(ErrorStr[5][0]),"Can not send the data,Network Error.");
strcpy(&(ErrorStr[6][0]),"Can not receive the data,Network Error.");
strcpy(&(ErrorStr[7][0]),"SMTP server response Error.");
strcpy(&(ErrorStr[8][0]),"Need the further information.");
strcpy(&(ErrorStr[9][0]),"No enough memory to use.");
strcpy(&(ErrorStr[10][0]),"OPen attachment fail or Not found.");
strcpy(&(ErrorStr[11][0]),"Unknown Error occur.");
}
CSendMail::~CSendMail()
{
if(Data)
delete []Data;
if(Subject)
delete []Subject;
if(bMime)
if(szFiles)
delete []szFiles;
}
char* CSendMail::GetLastError(int ErrorCode)
{
if(ErrorCode>11)
ErrorCode=11;
return &(ErrorStr[ErrorCode][0]);
}
void CSendMail::SetSMTP(char *szSMTP)
{
if(szSMTP==NULL||*szSMTP=='\0')
return;
nComplete++;//incount the needed informations.
strcpy(Smtp,szSMTP);
}
void CSendMail::SetHello()
{
nComplete++;//incount the needed informations.
strcpy(Hello,"EHLO <");
WORD ver=MAKEWORD(2,0);
WSADATA wsaData;
int ERet;
char *buf=new char[32];
ERet=::WSAStartup(ver,&wsaData);
if(ERet==0)
{
ERet=gethostname(buf,32);
if(ERet==0)
strcat(Hello,buf);
else
strcat(Hello,"Fang");
::WSACleanup();
}
sprintf(buf,">\r\n");
strcat(Hello,buf);
delete []buf;
}
void CSendMail::SetFrom(char *szFrom)
{
if(szFrom==NULL||*szFrom=='\0')
return;
nComplete++;//incount the needed informations.
strcpy(From,"MAIL FROM:<");
strcat(From,szFrom);
char *buf=new char[4];
sprintf(buf,">\r\n");
strcat(From,buf);
delete []buf;
}
void CSendMail::SetTo(char *szTo)
{
if(szTo==NULL||*szTo=='\0')
return;
nComplete++;//incount the needed informations.
strcpy(To,"RCPT TO:<");
strcat(To,szTo);
char *buf=new char[4];
sprintf(buf,">\r\n");
strcat(To,buf);
delete []buf;
}
void CSendMail::SetRCPT(RCPT *rcpt)
{
if(rcpt==NULL)
return;
char *RcptArray;
RcptArray=new char[(rcpt->Count)*64];
bRcpt=TRUE;
Rcpt.Count=rcpt->Count;
Rcpt.Length=64;
Rcpt.szRcpt=RcptArray;
char *buf=new char[4];
sprintf(buf,">\r\n");
for(int i=0;i<(rcpt->Count);i++)
{
strcpy(&(RcptArray[i*Rcpt.Length]),"RCPT TO:<");
strcat(&(RcptArray[i*Rcpt.Length]),&(rcpt->szRcpt[i*(rcpt->Length)]));
strcat(&(RcptArray[i*Rcpt.Length]),buf);
}
delete []buf;
}
int CSendMail::DataNormal(char *szSubject,char *szBuf, size_t nLength)
{
Subject=new char[strlen(szSubject)+12];
if(Subject==NULL)
return 9;
sprintf(Subject,"Subject:");
strcat(Subject,szSubject);
strcat(Subject,"\r\n");
Data=new char[nLength+1];
if(Data==NULL)
return 9;
strncpy(Data,szBuf,nLength);
Length=nLength;
return 0;
}
int CSendMail::DataMIME(char *szSubject,char *szBuf, size_t nLength,RCPT *rcpt)
{
if(rcpt==NULL)
return DataNormal(szSubject,szBuf,nLength);
bMime=TRUE;
memcpy(&file,rcpt,sizeof(RCPT));
int len=file.Count*(file.Length+1);
szFiles=new char[len];
if(szFiles==NULL)
return 9;
memcpy(szFiles,file.szRcpt,len);
file.szRcpt=szFiles;
Subject=new char[strlen(szSubject)+12];
if(Subject==NULL)
return 9;
sprintf(Subject,"Subject:");
strcat(Subject,szSubject);
strcat(Subject,"\r\n");
Data=new char[nLength+1];
if(Data==NULL)
return 9;
strncpy(Data,szBuf,nLength);
Length=nLength;
return 0;
}
int CSendMail::SendMail()
{
if(nComplete<4)
return 8;//Need the further information.
int ERet;
CSend sd;
//initlize the socket .
ERet=sd.Startup();
if(ERet!=0)
return ERet;
//create a socket.
ERet=sd.CreateSocket();
if(ERet!=0)
return ERet;
//connect the smtp server.
ERet=sd.Connect(Smtp);
if(ERet!=0)
return ERet;
//send "HELO" to the server.
ERet=sd.SayHello(Hello);
if(ERet!=0)
return ERet;
//send "MAIL From:" to the server.
ERet=sd.SayMail(From);
if(ERet!=0)
return ERet;
//send "RCPT TO:" to the server.
ERet=sd.SayTo(To);
if(ERet!=0)
return ERet;
//send many "RCPT TO:" to the server.
if(bRcpt)
for(int i=0;i<Rcpt.Count;i++)
{
ERet=sd.SayTo(&(Rcpt.szRcpt[i*Rcpt.Length]));
if(ERet!=0)
return ERet;
}
//send DATA
if(!bMime)
{
ERet=sd.SayDataNormal(Subject,Data,Length);
if(ERet!=0)
return ERet;
}
else//with MIME encoded.
{
ERet=sd.SayDataMIME(Subject,Data,Length,&file);
if(ERet!=0)
return ERet;
}
//send QUIT
ERet=sd.SayQuit();
if(ERet!=0)
return ERet;
return 0;
}
int CSendMail::Initlize(char *szSmtp, char *szFrom)
{
char *tempbuf=new char[64];
char *programpath=new char[256];//程序目录
memset(programpath,0,256);
::GetModuleFileName(NULL,programpath,256);
int i=strlen(programpath);
for(int k=i;(programpath[k]!='\\')&&(k>0);k--);
programpath[k+1]='\0';
strcat(programpath,"SendMail.ini");
//Set SMTP
if((szSmtp==NULL)||(szSmtp[0]=='\0'))
{
//read the rigestry database.
::GetPrivateProfileString("SMTP Server","Server Name",NULL,
tempbuf,63,programpath);
}
else
strcpy(tempbuf,szSmtp);
if(tempbuf[0]!='\0')
SetSMTP(tempbuf);
//Set From
if((szFrom==NULL)||(szFrom[0]=='\0'))
{
//read the rigestry database.
::GetPrivateProfileString("MAIL FROM","Email Address",NULL,
tempbuf,63,programpath);
}
else
strcpy(tempbuf,szFrom);
if(tempbuf[0]!='\0')
SetFrom(tempbuf);
//Set Hello
SetHello();
delete []programpath;
delete []tempbuf;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -