#ifndef __SMTP_H__ //避免重复包含
#define __SMTP_H__
#include <list>
#include <WinSock2.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
const int MAXLEN = 1024;
const int MAX_FILE_LEN = 6000;
static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
struct FILEINFO /*用来记录文件的一些信息*/
{
char fileName[128]; /*文件名称*/
char filePath[256]; /*文件绝对路径*/
};
class CSmtp
{
public:
CSmtp(void);
CSmtp(
int port,
std::string srvDomain, //smtp服务器域名
std::string userName, //用户名
std::string password, //密码
std::string targetEmail, //目的邮件地址
std::string emailTitle, //主题
std::string content //内容
);
public:
~CSmtp(void);
public:
int port;
public:
std::string domain;
std::string user;
std::string pass;
std::string targetAddr;
std::string title;
std::string content;
/*为了方便添加文件,删除文件神马的,使用list容器最为方便,相信大家在数据结构里面都学过*/
std::list <FILEINFO *> listFile;
public:
char buff[MAXLEN + 1];
int buffLen;
SOCKET sockClient; //客户端的套接字
public:
bool CreateConn(); /*创建连接*/
bool Send(std::string &message);
bool Recv();
void FormatEmailHead(std::string &email);//格式化要发送的邮件头部
int Login();
bool SendEmailHead(); //发送邮件头部信息
bool SendTextBody(); //发送文本信息
//bool SendAttachment(); //发送附件
int SendAttachment_Ex();
bool SendEnd();
public:
void AddAttachment(std::string &filePath); //添加附件
void DeleteAttachment(std::string &filePath); //删除附件
void DeleteAllAttachment(); //删除所有的附件
void SetSrvDomain(std::string &domain);
void SetUserName(std::string &user);
void SetPass(std::string &pass);
void SetTargetEmail(std::string &targetAddr);
void SetEmailTitle(std::string &title);
void SetContent(std::string &content);
void SetPort(int port);
int SendEmail_Ex();
/*关于错误码的说明:1.网络错误导致的错误2.用户名错误3.密码错误4.文件不存在0.成功*/
char* base64Encode(char const* origSigned, unsigned origLength);
};
#endif // !__SMTP_H__
smtp.cpp#include "StdAfx.h"
#include "smtp.h"
#pragma comment(lib, "ws2_32.lib") /*链接ws2_32.lib动态链接库*/
char* CSmtp::base64Encode(char const* origSigned, unsigned origLength)
{
unsigned char const* orig = (unsigned char const*)origSigned;
if (orig == NULL) return NULL;
unsigned const numOrig24BitValues = origLength / 3;
bool havePadding = origLength > numOrig24BitValues * 3;
bool havePadding2 = origLength == numOrig24BitValues * 3 + 2;
unsigned const numResultBytes = 4 * (numOrig24BitValues + havePadding);
char* result = new char[numResultBytes + 3];
unsigned i;
for (i = 0; i < numOrig24BitValues; ++i)
{
result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];
result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
result[4 * i + 2] = base64Char[((orig[3 * i + 1] << 2) | (orig[3 * i + 2] >> 6)) & 0x3F];
result[4 * i + 3] = base64Char[orig[3 * i + 2] & 0x3F];
}
if (havePadding)
{
result[4 * i + 0] = base64Char[(orig[3 * i] >> 2) & 0x3F];
if (havePadding2)
{
result[4 * i + 1] = base64Char[(((orig[3 * i] & 0x3) << 4) | (orig[3 * i + 1] >> 4)) & 0x3F];
result[4 * i + 2] = base64Char[(orig[3 * i + 1] << 2) & 0x3F];
}
else
{
result[4 * i + 1] = base64Char[((orig[3 * i] & 0x3) << 4) & 0x3F];
result[4 * i + 2] = '=';
}
result[4 * i + 3] = '=';
}
result[numResultBytes] = '\0';
return result;
}
CSmtp::CSmtp(void)
{
this->content = "";
this->port = 25;
this->user = "";
this->pass = "";
this->targetAddr = "";
this->title = "";
this->domain = "";
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 1);
err = WSAStartup(wVersionRequested, &wsaData);
this->sockClient = 0;
}
CSmtp::~CSmtp(void)
{
DeleteAllAttachment();
closesocket(sockClient);
WSACleanup();
}
CSmtp::CSmtp(
int port,
std::string srvDomain,
std::string userName,
std::string password,
std::string targetEmail,
std::string emailTitle,
std::string content
)
{
this->content = content;
this->port = port;
this->user = userName;
this->pass = password;
this->targetAddr = targetEmail;
this->title = emailTitle;
this->domain = srvDomain;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 1);
err = WSAStartup(wVersionRequested, &wsaData);
this->sockClient = 0;
}
bool CSmtp::CreateConn()
{
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
HOSTENT* pHostent;
pHostent = gethostbyname(domain.c_str());
addrSrv.sin_addr.S_un.S_addr = *((DWORD *)pHostent->h_addr_list[0]);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(port);
int err = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (err != 0)
{
return false;
}
this->sockClient = sockClient;
if (false == Recv())
{
return false;
}
return true;
}
bool CSmtp::Send(std::string &message)
{
int err = send(sockClient, message.c_str(), message.length(), 0);
if (err == SOCKET_ERROR)
{
return false;
}
std::string message01;
return true;
}
bool CSmtp::Recv()
{
memset(buff, 0, sizeof(char)* (MAXLEN + 1));
int err = recv(sockClient, buff, MAXLEN, 0);
if (err == SOCKET_ERROR)
{
return false;
}
buff[err] = '\0';
return true;
}
int CSmtp::Login()
{
std::string sendBuff;
sendBuff = "EHLO ";
sendBuff += user;
sendBuff += "\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
sendBuff = "AUTH LOGIN\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
int pos = user.find('@', 0);
sendBuff = user.substr(0, pos);
char *ecode;
ecode = base64Encode(sendBuff.c_str(), strlen(sendBuff.c_str()));
sendBuff.empty();
sendBuff = ecode;
sendBuff += "\r\n";
delete[]ecode;
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
sendBuff.empty();
ecode = base64Encode(pass.c_str(), strlen(pass.c_str()));
sendBuff = ecode;
sendBuff += "\r\n";
delete[]ecode;
if (false == Send(sendBuff) || false == Recv())
{
return 1;
}
if (NULL != strstr(buff, "550"))
{
return 2;
}
if (NULL != strstr(buff, "535"))
{
return 3;
}
return 0;
}
bool CSmtp::SendEmailHead()
{
std::string sendBuff;
sendBuff = "MAIL FROM: <" + user + ">\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
std::istringstream is(targetAddr);
std::string tmpadd;
while(is>>tmpadd)
{
sendBuff.empty();
sendBuff = "RCPT TO: <" + tmpadd + ">\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
}
sendBuff.empty();
sendBuff = "DATA\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
sendBuff.empty();
FormatEmailHead(sendBuff);
if (false == Send(sendBuff))
{
return false;
}
return true;
}
void CSmtp::FormatEmailHead(std::string &email)
{
email = "From: ";
email += user;
email += "\r\n";
email += "To: ";
email += targetAddr;
email += "\r\n";
email += "Subject: ";
email += title;
email += "\r\n";
email += "MIME-Version: 1.0";
email += "\r\n";
email += "Content-Type: multipart/mixed;boundary=qwertyuiop";
email += "\r\n";
email += "\r\n";
}
bool CSmtp::SendTextBody()
{
std::string sendBuff;
sendBuff = "--qwertyuiop\r\n";
sendBuff += "Content-Type: text/plain;";
sendBuff += "charset=\"gb2312\"\r\n\r\n";
sendBuff += content;
sendBuff += "\r\n\r\n";
return Send(sendBuff);
}
int CSmtp::SendAttachment_Ex()
{
for (std::list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end(); pIter++)
{
std::string sendBuff;
sendBuff = "--qwertyuiop\r\n";
sendBuff += "Content-Type: application/octet-stream;\r\n";
sendBuff += " name=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "Content-Transfer-Encoding: base64\r\n";
sendBuff += "Content-Disposition: attachment;\r\n";
sendBuff += " filename=\"";
sendBuff += (*pIter)->fileName;
sendBuff += "\"";
sendBuff += "\r\n";
sendBuff += "\r\n";
Send(sendBuff);
std::ifstream ifs((*pIter)->filePath, std::ios::in | std::ios::binary);
if (false == ifs.is_open())
{
return 4;
}
char fileBuff[MAX_FILE_LEN];
char *chSendBuff;
memset(fileBuff, 0, sizeof(fileBuff));
while (ifs.read(fileBuff, MAX_FILE_LEN))
{
chSendBuff = base64Encode(fileBuff, MAX_FILE_LEN);
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
send(sockClient, chSendBuff, strlen(chSendBuff), 0);
delete[]chSendBuff;
}
chSendBuff = base64Encode(fileBuff, ifs.gcount());
chSendBuff[strlen(chSendBuff)] = '\r';
chSendBuff[strlen(chSendBuff)] = '\n';
int err = send(sockClient, chSendBuff, strlen(chSendBuff), 0);
if (err != strlen(chSendBuff))
{
return 1;
}
delete[]chSendBuff;
}
return 0;
}
bool CSmtp::SendEnd()
{
std::string sendBuff;
sendBuff = "--qwertyuiop--";
sendBuff += "\r\n.\r\n";
if (false == Send(sendBuff) || false == Recv())
{
return false;
}
sendBuff.empty();
sendBuff = "QUIT\r\n";
return (Send(sendBuff) && Recv());
}
int CSmtp::SendEmail_Ex()
{
if (false == CreateConn())
{
return 1;
}
int err = Login();
if (err != 0)
{
return err;
}
if (false == SendEmailHead())
{
return 1;
}
if (false == SendTextBody())
{
return 1;
}
err = SendAttachment_Ex();
if (err != 0)
{
return err;
}
if (false == SendEnd())
{
return 1;
}
return 0;
}
void CSmtp::AddAttachment(std::string &filePath)
{
FILEINFO *pFile = new FILEINFO;
strcpy_s(pFile->filePath, filePath.c_str());
const char *p = filePath.c_str();
strcpy_s(pFile->fileName, p + filePath.find_last_of("\\") + 1);
listFile.push_back(pFile);
}
void CSmtp::DeleteAttachment(std::string &filePath)
{
std::list<FILEINFO *>::iterator pIter;
for (pIter = listFile.begin(); pIter != listFile.end(); pIter++)
{
if (strcmp((*pIter)->filePath, filePath.c_str()) == 0)
{
FILEINFO *p = *pIter;
listFile.remove(*pIter);
delete p;
break;
}
}
}
void CSmtp::DeleteAllAttachment()
{
for (std::list<FILEINFO *>::iterator pIter = listFile.begin(); pIter != listFile.end();)
{
FILEINFO *p = *pIter;
pIter = listFile.erase(pIter);
delete p;
}
}
void CSmtp::SetSrvDomain(std::string &domain)
{
this->domain = domain;
}
void CSmtp::SetUserName(std::string &user)
{
this->user = user;
}
void CSmtp::SetPass(std::string &pass)
{
this->pass = pass;
}
void CSmtp::SetTargetEmail(std::string &targetAddr)
{
this->targetAddr = targetAddr;
}
void CSmtp::SetEmailTitle(std::string &title)
{
this->title = title;
}
void CSmtp::SetContent(std::string &content)
{
this->content = content;
}
void CSmtp::SetPort(int port)
{
this->port = port;
}
测试程序
#include "Smtp.h"
#include <iostream>
using namespace std;
int main()
{
CSmtp smtp(
25,
"smtp.163.com",
"yourname@163.com",
"yourpasswd",
"target1@163.com target2@163.com",
"消息",
"消息提示"
);
/**
//添加附件时注意,\一定要写成\\,因为转义字符的缘故
string filePath("D:\\课程设计报告.doc");
smtp.AddAttachment(filePath);
*/
int err;
if ((err = smtp.SendEmail_Ex()) != 0)
{
if (err == 1)
cout << "错误1: 由于网络不畅通,发送失败!" << endl;
if (err == 2)
cout << "错误2: 用户名错误,请核对!" << endl;
if (err == 3)
cout << "错误3: 用户密码错误,请核对!" << endl;
if (err == 4)
cout << "错误4: 请检查附件目录是否正确,以及文件是否存在!" << endl;
}
system("pause");
return 0;
}