前言
本人第一次写博客,主要目的是想记录一下自己的学习结果,本篇博客内容纯手打,参考资料来自于互联网,由于参考资料过多,就不一一列举参考来源,若是原作者认为侵犯了您的权益,请及时与我联系。
项目简介
该项目是利用C/C++语言编写的基于MIME1.0协议的发送邮件的一个小程序,可以同时发送给多个收件人,可以添加多个附件。我写这个小程序的目的是想自己做一个验证码系统,用于自己其它项目的验证服务。
项目开发环境
操作系统: Ubuntu18.04 (内核:4.15.0-134-generic)
编程语言:C/C++ (C++11)
编译器: g++7.5.0
开发工具:vscode,cmake
vscode的工程配置以及cmakelists.txt由于不是本篇博客的重点,就略去,有需要的可以私信或者我改天上传到git上(emmm,这项目太小了,不想上传)。
项目代码
项目代码我是从一篇前辈上的csdn上面的代码改的,我这里就不找了,有小伙伴知道的话在评论区里留言或者发我邮箱,或者等我找到后在把引用信息添加进去。具体修改的代码部分是:添加发送多个收件人,添加发送附件(可以发送多个附件)
smtp.h文件
#ifndef __SMTP_H__
#define __SMTP_H__
#include <unistd.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int MAX_EMAIL_MESSAGE_LEN = 4096;
static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char *base64Encode(char const *origStr, unsigned int origLength);
char *base64Decode(char const *origStr, unsigned int *resLength);
class Smtp
{
public:
Smtp();
Smtp(
int port,
string srvDomain, //smtp服务器域名
string userName, //用户名
string password, //密码
string targetEmail, //目的邮件地址
string emailTitle, //主题
string content //内容
);
~Smtp();
void setSrvDomain(string &strDomain) {
this->_domain = strDomain; }
void setUserName(string &strUser) {
this->_user = strUser; }
void setPass(string &strPass) {
this->_password = strPass; }
void setEmailTitle(string &strTitle) {
this->_title = strTitle; }
void setContent(string &strContent) {
this->_content = strContent; }
void setPort(int nPort) {
this->_port = nPort; }
void addTargetEmail(string targetEmail);
void addAccessory(string fileName);
int sendEmail();
private:
int _port;
string _domain;
string _user;
string _password;
vector<string> _targetAddrs;
string _title;
//邮件正文
string _content;
//附件文件名包含路径
vector<string> _accessories;
char _buff[MAX_EMAIL_MESSAGE_LEN + 1];
int _buffLen;
int _sockClient;
bool _createConn();
bool _send(string &strMessage);
bool _recv();
void _formatEmailHead(string &strEmail);
int _login();
bool _sendEmailHead();
bool _sendTextBody();
bool _sendAccessory(const char *fileName);
bool _sendEnd();
};
#endif // !__SMTP_H__
smtp.cpp文件
#include "smtp.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
char *base64Encode(char const *origStr, unsigned int origLength)
{
unsigned char const *orig = (unsigned char const *)origStr; // in case any input bytes have the MSB set
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]; // allow for trailing '/0'
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