C++ realizes the function of sending emails and attachments
- 2020-06-07 04:54:48
- OfStack
This article shares the specific code of C++ to send emails and attachments for your reference. The specific content is as follows
The header file
/*************************
* Send mail module header file
* Text and attachments can be sent (multiple attachments are supported) 1 Send)
**************************/
#pragma once
struct sMailInfo // Email messages
{
char* m_pcUserName;// The name of the user's login mailbox
char* m_pcUserPassWord;// The password of the user's login mailbox
char* m_pcSenderName;// The name displayed when the user sends it
char* m_pcSender;// Email address of the sender
char* m_pcReceiver;// The recipient's email address
char* m_pcTitle;// Your email title
char* m_pcBody;// Email text body
char* m_pcIPAddr;// The server's IP
char* m_pcIPName;// Server name ( IP And the name 2 choose 1 , preferred name)
sMailInfo(){memset(this,0,sizeof(sMailInfo));}
};
class CSendMail
{
public:
CSendMail(void);
~CSendMail(void);
public:
bool SendMail(sMailInfo &smailInfo);// To send a message, you need to initialize the message message at the time of sending
void AddFilePath(char * pcFilePath);// Add the attachment's decision path to the attachment list
void DeleteFilePath(char* pcFilePath);// Delete the attachment path, if any
void DeleteAllPath(void);// Delete the path of all attachments
protected:
void GetFileName(char* fileName,char* filePath);// Gets the file name from the path of the attachment
void Char2Base64(char* pBuff64,char* pSrcBuff,int iLen);// the char Type into Base64 type
bool CReateSocket(SOCKET &sock);// To establish socket The connection
bool Logon(SOCKET &sock);// Login mailbox, mainly for the preparation of email
int GetFileData(char* FilePath);// Gets the attachment content from the file path
bool SendHead(SOCKET &sock);// Sending header
bool SendTextBody(SOCKET &sock);// Send the message text body
bool SendFileBody(SOCKET &sock);// Send email attachments
bool SendEnd(SOCKET &sock);// End of send message
protected:
CList<char*,char*> m_pcFilePathList;// Record attachment path
char m_cSendBuff[4096];// Send buffer
char m_cReceiveBuff[1024];
char* m_pcFileBuff;// Point to attachment content
sMailInfo m_sMailInfo;
};
Module implementation file
/****************************
* Send mail module
* Text and attachments can be sent (multiple attachments are supported) 1 Send)
*Date:2011-12-01
******************************/
#include "StdAfx.h"
#include "SendMail.h"
#include "winsock2.h"
#pragma comment(lib,"WSOCK32")
CSendMail::CSendMail(void)
{
m_pcFileBuff=NULL;
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
memset(m_cReceiveBuff,0,sizeof(m_cReceiveBuff));
}
CSendMail::~CSendMail(void)
{
DeleteAllPath();
}
void CSendMail::Char2Base64(char* pBuff64,char* pSrcBuff,int iLen)
{
//1 1 1 1 1 1 1 1
// Assigned to pBuff64 write Assigned to pBuff64+1
// point Where we are
static char Base64Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//base64 The character table to map
int point;// every 1 Source character split position, desirable 2,4,6 ; For the initial 2
point=2;
int i;
int iIndex;//base64 Index of characters
char n=0;// on 1 The residual value of the source character
for(i=0;i<iLen;i++)
{
if(point==2)
{
iIndex=((*pSrcBuff)>>point)&0x3f;// achieve pSrcBuff The high point position
}
else if (point==4)
{
iIndex=((*pSrcBuff)>>point)&0xf;// achieve pSrcBuff The high point position
}
else if(point==6)
{
iIndex=((*pSrcBuff)>>point)&0x3;// achieve pSrcBuff The high point position
}
iIndex+=n;// with pSrcBuff-1 The low point Combination of Base64 The index of the
*pBuff64++=Base64Encode[iIndex];// From the index table pBuff64
n=((*pSrcBuff)<<(6-point));// Calculates residual values in source characters
n=n&0x3f;// Make sure that n The top two are 0
point+=2;// The split position of the source character goes up 2
if(point==8)// If the split position is 8 instructions pSrcBuff There are 6 Bit residue can be composed 1 A complete Base64 Character, so recombine directly 1 time
{
iIndex=(*pSrcBuff)&0x3f;// Extraction of low 6 Bits, that's the index
*pBuff64++=Base64Encode[iIndex];//
n=0;// Residual value is 0
point=2;// The split position is set to 2
}
pSrcBuff++;
}
if(n!=0)
{
*pBuff64++=Base64Encode[n];
}
if(iLen%3==2)// If the source string length is not 3 I'm going to use multiples of theta '=' completion
{
*pBuff64='=';
}
else if(iLen%3==1)
{
*pBuff64++='=';
*pBuff64='=';
}
}
void CSendMail::AddFilePath(char * pcFilePath)// Add attachment path
{
if(pcFilePath==NULL)
{
return;
}
int i;
char* temp;
for(i=0;i<m_pcFilePathList.GetCount();i++)
{
temp=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i));
if(strcmp(pcFilePath,temp)==0)// If it already exists, you don't have to add it
{
return;
}
}
m_pcFilePathList.AddTail(pcFilePath);
}
void CSendMail::DeleteFilePath(char* pcFilePath)// Delete attachment path
{
int i;
char* temp;
for(i=0;i<m_pcFilePathList.GetCount();i++)
{
temp=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i));
if(strcmp(temp,pcFilePath)==0)// Find it and delete it. If you don't find it, forget about it
{
m_pcFilePathList.RemoveAt(m_pcFilePathList.FindIndex(i));
delete[] temp;
return;
}
}
}
void CSendMail::DeleteAllPath(void)
{
m_pcFilePathList.RemoveAll();
}
int CSendMail::GetFileData(char* FilePath)
{
m_pcFileBuff=NULL;
if(FilePath==NULL)
{
return 0;
}
CFile f;
int len;
USES_CONVERSION;
if(!f.Open(A2W(FilePath),CFile::modeRead|CFile::modeNoTruncate|CFile::typeBinary))
{
return 0;
}
len=(int)f.GetLength();
m_pcFileBuff=new char[len+1];
memset(m_pcFileBuff,0,len+1);
f.Read(m_pcFileBuff,len);
f.Close();
return len;
}
void CSendMail::GetFileName(char* fileName,char* filePath)
{
if(filePath==NULL || fileName==NULL)
{
return;
}
int i;
for(i=0;i<(int)strlen(filePath);i++)
{
if(filePath[strlen(filePath)-1-i]=='\\')
{
memcpy(fileName,&filePath[strlen(filePath)-i],i);
return;
}
}
}
bool CSendMail::CReateSocket(SOCKET &sock)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return false;
}
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if (sock == INVALID_SOCKET)
{
return false;
}
sockaddr_in servaddr;
memset(&servaddr,0,sizeof(sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(25);// email 1 As is 25 port
if(m_sMailInfo.m_pcIPName=="")
{
servaddr.sin_addr.s_addr = inet_addr(m_sMailInfo.m_pcIPAddr);// Direct use of IP address
}
else
{
struct hostent *hp=gethostbyname(m_sMailInfo.m_pcIPName);// Use the name
servaddr.sin_addr.s_addr=*(int*)(*hp->h_addr_list);
}
int ret = connect(sock,(sockaddr*)&servaddr,sizeof(servaddr));// Establish a connection
if (ret == SOCKET_ERROR)
{
return false;
}
return true;
}
bool CSendMail::Logon(SOCKET &sock)
{
recv(sock,m_cReceiveBuff,1024,0);
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"HELO []\r\n");
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);// Start session
recv(sock,m_cReceiveBuff,1024,0);
if(m_cReceiveBuff[0]!='2' || m_cReceiveBuff[1]!='5' || m_cReceiveBuff[2]!='0')
{
return false;
}
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"AUTH LOGIN\r\n");
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);// Login request
recv(sock,m_cReceiveBuff,1024,0);
if(m_cReceiveBuff[0]!='3' || m_cReceiveBuff[1]!='3' || m_cReceiveBuff[2]!='4')
{
return false;
}
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
Char2Base64(m_cSendBuff,m_sMailInfo.m_pcUserName,strlen(m_sMailInfo.m_pcUserName));
m_cSendBuff[strlen(m_cSendBuff)]='\r';
m_cSendBuff[strlen(m_cSendBuff)]='\n';
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);// Send user name
recv(sock,m_cReceiveBuff,1024,0);
if(m_cReceiveBuff[0]!='3' || m_cReceiveBuff[1]!='3' || m_cReceiveBuff[2]!='4')
{
return false;
}
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
Char2Base64(m_cSendBuff,m_sMailInfo.m_pcUserPassWord,strlen(m_sMailInfo.m_pcUserPassWord));
m_cSendBuff[strlen(m_cSendBuff)]='\r';
m_cSendBuff[strlen(m_cSendBuff)]='\n';
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);// Send user password
recv(sock,m_cReceiveBuff,1024,0);
if(m_cReceiveBuff[0]!='2' || m_cReceiveBuff[1]!='3' || m_cReceiveBuff[2]!='5')
{
return false;
}
return true;// Login successful
}
bool CSendMail::SendHead(SOCKET &sock)
{
int rt;
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"MAIL FROM:<%s>\r\n",m_sMailInfo.m_pcSender);
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
recv(sock,m_cReceiveBuff,1024,0);
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"RCPT TO:<%s>\r\n",m_sMailInfo.m_pcReceiver);
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
recv(sock,m_cReceiveBuff,1024,0);
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
memcpy(m_cSendBuff,"DATA\r\n",strlen("DATA\r\n"));
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
recv(sock,m_cReceiveBuff,1024,0);
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"From:\"%s\"<%s>\r\n",m_sMailInfo.m_pcSenderName,m_sMailInfo.m_pcSender);
sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"To:\"INVT.COM.CN\"<%s>\r\n",m_sMailInfo.m_pcReceiver);
sprintf_s(&m_cSendBuff[strlen(m_cSendBuff)],150,"Subject:%s\r\nMime-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"INVT\"\r\n\r\n",m_sMailInfo.m_pcTitle);
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
return true;
}
bool CSendMail::SendTextBody(SOCKET &sock)
{
int rt;
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
sprintf_s(m_cSendBuff,"--INVT\r\nContent-Type: text/plain;\r\n charset=\"gb2312\"\r\n\r\n%s\r\n\r\n",m_sMailInfo.m_pcBody);
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
else
{
return true;
}
}
bool CSendMail::SendFileBody(SOCKET &sock)
{
int i;
char* filePath;
int rt;
int len;
int pt=0;
char fileName[128];
for(i=0;i<m_pcFilePathList.GetCount();i++)
{
pt=0;
memset(fileName,0,128);
filePath=m_pcFilePathList.GetAt(m_pcFilePathList.FindIndex(i));
len=GetFileData(filePath);
GetFileName(fileName,filePath);
sprintf_s(m_cSendBuff,"--INVT\r\nContent-Type: application/octet-stream;\r\n name=\"%s\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment;\r\n filename=\"%s\"\r\n\r\n",fileName,fileName);
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
while (pt<len)
{
memset(m_cSendBuff,0,sizeof(m_cSendBuff));
Char2Base64(m_cSendBuff,&m_pcFileBuff[pt],min(len-pt,3000));
m_cSendBuff[strlen(m_cSendBuff)]='\r';
m_cSendBuff[strlen(m_cSendBuff)]='\n';
rt=send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
pt+=min(len-pt,3000);
if(rt!=strlen(m_cSendBuff))
{
return false;
}
}
if(len!=0)
{
delete [] m_pcFileBuff;
}
}
return true;
}
bool CSendMail::SendEnd(SOCKET &sock)
{
sprintf_s(m_cSendBuff,"--INVT--\r\n.\r\n");
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
sprintf_s(m_cSendBuff,"QUIT\r\n");
send(sock,m_cSendBuff,strlen(m_cSendBuff),0);
closesocket(sock);
WSACleanup();
return true;
}
bool CSendMail::SendMail(sMailInfo &smailInfo)
{
memcpy(&m_sMailInfo,&smailInfo,sizeof(smailInfo));
if(m_sMailInfo.m_pcBody==NULL
|| m_sMailInfo.m_pcIPAddr==NULL
|| m_sMailInfo.m_pcIPName==NULL
|| m_sMailInfo.m_pcReceiver==NULL
|| m_sMailInfo.m_pcSender==NULL
|| m_sMailInfo.m_pcSenderName==NULL
|| m_sMailInfo.m_pcTitle==NULL
|| m_sMailInfo.m_pcUserName==NULL
|| m_sMailInfo.m_pcUserPassWord==NULL)
{
return false;
}
SOCKET sock;
if(!CReateSocket(sock))// Establish a connection
{
return false;
}
if(!Logon(sock))// The login email
{
return false;
}
if(!SendHead(sock))// Sending header
{
return false;
}
if(!SendTextBody(sock))// Send the text portion of the message
{
return false;
}
if(!SendFileBody(sock))// Send attachments
{
return false;
}
if(!SendEnd(sock))// End the message and close it sock
{
return false;
}
return true;
}